プログラムリスト
VBAのプログラムリストは以下のようになります。
Dim T(101), OT(101)
Sub Main()
'初期入力
N = Sheets("Sheet1").Cells(8, 3).Value
T0 = Sheets("Sheet1").Cells(6, 3).Value
T1 = Sheets("Sheet1").Cells(7, 3).Value
A = Sheets("Sheet1").Cells(4, 3).Value
DT = Sheets("Sheet1").Cells(10, 3).Value
DX = Sheets("Sheet1").Cells(9, 3).Value
Nt = Sheets("Sheet1").Cells(11, 3).Value
C = A * DT / DX ^ 2
'初期設定
For i = 0 To N - 1
T(i) = T0
Sheets("Sheet1").Cells(15, 2 + i).Value = T(i)
Next i
T(N) = T1
'表題と1行目出力
Sheets("Sheet1").Cells(14, 1).Formula = "時間(s)"
Sheets("Sheet1").Cells(15, 1).Value = 0
For i = 0 To N
Sheets("Sheet1").Cells(14, 2 + i).Formula = DX * i & "(mm)"
Sheets("Sheet1").Cells(15, 2 + i).Value = T(i)
Next i
'繰り返し計算
For j = 1 To Nt
For i = 0 To N
OT(i) = T(i)
Next i
T(0) = T(0) + C * (OT(1) - 2 * OT(0) + OT(0))
For i = 1 To N - 1
T(i) = T(i) + C * (OT(i + 1) - 2 * OT(i) + OT(i - 1))
Next i
'結果出力
Sheets("Sheet1").Cells(15 + j, 1).Value = DT * j
For i = 0 To N
Sheets("Sheet1").Cells(15 + j, 2 + i).Value = T(i)
Next i
Next j
End Sub