プログラムリスト
VBAのプログラムリストは以下のようになります。
Dim F(256), OF(256), OF2(256)
Sub Main()
'初期入力
Pi = 3.141592654
A = Sheets("Sheet1").Cells(4, 3).Value
S = Sheets("Sheet1").Cells(5, 3).Value
Ns = Sheets("Sheet1").Cells(6, 3).Value
L = Sheets("Sheet1").Cells(7, 3).Value
Nl = Sheets("Sheet1").Cells(8, 3).Value
DX = Sheets("Sheet1").Cells(9, 3).Value
DT = Sheets("Sheet1").Cells(10, 3).Value
Nt = Sheets("Sheet1").Cells(11, 3).Value
C = (A ^ 2) * (DT ^ 2) / DX ^ 2
'初期設定
For i = 0 To Nl
F(i) = 0
OF(i) = 0
OF2(i) = 0
Next i
'表題出力
Sheets("Sheet1").Cells(15, 1).Formula = "時間(s)\x(mm)"
For i = 0 To Nl
Sheets("Sheet1").Cells(15, 2 + i).Value = DX * i
Next i
'繰り返し計算
For j = 1 To Nt
T = DT * j
If T < Ns / S Then
F(0) = Sin(2 * Pi * S * T)
Else
F(0) = 0
End If
For i = 0 To Nl
OF2(i) = OF(i)
OF(i) = F(i)
Next i
For i = 1 To Nl - 1
F(i) = 2 * OF(i) - OF2(i) + C * (OF(i + 1) + OF(i - 1) - 2 * OF(i))
Next i
F(Nl) = 0
'結果出力
Sheets("Sheet1").Cells(15 + j, 1).Value = T
For i = 0 To Nl
Sheets("Sheet1").Cells(15 + j, 2 + i).Value = F(i)
Next i
Next j
End Sub
Sheets("Sheet2").Cells(5 + k, 1).Value = k
Sheets("Sheet2").Cells(5 + k, 2).Value = Se / Nk
If Se / Nk < e Then Exit For
Next k
'結果出力
Sheets("Sheet2").Cells(1, 1).Formula = "計算結果"
Sheets("Sheet2").Cells(4, 1).Formula = "計算回数"
Sheets("Sheet2").Cells(4, 2).Formula = "変化量"
For j = 0 To Nj - 1 Step 1
Sheets("Sheet2").Cells(4, 4 + j).Value = j
Next j
For i = 0 To Ni - 1 Step 1
Sheets("Sheet2").Cells(5 + i, 3).Value = i
For j = 0 To Nj - 1 Step 1
Sheets("Sheet2").Cells(5 + i, 4 + j).Value = T(i, j)
Next j
Next i
End Sub