数值计算方法大作业_第1页
数值计算方法大作业_第2页
数值计算方法大作业_第3页
数值计算方法大作业_第4页
数值计算方法大作业_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、目录第一章 非线性方程求根····················31.1迭代法··························

2、3;···········31.2牛顿法·····································

3、83;41.3弦截法······································51.4二分法·········&

4、#183;····························6第二章 插值····················

5、···········72.1线性插值····································72.2二次插值&#

6、183;···································82.3拉格朗日插值············

7、3;···················92.4分段线性插值·····························

8、··102.5分段二次插值·······························11第三章 数值积分·············

9、83;············133.1复化矩形积分法·····························133.2复化梯形积分法····

10、83;························143.3辛普森积分法·······················

11、3;·······153.4变步长梯形积分法···························16第四章 线性方程组数值法···········&

12、#183;······174.1约当消去法·································17 4.2高斯消去法······

13、3;··························184.3三角分解法······················

14、···········204.4雅可比迭代法································214.5高斯赛德尔迭代法···

15、·······················23第五章 常积分方程数值法··················255.1显示欧拉公式法····

16、3;·························255.2欧拉公式预测校正法······················&#

17、183;···26 5.3改进欧拉公式法······························275.4四阶龙格库塔法············&

18、#183;···············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

19、= 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例:求的值。(ep=10-

20、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 = X

21、2End 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 -

22、 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章 插 值2.1线性插值程序代码:Private Sub Command1_Click()X0 = Val(Inp

23、utBox("请输入第一个结点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=&quo

24、t; 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

25、:")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

26、* 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(&

27、quot;请输入插值节点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

28、(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("请

29、输入插值节点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 -

30、 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(Input

31、Box("请输入插值节点数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 W

32、hile 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) *

33、 (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处的值。第3章 数值积分3.1 复化矩形积分法程序代码:Pr

34、ivate Sub Command1_Click()a = Val(InputBox("请输入积分下限a")b = Val(InputBox("请输入积分上限b")n = 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复化梯

35、形积分法程序代码:Private Sub Command1_Click()a = Val(InputBox("请输入积分下限a")b = Val(InputBox("请输入积分上限b")n = Val(InputBox("请输入积分区间等分数N")h = (b - a) / nSum = 0For i = 1 To n - 1Sum = Sum + (a + i * h) 2Next it = h * (a 2 + b 2) / 2 + h * SumPrint "复化梯形积分法计算结果:" tEnd Sub例:求

36、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 = a + h / 2While w < bSum = Sum + (w - h / 2) 2 + 4 * w 2 + (w + h / 2) 2w = w + hWends =

37、 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 * (a 2 + b 2) / 2f = 0While f = 0Sum = 0Fo

38、r 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章 线性方程组数值解法4.1 约当消去法程序代码:Private Sub Command1_Click()Dim a()n = Val(InputBox("请输入方程的个数N:")

39、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) =

40、a(i, j) - a(k, j) * mNext 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

41、+ 1), x(n)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 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) *

42、 mNext jNext iNext kx(n) = a(n, n + 1) / a(n, n)For i = n - 1 To 1 Step -1Sum = 0For j = i + 1 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=

43、16 4.3 三角分解法程序代码:Private Sub Command1_Click()Dim a(), b(), l(), u(), x(), y()n = Val(InputBox("请输入方程个数N:")ReDim a(n, n), 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)

44、= 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) - 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

45、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) = (y(i) - Sum) / u(i, i)Next iFor i = 1 To nPrint "x(" + Str(i) + ")=" x(i)Next iEnd Sub

46、例:已知方程组,求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("请输入最大迭代次数Nmax:")For i = 1 To nFor j = 1 To na(i, j) = Va

47、l(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

48、= 0For j = 1 To nSum = Sum + a(i, j) * 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 < ep Then For i = 1 To n Print "x(" + Str(i) + ")=" x(i) Next i f = 1Else If k > nmax Then Print "迭代失败" f = 1

49、 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+3X4=2 X1+4X2+ X3+7X4=74.5 高斯-赛德尔迭代法程序代码:Private Sub Command1_Click()Dim a(), b(), x()n = Val(InputBox("请输入方程个数N:")ep = 0.0001nma

50、x = 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) + "):")Next iFor i = 1 To nx(i) = Val(InputBox("请输入初始解X(:" + St

51、r(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 < ep ThenFor i = 1 To nPrint "x(" + Str(i) + ")="

52、x(i)Next if = 1ElseIf k > nmax ThenPrint "迭代失败"f = 1End IfEnd IfWendEnd Sub例:已知方程组,求X1 X2 X3的值。(初始值均为0)5X1+ X2+2X3=2 X1+3X2+ X3=8 3X1+ X2+7X3=6第5章 常微分方程的数值解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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论