当前位置:文档之家› 数值计算方法大作业

数值计算方法大作业

目录第一章非线性方程求根 (3)1.1迭代法 (3)1.2牛顿法 (4)1.3弦截法 (5)1.4二分法 (6)第二章插值 (7)2.1线性插值 (7)2.2二次插值 (8)2.3拉格朗日插值 (9)2.4分段线性插值 (10)2.5分段二次插值 (11)第三章数值积分 (13)3.1复化矩形积分法 (13)3.2复化梯形积分法 (14)3.3辛普森积分法 (15)3.4变步长梯形积分法 (16)第四章线性方程组数值法 (17)4.1约当消去法 (17)4.2高斯消去法 (18)4.3三角分解法 (20)4.4雅可比迭代法 (21)4.5高斯—赛德尔迭代法 (23)第五章常积分方程数值法 (25)5.1显示欧拉公式法 (25)5.2欧拉公式预测校正法 (26)5.3改进欧拉公式法 (27)5.4四阶龙格—库塔法 (28)数值计算方法第一章非线性方程求根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("请输入被开方数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例:求56的值。

(ep=10-10)1.3弦截法程序代码:Private Sub Command1_Click()x0 = Val(InputBox("请输入第一个初始值x0"))X1 = Val(InputBox("请输入第二个初始值x1"))ep = Val(InputBox("请输入误差限ep"))f = 0While f = 0X2 = X1 - (X1 ^ 8 - 13) * (X1 - x0) / ((X1 ^ 8 - 13) - (x0 ^ 8 - 13))If Abs(X2 - X1) < ep ThenPrint X2f = 1Elsex0 = X1X1 = X2End IfWendEnd Sub例:求f(x)=x8-13的正根(初始值x1=1,x2=10,ep=10-10)1.4二分法程序代码:Private Sub Command1_Click()a = Val(InputBox("请输入区间端点a"))b = Val(InputBox("请输入区间端点b"))ep = Val(InputBox("请输入误差限ep"))f = 0While f = 0x = (a + b) / 2fx = Exp(-x / 7) * (9 - 2 * x) - 8fa = Exp(-a / 7) * (9 - 2 * a) - 8If fx = 0 Thenf = 1Print "方程的根是", xElseIf fa * fx > 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("请输入第一个结点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二次插值程序代码: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 - 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(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:"))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 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) + ")")) Next if = 0While f = 0xx = Val(InputBox("请输入未知点的自变量x:"))L = 0j = 1While L = 0If xx < x(j) Thenk = j + 1L = 1Elsej = j + 1If j > 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)、(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 < x(1) Thenk = 1f = 1End Ifi = 2Do While f = 0 And i >= n - 1If x0 < x(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 - 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处的值。

相关主题