Option Base 1
Private h As Single, th As Single, t0 As Single, t As Single, t1 As Single, y0 As Single, y As Single, y1 As Single, yh As Single, yh1 As Single, a As Single, b As Single, e As Single, z As Single, k As Single, k1 As Single, k2 As Single, k3 As Single, k4 As Single
Private i As Integer, m As Integer, n As Integer
Private Function fun(t, y)
fun = -0.011 * y + 0.00236
End Function
Private Sub fx(t, y, k)
k = fun(t, y)
End Sub
Private Sub rk(t0, y0, y1)
t1 = t0
y1 = y0
Call fx(t0, y0, k)
k1 = k
t0 = t1 + h / 2
y0 = y1 + h * k1 / 2
Call fx(t0, y0, k)
k2 = k
y0 = y1 + h * k2 / 2
Call fx(t0, y0, k)
k3 = k
t0 = t1 + h
y0 = y1 + h * k3
Call fx(t0, y0, k)
k4 = k
y1 = y1 + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6
t0 = t1 + h
End Sub
Private Sub command1_click()
a = InputBox("请输入所要计算的求解区间的下限:a=")
b = InputBox("请输入所要计算的求解区间的上限:b=", b = 1)
t0 = InputBox("请输入所求常微分方程的初始条件:t0=")
y0 = InputBox("请输入所求长微分方程的初始条件:y0=")
e = InputBox("请输入所要求的误差限:e=")
h = InputBox("请输入所要求的预设的步长")
th = 1: m = 1: n = 0 '在此:a=t0=0,b=th=1
Picture1.Print
Picture1.Print
Picture1.Print "用四阶R-K方法所得 解";
Picture1.Print Tab(1); "时间";
Picture1.Print Tab(28); "步长";
Picture1.Print Tab(50); "精确解";
Picture1.Print Tab(68); "R-K 法高误差阶的解";
Picture1.Print Tab(90); "R-K 方法所得解";
Picture1.Print
Picture1.Print
Picture1.Print
Do Until t >= th
t = t0
y = y0
Do
t0 = t
y0 = y
Call rk(t0, y0, y1)
yh = y1
h = h / 2
t0 = t
y0 = y
For i = 1 To 2
Call rk(t0, y0, y1)
y0 = y1
Next i
b = Abs((yh - y1) / (2 ^ 4 - 1))
Loop Until b < e
yh1 = (2 ^ 4 * y1 - yh) / (2 ^ 4 - 1)
Do
t0 = t
y0 = y
Call rk(t0, y0, y1)
yh = y1
h = h * 2
t0 = t
y0 = y
For i = 1 To 2
Call rk(t0, y0, y1)
y0 = y1
Next i
b = Abs((yh - y1) / (2 ^ 4 - 1))
Loop Until b >= e
h = h / 2
t0 = t
y0 = y
Call rk(t0, y0, y1)
y0 = y1
z = Sqr(1 + 2 * t0)
'If t >= m * (1 - 0) / 10 - 0.001 And t <= m * (1 - 0) / 10 + 0.001 Then
Picture1.Print Tab(1); "t="; t;
Picture1.Print Tab(28); "h="; h;
Picture1.Print Tab(45); "y(i)="; z;
Picture1.Print Tab(68); "y="; yh1;
Picture1.Print Tab(90); "yi="; y0
m = m + 1
' End If
n = n + 1
h = h * 2
Loop
Picture1.Print "N="; n
Picture1.Print Tab(1); "t="; t;
Picture1.Print Tab(28); "h="; h;
Picture1.Print Tab(45); "y(i)="; z;
Picture1.Print Tab(68); "y="; yh1;
Picture1.Print Tab(90); "yi="; y0
End Sub
结果应该有很多行,为什么就只有几行~~