2023年数值计算方法大作业.pdf
目录第一章非线性方程求根.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约当消去法.1 74.2 高斯消去法.184.3 三角分解法.204.4 雅可比迭代法.214.5 高斯一赛德尔迭代法.23第五章常积分方程数值法.255.1 显示欧拉公式法.255.2欧拉公式预测校正法.265.3改善欧拉公式法.275.4 四阶龙格一库塔法.28数值计算方法第一章非线性方程求根1.1迭代法程序代码:Privat e S ub C o mman d 1 _Click()xO=Val(InputBox(请输入初始值 xO)印=V a i(In putBox(请输入误差限ep)f=0W h ile f=0X 1 =(Exp(2*xO)-xO)/5If Abs(X 1-xO)ep The nPrint XIf=1Els exO=X1End IfWendEn d Sub例:求 f(x)=e2x-6 x=0 在 x=0.5 附近的根(e p=1010)5.Form l 309530643479467i n 回1.2牛顿法程序代码:Pr i v a te Sub Comm a ndl_ C 1 i c k()b=Val(I n p u t Box(请输入被开方数xO)ep=Val(In p u t Box(请输入误差限 e p)f=0Wh i 1 ef=0XI=xO-(xO A 2-b)/(2*b)I f A b s(X I-xO)ep ThenP r int XIf=1E ls exO=XlEnd IfW e ndEnd Sub例:求廊的值。(ep=1O10)二 0 Form l|D|回|次|;f T.483314772979451.3弦截法程序代码:Priva t e S ub C o mma n dl_C lick()x 0=Val(I n p u tB ox(请输入第一个初始值xO)XI=Va 1 (InputB o x(请输入第二个初始值x 1 )e p=Vai(InputBox(请输入误差限 ep)f=0W hile f=0X2=X1-(XI A81 3)*(XI-x0)/(X 1 A 8-13)(xO A8-1 3)I f A b s(X2-XI)0 Th e na=xE 1 seb=xEnd I fIf Abs(b a)r5 F orm l(p|Bx=98 y=9.89899749373434 口叵11111二次插值2.3拉格朗日插值程序代码:P r i v ate Sub Comman dl_Click()Dim x(),y()n=Val(In p u tBo x(请输入插值节点数N)ReDim x(n),y(n)For i=OTo nx(i)=Val(Inpu t Box(”请输入插值节点 x(”+Str(i)+”)”)y(i)=Val(I n putBox(”请输入插值节点 y(+St r(i)+”)”)N e x t if=0W h ile f=0 x x=Va 1 (I nputBox(请输入未知点的自变量x:”)Sum =0Fo r i=0 To nt=1F or j=0 T o nI f j i T h e nt=t*(xx x(j)/(x(i)-x(j)En d I fNex t jS u m=Sum+t*y(i)Nex t iP r i n t x=;xx,y=;Sumf=Val(InputBox(是否继续(0/1)”)WendEnd Sub例:已知四点(1 0 0,1 0 )、(81,9)、(64,8).(49,7),求 87处的值。Q.Form lx=87 y=9.32663398692811I :E l|l S3拉格朗日插值2.4 分段线性插值程序代码:Priv a t e S u b C omma n dl_Click()Dim x(),y()n=Val(Inpu t B o x(请输入插值节点数N)ReD i m x(n),y(n)For i=0 T o nx(i)=V a i(InputBox(请输入插值节点 x(+Str(i)+)y(i)=Val(I nputBo x(请输入插值节点 y(+Str(i)+)Next if=0W hi 1e f=0 xx=Val(I nputBox(请输入未知点的自变量x:)L=0j=1While L=0If x x n-1 Thenk=n-1L=1End IfEnd I fWend1 0=(x x-x(k)/(x(k-1)x(k)1 1 =(x x-x(k-1)/(x(k)x(k 1)yy=10*y(k-1)+11*y(k)Pr i nt x=;xx,y=;yyf=V a KInputB o x(是否继续(0/1)W en dEnd S ub例:已知三点(361,19)、(324,1 8)、(289,17),N=2,求 300 处的值。巴 Forml 岂回 应 x=300 y=IT.314285T142857.分段线性插值2.5分段二次插值程序代码:P rivate Su b Comm a n d 1_ Cli c k()D i m x(),y()n=V a l(InputBo x(请输入插值节点数N )ReDim x(n),y(n)For i=0 T o nx(i)=Vai(Inpu t Box(请输入插值节点 x(+St r(i)+)y(i)=V a l(InputB ox(请输入插值节点 y(+St r(i)+)Next if=0W hile f=0 xx=Val(I npu t Box(请输入未知点的自变量x:)I f xO=n-1If xO x(i)T henIf xO-x(i-1)x(i)-xO The nk =i 1f=1E l s ek =if =1E n d I fE l s ei =i +1E nd I fL o o pI f f =0 T he nk=n-1E nd I f1 1 =(xx-x(k+1)*(xx-x(k)/(x(k -1)-x(k +1)*(x(k-1)-x(k)I 2 =(x x-x(k +1)*(xx-x(k -1)/(x(k)-x(k +1)*(x(k)-x(k -1 )1 3 =(xx-x(k)*(xx-x(k-1)/(x(k+1)-x(k)*(x(k+1)-x(k-1)y y =1 1 *y (k -1)+1 2 *y(k)+1 3 *y (k +1)P r i n t x=;xx,y=;y yf =V a l(I np u t B o x(是否继续(0/1)W endE nd S u b例:已知三点(225,1 5)、(196,14)、(1 69,1 3),求 180 处的值。&Form lx=180第三章 数值积分3.1复化矩形积分法程序代码:Priva t e Sub Comm a n d 1 _C lick()a=V a l(In p u tB o x(”请输入积分下限a)b=V al(Inpu t Bo x(”请输入积分上限b)n=Val(Input Box(”请输入积分区间等分数N”)h=(b-a)/nSum =0Fo ri=1 TonS u mSum+(a+(i0.5)*h)八 2N e x t ir=h*SumP r int”复化矩形积分法计算结果:rEnd Sub例:求X?在区间 3,5的积分值,等分区间为10 0。|Click3.2复化梯形积分法程序代码:P r i va t e S u b C o m m a nd l _ C 1 i c k ()a =V a i (I n p u t B o x(“请输入积分下限a )b=V a 1 (I n p u t B o x(请输入积分上限b)n=V a i (I np u t B o x(请输入积分区间等分数N )h=(b-a )/nS u m =0For i=1 To n-1Sum=Sum+(a+i*h)2N e x t it 二 h*(a-2 +b 人 2)/2+h*SumP rin t”复化梯形积分法计算结果:;tEnd Su b例:求X?在区间3,5的积分值,等分区间为100。复化梯形世分法计算结果:32.6668复化梯形积分法3.3辛普生积分法程序代码:P r ivate Su b C o mm a ndl_Click()a=Val(I n p utB o x(”请输入积分下限A:”)b=Val(I nput B ox(n请输入积分上限B:)n=Val(Inp u tBox(”请输入积分区间等分数N:)h=(b-a)/nSum=0w=a+h/2While w bSum=Sum+(w h/2)A 2+4*wA2+(w+h/2)A 2w=w+hWends=Sum*h/6P r i n t”辛普生积分法计算结果:sEnd Su b例:求X2在区间 3,5 的积分值,等分区间为1 0 0oa F orm l|。辛普生租分法计算结果:32.6666666666666辛普生租分法3.4变步长梯形积分法程序代码:Pr i v ate S u b C o mmandl_Click()a=Va 1 (I n p utB o x(请输入积分下限a:)b=Vai(Inp utB o x(”请输入积分上限b:)e p=Val(I n p u t Box(请输入误差限 ep:)n=1h=b-at l=h*(a 2+b 2)/2f=0While f=0Sum=0For i=1 To n-1Sum=Sum+(a+(i 1 /2)*h)A 2Next iT2=t 1 /2+h*Sum/2I f Abs(T2-tl)ep ThenP r int T2f=1E 1 setl=T2h=h/2n=2*nEnd IfWendEnd Sub例:求 X2在区间 3,5 的积分值,误差限e p=0.0 0 01。第四章线性方程组数值解法4.1约当消去法程序代码:P r i v a t e S u b C o m m a nd l _ C l i c k()D i m a()n=V a i (I np u t B o x(请输入方程的个数N:)R e D i m a (n,n+1 )F o r i =1 T o nF o r j =1 T o n+1a(i ,j )=V a l(I n p u t B o x(”请输入增广矩阵 A(+S t r(i)+S t r (j)+”)=)N e xt jN ext iF o r k =1 T o nm =a (k,k)F o r j =k T o n+1a(k,j)=a (k ,j )/mN ext jF o r i =1 T o nI f i k T henm =a(i ,k )F o r j =k T o n+1a(i,j)=a (i,j)-a (k,j )*mN ext jE nd I fN ext iN ext kF o r i =1 T o nP r i n t x(”+S t r (i)+”)=;a(i ,n+1)N ext iE nd S u b例:已知方程组,求 X 0 2 X 3 的值。X 1+X2+X 3=2 02 Xt+3 X2+X3=3 8X 1+4X2+X3=3 5annvimsDi5rjr(i,114.2高斯消去法程序代码:Pr i vate Sub Com man d 1 _Click()Dim a(),x()n=Val(InputBox(请输入方程个数N:)ReDim a(n,n+1),x(n)For i=1 To nFo r j=1 To n+1a(i,j)=Val(In p u tB o x(请输入增广矩阵A(+St r(i)+Str(j)+):)Next jNe x t iFo r k=1 To n-1m=a(k,k)Fo r j=k To n+1a(k,j)=a(k,j)/mNext jF o r i=k+l T o nm=a(i,k)F or j=kTo n+1a(i,j)=a(i,j)-a(k,j)*mNext jN e x t iNext kx(n)=a(n,n+l)/a (n,n)F o r i=n-1 To 1 Step-1Sum=0For j=i+1 To nS u m=Sum+a(i,j)*x(j)Next jx(i)=a(i,n+1)SumN e xt iFo r i=1 To nPrint MxC+St r(i)+”)=;x(i)Ne x t iEnd S ub例:已知方程组,求X i例X 3的值。2X1+5X2+X3=2 1 j T henSum=0Fo r k=1 To j-1Sum=S um+1 (i,k)*u(k,j)Ne x t k1 (i,j)=(a(i,j)-Sum)/u(j,j)E 1 seSum=0Fork=1 To i-1Sum=Sum+1(i,k)*u(k,j)Nex t ku(i,j)=a(i,j)-SumEnd IfN e xt jNext iF o r i=1 To nSum=0For j=1 To i-1Sum=Sum+1 (i,j)*y(j)Next jy(i)=b(i)-SumNext iF o r i=n To 1 Step-1S um=0Fo r j=i+1 To nSum=S u m+u(i,j)*x(j)Next jx(i)=(y(i)-Sum)/u(i,i)Nex t iFor i=1 To nPrint”x(+S t r(i)+”户;xNext iEnd Su b例:已知方程组,求X“22XI+3X2+4X3=38X 1 +7X2+3 x3=65,+2X 2+X3=33X3的值。D Horml|D II W|1划=VJSJJVr1234.4雅可比迭代法程序代码:D i m a(),b(),xO(),x()n=V a i (I np u t B o x(请输入方程个数N:)R e D i m a (n,n),b(n),xO (n),x(n)ep =V a l (I np u t B o x(请输入误差限 E P:)nm a x=V a i (I n P u t B o x(请输入最大迭代次数N m a x:)F o r i =1 T o nF o r j =1 T o na (i,j)=V a i (I np u t B o x(请输入系数矩阵 A (+S t r(i )+,”S t r(j)+”):)N e x t jb(i)=V a l(I np u t B o x(请输入右端常数项 B(+S t r (i)+):”)N ex t iF o r i =1 T o nxO (i)=V a l(I np u t B o x(请输入初始解 X O (+S t r(i)+):H)N ext ik =0f =0W hi l e f =0M a x=0F o r i =1 T o nS u m =0F o r j =1 T o nS u m =S u m +a(i,j)*x 0(j)N ext jd =(b(i)-S u m)/a(i ,i)x(i)=xO (i)+dI f M a x A b s (d )T h enM a x=A bs (d)E nd I fN ext ik =k +1I f M a x nm a x T h enP r i n t ”迭代失败f =1E l s eF o r i =1 T o nx 0 (i )=x(i)N ext iE nd I fE nd I fW e n dE n d S u b例:已知方程组,求L X 2 X 3 X 4的值。(ep =0.0 0 0 1,最大迭代次数10 0 0,初始值均为0)r 7 X 1+2 X 2+3 X 3+X4=5XI+5 X 2+2 X3+X4=42XI+X 2+8 X 3+3 X,i=2、X,+4 X2+X3+7 X4=7一t j Forml I=百-二_ x(1)=.582814840156221e x(2)=.428034813659462)x(3)=-.2126124332973741(x(4)=.702599861748731_V If.-Mz;雅可比迭代法 14.5 高斯一赛德尔迭代法程序代码:P r i v at e Sub Comman d 1 _ Click()Dim a(),b(),x()n=Val(Inp u tBox(请输入方程个数N:)ep=0.000 1nm a x=10000ReD i m a(n,n),b(n),x(n)F o r i=1 T o nFo r j=1 To na(i,j)=Va l(Inp u tBox(请输入系数矩阵 A(+St r(i)+Str(j)+):)Next jb(i)=Val(InputBox(请输入右端常数项 b(:+Str(i)+):)N e xt iF or i=1 To nx(i)=Val(InputBox(请输入初始解 X(:+S t r(i)+):)Nex t ik=0f=0While f=0Max=0F o r i=1 To nSum=0F or j=1 T o nSum=Sum+a(i,j)*x(j)N e xt jd=(b(i)-S um)/a(i,i)x(i)=x(i)+dI f M a x A b s(d)The nMa x=Abs(d)End IfN e x t ik=k+1If Max nmax ThenPrint”迭代失败”f=1End IfEnd IfWendEn d S u b例:已知方程组,求X1 X 2 X3的值。(初始值均为0)5 X.+X2+2X3=2X1+3X2+X3=8、3XI+X2+7X3=6)u0N Form lx(1)=.375018076390439x(2)=2.57498938856988x(3)=.650009262943062J高斯塞德尔迭代法第五章常微分方程的数值解5.1显示欧拉法程序代码:P r i v a t e S u b C o m m a nd 1 _ C l i c k()x 0 =V a 1 (I np u t B o x(请输入自变量初始值xO )y O =V a i (I n p u t B o x(请输入自变量初始值y O )xn=V a i (I np u t B o x(请输入自变量值终值xn )n=丫 2 1(1 1 1 8。*(请输入区间等分数”)h=(x n-xO)/nX 1 =xO +hI f X I xn T h e nY1 =y 0 +h*(3 +(y O -xO)3)P r i nt x 1 =:X I,y l=;Y1xO =X Iy 0 =Y1E I s eE nd I fE nd S u b例:用欧拉法计算 d y /d x=3+(y-x)/y (0)=0.5 (OWx Wl)。区间等分数N=4。5 Formlxl=.25 yl=1.281255.2 欧拉公式预测校正法程序代码:P r i va t e S u b C o m m a nd l _ C 1 i c k ()xO =V a i (I np u t B o x(请输入自变量初始值xO )y O =V a i (I np u t B o x(请输入自变量初始值y O )xn=V a 1 (I np u t B o x(请输入自变量值终值x n)n=V a 1 (I np u t B o x(请输入区间等分数”)h=(xn-x 0 )/nX I =xO +hI f X I xn T h enf =1E l s ek 1 =x 0 *y O +4k 2 =(xO +h/2 )*(y O +h*k 1 /2)+4k 3 =(xO +h/2)*(y 0 +h*k 2/2)+4k 4 =X I *(y O +h*k 3)+4Y1 =y O +h*(k l +2 *k 2 +2 *k 3 +k 4 )/6P r i n t x(+S t r (i)+)=;X 1 ,y(+S t r(i)+)=;Y1xO =X Iy O =Y1E nd I fi =i +1W endE n d S u b例:用四阶龙格库塔法计算d y/d x=x y +4 ,y (0)=0 (O W x W l)。区间等分数N=4。5 Form lx(1)=.25x(2)=.5x(3)=.75x(4)=1y(D=y(2)=y(3)=y(4)=1.021080017089842.175274676146123.631149834880875.64264289565875四阶龙格-库塔法