プログラムリスト
VBAのプログラムリストは以下のようになります。
Sub Main()
'初期入力
H = Sheets("Sheet1").Cells(8, 3).Value
d = Sheets("Sheet1").Cells(9, 3).Value
B = Sheets("Sheet1").Cells(10, 3).Value
g = Sheets("Sheet1").Cells(11, 3).Value
a = Sheets("Sheet1").Cells(12, 3).Value
C0 = Sheets("Sheet1").Cells(13, 3).Value
C1 = Sheets("Sheet1").Cells(14, 3).Value
x = Sheets("Sheet1").Cells(15, 3).Value
dx = Sheets("Sheet1").Cells(16, 3).Value
N = Sheets("Sheet1").Cells(17, 3).Value
If x < 0.01 Then x = 0.01 'x入力制限
If x > H - 0.01 Then x = H - 0.01 'x入力制限
'ニュートン法
For i = 0 To N Step 1
k = x - dx
Sheets("Sheet1").Cells(15, 3).Value = k
Qa = Sheets("Sheet1").Cells(21, 3).Value
Qb = Sheets("Sheet1").Cells(22, 3).Value
Fm = Qa - Qb 'Fm=F(x-dx)
k = x
Sheets("Sheet1").Cells(15, 3).Value = k
Qa = Sheets("Sheet1").Cells(21, 3).Value
Qb = Sheets("Sheet1").Cells(22, 3).Value
F0 = Qa - Qb 'F0=F(x)
k = x + dx
Sheets("Sheet1").Cells(15, 3).Value = k
Qa = Sheets("Sheet1").Cells(21, 3).Value
Qb = Sheets("Sheet1").Cells(22, 3).Value
Fp = Qa - Qb 'Fp=F(x+dx)
x = x - (2 * F0 * dx) / (Fp - Fm)
'計算結果出力
Sheets("Sheet1").Cells(15, 3).Value = x
Sheets("Sheet1").Cells(25 + i, 1).Value = i
Sheets("Sheet1").Cells(25 + i, 2).Value = x
Sheets("Sheet1").Cells(25 + i, 3).Value = F0
Next i
End Sub