《计算方法实验(共10页).doc》由会员分享,可在线阅读,更多相关《计算方法实验(共10页).doc(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上计算方法实验八一、编程并在计算机上调试修改运行根据P125框图编写Newton法求根程序.以课本P125例1测试,并以不同初值及精度计算习题P1352、3 .二、阅读理解下列程序,并在计算机上运行.1. 比较下列两个复化梯形求积公式程序(1) ftx.m %a,b是积分上下限,n等分数function T=ftx(a,b,n) h=(b-a)/n;T=0;for x=a+h:h:b-h T=T+f(x);endT=h*(2*T+f(a)+f(b)/2;function y=f(x) %子函数if x=0 y=1;else y=sin(x)/x;end(2) ftx1.
2、m %a,b是积分上下限,n等分数function T=ftx1(a,b,n)h=(b-a)/n;x=a:h:b;T=h*(f(0)+2*sum(f(x(2:n)+f(x(n+1)/2; function y=f(x) %子函数if x=0 y=1;else y=sin(x)./x;end2. fsim.m (复化Simpson求积公式)%a,b是积分上下限,n等分数function s=fsim(a,b,n)h=(b-a)/(2*n);x1=a:2*h:b;x2=a+h:2*h:b-h;y1(1)=f(0);y1(2:n+1)=f(x1(2:n+1);y2=f(x2);s=h*(2*sum(
3、y1)-y1(1)-y1(n+1)+4*sum(y2)/3;function y=f(x) %子函数if x=0 y=1;else y=sin(x)./x;end3. tx.m (变步长梯形法) %a,b是积分上下限,w精度function T2,k=tx(a,b,w)h=b-a;T2=h*(f(a)+f(b)/2;T1=0;k=0;while abs(T2-T1)w s=0;x=a+h/2;T1=T2; while xb s=s+f(x);x=x+h; end T2=(T1+h*s)/2; h=h/2;k=k+1;endfunction y=f(x) %子函数if x=0 y=1;else
4、y=sin(x)/x;end三、对复化梯形、Simpson公式及变步长梯形法程序作适当修改,计算P8817 .一、课本P125例1function y=net(x0,e,N)k=1;while 1 if f1(x0)=0 x1=x0-f(x0)/f1(x0); else disp(奇异); break; end if abs(x0-x1) net(0.5,1e-5,100)ans = 0.9784习题P135 例2function y=net(x0,e,N)k=1;while 1 if f1(x0)=0 x1=x0-f(x0)/f1(x0); else disp(奇异); break; end
5、 if abs(x0-x1) net(0.5,1e-5,100)ans = 1.1373 net(1,1e-7,100)ans = 1.1375 net(1.5,1e-3,100)ans = 1.4412习题P135 例3(1)function y=net(x0,e,N)k=1;while 1 if f1(x0)=0 x1=x0-f(x0)/f1(x0); else disp(奇异); break; end if abs(x0-x1) net(2,1e-5,100)ans = 1.1817 net(1,1e-5,100)奇异 net(1.5,1e-6,100)ans = 1.1822 net(
6、2,1e-3,100)ans = 1.6671(2)function y=net(x0,e,N)k=1;while 1 if f1(x0)=0 x1=x0-f(x0)/f1(x0); else disp(奇异); break; end if abs(x0-x1) net(1,1e-7,10)ans = 0.7837 net(1.5,1e-3,5)迭代失败 net(2,1e-6,10)ans = 3.8140 net(1,1e-2,10)ans = 0.6595二、1、 (1) ftx(0,1,8)ans = 0.2701(2) ftx1(0,1,8)ans = 0.27012、 fsim(0,
7、1,4)ans = 0.84723、 T2 k=tx(0,1,1e-6)T2 = 0.8235k = 9 T2 k=tx(0,1,1e-8)T2 = 0.1262k = 12 T2 k=tx(0,1,1e-4)T2 = 0.2768k = 5三、复化梯形function T=ftx(a,b,n) h=(b-a)/n;T=0;for x=a+h:h:b-h T=T+f(x);endT=h*(2*T+f(a)+f(b)/2;function y=f(x) %子函数y=1+exp(-x)*sin(4*x); ftx(0,1,4)ans = 1.8086Simpsonfunction s=fsim(a
8、,b,n)h=(b-a)/(2*n);x1=a:2*h:b;x2=a+h:2*h:b-h;y1(1)=f(0);y1(2:n+1)=f(x1(2:n+1);y2=f(x2);s=h*(2*sum(y1)-y1(1)-y1(n+1)+4*sum(y2)/3;function y=f(x) %子函数y=1+exp(-x).*sin(4*x); fsim(0,1,2)ans = 1.3771变步长梯形法function T2,k=tx(a,b,w)h=b-a;T2=h*(f(a)+f(b)/2;T1=0;k=0;while abs(T2-T1)ws=0;x=a+h/2;T1=T2; while x tx(0,1,1e-6)ans = 1.1149专心-专注-专业