Created
February 19, 2017 08:17
-
-
Save KatsuhiroMorishita/54fee36290911d7c2257ffe467efed04 to your computer and use it in GitHub Desktop.
VBAでニュートン法を実装した例です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function my_func(x) | |
' とある関数(数学)の計算結果を返す | |
my_func = 100 * x ^ 2 + 50 * x - 500 | |
End Function | |
Function diff(x, dx) | |
' 1階微分の結果を返す | |
diff = (my_func(x + dx) - my_func(x)) / dx | |
End Function | |
Sub test() | |
Dim x As Double | |
Dim y As Double | |
Dim count As Integer | |
x = 100 | |
y = 100 | |
count = 1 | |
Do | |
y = my_func(x) ' y = f(x)を求める | |
dydx = diff(x, 0.01) ' f’(x)を求める | |
new_x = -y / dydx + x ' 接線のy=0でnew_xを求める。 | |
x = new_x | |
Cells(count, 2) = count | |
Cells(count, 3) = x | |
Cells(count, 4) = y | |
Cells(count, 5) = dydx | |
count = count + 1 | |
Loop While y > 0.01 And count < 1000 | |
Cells(1, 1) = x | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment