1、目录第一章 非线性方程求根31.1迭代法31.2牛顿法41.3弦截法51.4二分法6第二章 插值72.1线性插值72.2二次插值82.3拉格朗日插值92.4分段线性插值102.5分段二次插值11第三章 数值积分133.1复化矩形积分法133.2复化梯形积分法143.3辛普森积分法153.4变步长梯形积分法16第四章 线性方程组数值法174.1约当消去法17 4.2高斯消去法184.3三角分解法204.4雅可比迭代法214.5高斯赛德尔迭代法23第五章 常积分方程数值法255.1显示欧拉公式法255.2欧拉公式预测校正法26 5.3改善欧拉公式法275.4四阶龙格库塔法28数值计算措施第一章 非
2、线性方程求根1.1迭代法程序代码:Private Sub Command1_Click()x0 = Val(InputBox(请输入初始值x0)ep = Val(InputBox(请输入误差限ep)f = 0While f = 0X1 = (Exp(2 * x0) - x0) / 5If Abs(X1 - x0) ep ThenPrint X1f = 1Elsex0 = X1End IfWendEnd Sub例:求f(x)=e2x-6x=0在x=0.5附近旳根(ep=10-10)1.2牛顿法程序代码:Private Sub Command1_Click()b = Val(InputBox(请输
3、入被开方数x0)ep = Val(InputBox(请输入误差限ep)f = 0While f = 0X1 = x0 - (x0 2 - b) / (2 * b)If Abs(X1 - x0) ep ThenPrint X1f = 1Elsex0 = X1End IfWendEnd Sub例:求旳值。(ep=10-10)1.3弦截法程序代码:Private Sub Command1_Click()x0 = Val(InputBox(请输入第一种初始值x0)X1 = Val(InputBox(请输入第二个初始值x1)ep = Val(InputBox(请输入误差限ep)f = 0While f
4、= 0X2 = X1 - (X1 8 - 13) * (X1 - x0) / (X1 8 - 13) - (x0 8 - 13)If Abs(X2 - X1) 0 Thena = xElseb = xEnd IfIf Abs(b - a) ep Thenx = (b + a) / 2f = 1Print 方程旳根是, xEnd IfEnd IfWendEnd Sub例:求方程f(x)=e-7/x(9-2x)-8在区间0,1内旳实根。(ep=10-10)第二章 插 值2.1线性插值程序代码:Private Sub Command1_Click()X0 = Val(InputBox(请输入第一种结
5、点X:)Y0 = Val(InputBox(请输入第一种结点Y:)X1 = Val(InputBox(请输入第二个结点X:)Y1 = Val(InputBox(请输入第二个结点Y:)f = 0While f = 0x = Val(InputBox(请输入未知点旳自变量值X:)L0 = (x - X1) / (X0 - X1)L1 = (x - X0) / (X1 - X0)y = L0 * Y0 + L1 * Y1Print x=; x, y=; yf = Val(InputBox(与否继续(0/1):)WendEnd Sub例:已知两点(13 , 1)、(49 , 8),求30处旳值。2.2
6、二次插值程序代码:Private Sub Command1_Click()X0 = Val(InputBox(请输入第一种结点X:)Y0 = Val(InputBox(请输入第一种结点Y:)X1 = Val(InputBox(请输入第二个结点X:)Y1 = Val(InputBox(请输入第二个结点Y:)X2 = Val(InputBox(请输入第三个结点X:)Y2 = Val(InputBox(请输入第三个结点Y:)f = 0While f = 0x = Val(InputBox(请输入未知点旳自变量值X:)L0 = (x - X1) * (x - X2) / (X0 - X1) / (X0
7、 - X2)L1 = (x - X0) * (x - X2) / (X1 - X0) / (X1 - X2)L2 = (x - X0) * (x - X1) / (X2 - X0) / (X2 - X1)y = L0 * Y0 + L1 * Y1 + L2 * Y2Print x=; x, y=; yf = Val(InputBox(与否继续(0/1):)WendEnd Sub例:已知三点(81 ,9)、(100 ,10)、(121 ,10),求98处旳值。2.3拉格朗日插值程序代码:Private Sub Command1_Click()Dim x(), y()n = Val(InputBo
8、x(请输入插值节点数N)ReDim x(n), y(n)For i = 0 To nx(i) = Val(InputBox(请输入插值节点x( + Str(i) + )y(i) = Val(InputBox(请输入插值节点y( + Str(i) + )Next if = 0While f = 0xx = Val(InputBox(请输入未知点旳自变量x:)Sum = 0For i = 0 To nt = 1For j = 0 To nIf j i Thent = t * (xx - x(j) / (x(i) - x(j)End IfNext jSum = Sum + t * y(i)Next
9、iPrint x=; xx, y=; Sumf = Val(InputBox(与否继续(0/1))WendEnd Sub例:已知四点(100 ,10)、(81 ,9)、(64 ,8)、(49 ,7),求87 处旳值。2.4分段线性插值程序代码:Private Sub Command1_Click()Dim x(), y()n = Val(InputBox(请输入插值节点数N)ReDim x(n), y(n)For i = 0 To nx(i) = Val(InputBox(请输入插值节点x( + Str(i) + )y(i) = Val(InputBox(请输入插值节点y( + Str(i)
10、+ )Next if = 0While f = 0xx = Val(InputBox(请输入未知点旳自变量x:)L = 0j = 1While L = 0If xx n - 1 Thenk = n - 1L = 1End IfEnd IfWendl0 = (xx - x(k) / (x(k - 1) - x(k)l1 = (xx - x(k - 1) / (x(k) - x(k - 1)yy = l0 * y(k - 1) + l1 * y(k)Print x=; xx, y=; yyf = Val(InputBox(与否继续(0/1))WendEnd Sub例:已知三点(361 , 19)、
11、(324 ,18)、(289 ,17),N=2,求300处旳值。2.5分段二次插值程序代码:Private Sub Command1_Click()Dim x(), y()n = Val(InputBox(请输入插值节点数N)ReDim x(n), y(n)For i = 0 To nx(i) = Val(InputBox(请输入插值节点x( + Str(i) + )y(i) = Val(InputBox(请输入插值节点y( + Str(i) + )Next if = 0While f = 0xx = Val(InputBox(请输入未知点旳自变量x:)If x0 = n - 1If x0 x
12、(i) ThenIf x0 - x(i - 1) x(i) - x0 Thenk = i - 1f = 1Elsek = if = 1End IfElsei = i + 1End IfLoopIf f = 0 Thenk = n - 1End Ifl1 = (xx - x(k + 1) * (xx - x(k) / (x(k - 1) - x(k + 1) * (x(k - 1) - x(k)l2 = (xx - x(k + 1) * (xx - x(k - 1) / (x(k) - x(k + 1) * (x(k) - x(k - 1)l3 = (xx - x(k) * (xx - x(k
13、- 1) / (x(k + 1) - x(k) * (x(k + 1) - x(k - 1)yy = l1 * y(k - 1) + l2 * y(k) + l3 * y(k + 1)Print x=; xx, y=; yyf = Val(InputBox(与否继续(0/1))WendEnd Sub例:已知三点(225 , 15)、(196 ,14)、(169 ,13),求180处旳值。第三章 数值积分3.1 复化矩形积分法程序代码:Private Sub Command1_Click()a = Val(InputBox(请输入积分下限a)b = Val(InputBox(请输入积分上限b)n
14、 = Val(InputBox(请输入积分区间等分数N)h = (b - a) / nSum = 0For i = 1 To nSum = Sum + (a + (i - 0.5) * h) 2Next ir = h * SumPrint 复化矩形积分法计算成果:; rEnd Sub 例:求X2在区间3,5旳积分值,等分区间为100。3.2复化梯形积分法程序代码:Private Sub Command1_Click()a = Val(InputBox(请输入积分下限a)b = Val(InputBox(请输入积分上限b)n = Val(InputBox(请输入积分区间等分数N)h = (b -
15、 a) / nSum = 0For i = 1 To n - 1Sum = Sum + (a + i * h) 2Next it = h * (a 2 + b 2) / 2 + h * SumPrint 复化梯形积分法计算成果:; tEnd Sub例:求X2在区间3,5旳积分值,等分区间为100。3.3辛普生积分法程序代码:Private Sub Command1_Click()a = Val(InputBox(请输入积分下限A:)b = Val(InputBox(请输入积分上限B:)n = Val(InputBox(请输入积分区间等分数N:)h = (b - a) / nSum = 0w =
16、 a + h / 2While w bSum = Sum + (w - h / 2) 2 + 4 * w 2 + (w + h / 2) 2w = w + hWends = Sum * h / 6Print 辛普生积分法计算成果:; sEnd Sub例:求X2在区间3,5旳积分值,等分区间为100。3.4变步长梯形积分法程序代码:Private Sub Command1_Click()a = Val(InputBox(请输入积分下限a:)b = Val(InputBox(请输入积分上限b:)ep = Val(InputBox(请输入误差限ep:)n = 1h = b - at1 = h * (
17、a 2 + b 2) / 2f = 0While f = 0Sum = 0For i = 1 To n - 1Sum = Sum + (a + (i - 1 / 2) * h) 2Next iT2 = t1 / 2 + h * Sum / 2If Abs(T2 - t1) ep ThenPrint T2f = 1Elset1 = T2h = h / 2n = 2 * nEnd IfWendEnd Sub例:求X2在区间3,5旳积分值,误差限ep=0.0001。第四章 线性方程组数值解法4.1 约当消去法程序代码:Private Sub Command1_Click()Dim a()n = Va
18、l(InputBox(请输入方程旳个数N:)ReDim a(n, n + 1)For i = 1 To nFor j = 1 To n + 1a(i, j) = Val(InputBox(请输入增广矩阵A( + Str(i) + , + Str(j) + )=)Next jNext iFor k = 1 To nm = a(k, k)For j = k To n + 1a(k, j) = a(k, j) / mNext jFor i = 1 To nIf i k Thenm = a(i, k)For j = k To n + 1a(i, j) = a(i, j) - a(k, j) * mNe
19、xt jEnd IfNext iNext kFor i = 1 To nPrint x( + Str(i) + )=; a(i, n + 1)Next iEnd Sub例:已知方程组,求X1 X2 X3旳值。 X1+ X2+ X3=20 2X1+3X2+ X3=38 X1+4X2+ X3=35 4.2 高斯消去法程序代码:Private Sub Command1_Click()Dim a(), x()n = Val(InputBox(请输入方程个数N:)ReDim a(n, n + 1), x(n)For i = 1 To nFor j = 1 To n + 1a(i, j) = Val(In
20、putBox(请输入增广矩阵A( + Str(i) + , + Str(j) + ):)Next jNext iFor k = 1 To n - 1m = a(k, k)For j = k To n + 1a(k, j) = a(k, j) / mNext jFor i = k + 1 To nm = a(i, k)For j = k To n + 1a(i, j) = a(i, j) - a(k, j) * mNext jNext iNext kx(n) = a(n, n + 1) / a(n, n)For i = n - 1 To 1 Step -1Sum = 0For j = i + 1
21、 To nSum = Sum + a(i, j) * x(j)Next jx(i) = a(i, n + 1) - SumNext iFor i = 1 To nPrint x( + Str(i) + )=; x(i)Next iEnd Sub例:已知方程组,求X1 X2 X3旳值。2X1+5X2+ X3=21 4X1+3X2+3X3=31 X1+3X2+2X3=16 4.3 三角分解法程序代码:Private Sub Command1_Click()Dim a(), b(), l(), u(), x(), y()n = Val(InputBox(请输入方程个数N:)ReDim a(n, n)
22、, b(n), l(n, n), u(n, n), x(n), y(n)For i = 1 To nFor j = 1 To na(i, j) = Val(InputBox(请输入系数矩阵A( + Str(i) + , + Str(j) + ):)Next jb(i) = Val(InputBox(请输入右端常数项B( + Str(i) + ):)Next iFor i = 1 To nFor j = 1 To nIf i j ThenSum = 0For k = 1 To j - 1Sum = Sum + l(i, k) * u(k, j)Next kl(i, j) = (a(i, j) -
23、 Sum) / u(j, j)ElseSum = 0For k = 1 To i - 1Sum = Sum + l(i, k) * u(k, j)Next ku(i, j) = a(i, j) - SumEnd IfNext jNext iFor i = 1 To nSum = 0For j = 1 To i - 1Sum = Sum + l(i, j) * y(j)Next jy(i) = b(i) - SumNext iFor i = n To 1 Step -1Sum = 0For j = i + 1 To nSum = Sum + u(i, j) * x(j)Next jx(i) =
24、(y(i) - Sum) / u(i, i)Next iFor i = 1 To nPrint x( + Str(i) + )=; x(i)Next iEnd Sub例:已知方程组,求X1 X2 X3旳值。2X1+3X2+4X3=38 X1+7X2+3X3=65 5X1+2X2+ X3=33 4.4 雅可比迭代法程序代码:Dim a(), b(), x0(), x()n = Val(InputBox(请输入方程个数N:)ReDim a(n, n), b(n), x0(n), x(n)ep = Val(InputBox(请输入误差限EP:)nmax = Val(InputBox(请输入最大迭代次
25、数Nmax:)For i = 1 To nFor j = 1 To na(i, j) = Val(InputBox(请输入系数矩阵A( + Str(i) + , + Str(j) + ):)Next jb(i) = Val(InputBox(请输入右端常数项B( + Str(i) + ):)Next iFor i = 1 To nx0(i) = Val(InputBox(请输入初始解X0( + Str(i) + ):)Next ik = 0f = 0While f = 0Max = 0For i = 1 To nSum = 0For j = 1 To nSum = Sum + a(i, j)
26、* x0(j)Next jd = (b(i) - Sum) / a(i, i)x(i) = x0(i) + dIf Max Abs(d) ThenMax = Abs(d)End IfNext ik = k + 1If Max nmax Then Print 迭代失败 f = 1 Else For i = 1 To n x0(i) = x(i) Next i End IfEnd IfWendEnd Sub例:已知方程组,求X1 X2 X3 X4旳值。(ep=0.0001,最大迭代次数1000,初始值均为0)7X1+2X2+3X3+ X4=5 X1+5X2+2X3+ X4=42X1+ X2+8X3
27、+3X4=2 X1+4X2+ X3+7X4=74.5 高斯-赛德尔迭代法程序代码:Private Sub Command1_Click()Dim a(), b(), x()n = Val(InputBox(请输入方程个数N:)ep = 0.0001nmax = 10000ReDim a(n, n), b(n), x(n)For i = 1 To nFor j = 1 To na(i, j) = Val(InputBox(请输入系数矩阵A( + Str(i) + , + Str(j) + ):)Next jb(i) = Val(InputBox(请输入右端常数项b(: + Str(i) + ):
28、)Next iFor i = 1 To nx(i) = Val(InputBox(请输入初始解X(: + Str(i) + ):)Next ik = 0f = 0While f = 0Max = 0For i = 1 To nSum = 0For j = 1 To nSum = Sum + a(i, j) * x(j)Next jd = (b(i) - Sum) / a(i, i)x(i) = x(i) + dIf Max Abs(d) ThenMax = Abs(d)End IfNext ik = k + 1If Max nmax ThenPrint 迭代失败f = 1End IfEnd I
29、fWendEnd Sub例:已知方程组,求X1 X2 X3旳值。(初始值均为0)5X1+ X2+2X3=2 X1+3X2+ X3=8 3X1+ X2+7X3=6第五章 常微分方程旳数值解5.1显示欧拉法程序代码:Private Sub Command1_Click()x0 = Val(InputBox(请输入自变量初始值x0)y0 = Val(InputBox(请输入自变量初始值y0)xn = Val(InputBox(请输入自变量值终值xn)n = Val(InputBox(请输入区间等分数)h = (xn - x0) / nX1 = x0 + hIf X1 xn ThenY1 = y0 +
30、 h * (3 + (y0 - x0) 3)Print x1=; X1, y1=; Y1x0 = X1y0 = Y1ElseEnd IfEnd Sub例:用欧拉法计算dy/dx=3+(y-x)3,y(0)=0.5(0x1)。区间等分数N=4。5.2欧拉公式预测校正法程序代码:Private Sub Command1_Click()x0 = Val(InputBox(请输入自变量初始值x0)y0 = Val(InputBox(请输入自变量初始值y0)xn = Val(InputBox(请输入自变量值终值xn)n = Val(InputBox(请输入区间等分数)h = (xn - x0) / nX
31、1 = x0 + hIf X1 xn Thenf = 1Elsek1 = x0 * y0 + 4k2 = (x0 + h / 2) * (y0 + h * k1 / 2) + 4k3 = (x0 + h / 2) * (y0 + h * k2 / 2) + 4k4 = X1 * (y0 + h * k3) + 4Y1 = y0 + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6Print x( + Str(i) + )=; X1, y( + Str(i) + )=; Y1x0 = X1y0 = Y1End Ifi = i + 1WendEnd Sub例:用四阶龙格库塔法计算dy/dx=xy+4,y(0)=0(0x1)。区间等分数N=4。