Excel VBA オイラー法により、4つのサブルーチンに分け、dy/dx=-x/2y(初期値x=0,y=2、刻み幅h=0.1、xの範囲0≦x≦1)を解きたいのですが、画像のような結果になってしまいます。原因を教えてください。 以下コードのコピペ Option Explicit Sub Main_Runge() Dim x0 As Single, y0 As Single Read_Data Cal_Error -1 Out_Result Runge x0 End Sub Sub Read_Data() Dim n As Integer Dim i As Integer Dim yu As Integer Dim xu As Single, y0 As Single Dim h As Single, x0 As Single x0 = Cells(1, "B") y0 = Cells(2, "B") h = Cells(3, "B") xu = Cells(4, "B") n = (xu - x0) / h End Sub Sub Cal_Error(x0 As Single) Dim Ty As Single, y0 As Single Dim e As Single Ty = Fng(x0) '真の値 e = Abs(Ty - y0) End Sub Sub Out_Result() Dim x0 As Single, y0 As Single Dim Ty As Single, e As Single Cells(2, "D") = x0 Cells(2, "E") = y0 Cells(2, "F") = Ty Cells(2, "G") = e End Sub Sub Runge(x0 As Single) Dim i As Integer, n As Integer Dim h As Single, Ty As Single, e As Single Dim y0 As Single For i = 0 To n - 1 y0 = y0 + h * Fnf(x0, y0) x0 = x0 + h Ty = Fng(x0) e = Abs(y0 - Ty) Cells(i + 3, "D") = x0 Cells(i + 3, "E") = Ty Cells(i + 3, "F") = y0 Cells(i + 3, "G") = e Next i End Sub Function Fnf(x As Single, y As Single) As Single Fnf = -0.5 * x / y End Function Function Fng(x As Single) As Single Fng = Sqr(4 - 0.5 * x ^ 2) End Function
Excel