プログラムリスト
VBAのプログラムリストは以下のようになります。
Dim T(101, 101), DT(101, 101), C(101, 101)
Sub Main()
'Sheet2クリア
Sheets("Sheet2").Select
Cells.Select
Selection.Clear
'初期入力
Ni = Sheets("Sheet1").Cells(4, 3).Value
Nj = Sheets("Sheet1").Cells(5, 3).Value
h = Sheets("Sheet1").Cells(6, 3).Value
e = Sheets("Sheet1").Cells(7, 3).Value
Nc = Sheets("Sheet1").Cells(8, 3).Value
Ce = Sheets("Sheet1").Cells(9, 3).Value
'計算部分
For i = 0 To Ni - 1 Step 1
For j = 0 To Nj - 1 Step 1
F = Sheets("Sheet1").Cells(15 + i, 2 + j).Value
If F = Ce Then
C(i, j) = 1
T(i, j) = T(0, 0)
Else
C(i, j) = 0
T(i, j) = F
End If
Next j
Next i
For k = 0 To Nc Step 1
Nk = 0
Se = 0
For i = 0 To Ni - 1 Step 1
For j = 0 To Nj - 1 Step 1
If C(i, j) = 1 Then
DT(i, j) = (T(i - 1, j) + T(i + 1, j) + T(i, j - 1) + T(i, j + 1)) / 4
Nk = Nk + 1
Se = Se + Abs(DT(i, j) - T(i, j))
Else
End If
Next j
Next i
For i = 0 To Ni - 1 Step 1
For j = 0 To Nj - 1 Step 1
If C(i, j) = 1 Then T(i, j) = DT(i, j)
Next j
Next i
'収束状況出力
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