1、计算方法实验9实验九一、编程并在计算机上调试修改运行P137 17、18.二、阅读理解下列程序,并在计算机上运行.1. newdd.m % x为初值,w为精度,n为最大迭代次数function a,k=newdd(x,w,n) a=;a(1)=x;for k=1:n if df(x)=0 disp(分母为零) end y=x-f(x)/df(x);a(k+1)=y; if abs(y-x)w x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=x*exp(x)-1;function y=df(x) %导函数y=(1+x)*exp
2、(x);2. ftx17.m %a,b是积分上下限,n等分数function T=ftx17(a,b,n)h=(b-a)/n;x=a:h:b;T=h*(f(a)+2*sum(f(x(2:n)+f(b)/2; function y=f(x) %子函数 y=1+sin(4*x).*exp(-x);3. fsim17.m (复化Simpson求积公式)%a,b是积分上下限,n等分数function s=fsim17(a,b,n)h=(b-a)/(2*n);x1=a:2*h:b;x2=a+h:2*h:b-h;y1=f(x1);y2=f(x2);s=h*(2*sum(y1)-f(a)-f(b)+4*su
3、m(y2)/3;function y=f(x) %子函数y=1+sin(4*x).*exp(-x);4. tx17.m (变步长梯形法) %a,b是积分上下限,w精度function T2,k=tx17(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 xw x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=exp(x)-4*cos(x);function y=df(x) y=exp(x)+4*sin(x); a,
4、k=newdd(pi/4,1e-4,10)a = 0.7854 0.9119 0.9048 0.9048k = 3(2)function a,k=xianjie(x0,x,w,n) a=;a(1)=x;for k=1:n if f(x)-f(x0)=0 disp(分母为零) end y=x-f(x)*(x-x0)/(f(x)-f(x0);a(k+1)=y; if abs(y-x)w x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=exp(x)-4*cos(x); a,k=xianjie(pi/4,pi/2,1e-4,10)a
5、 = 1.5708 0.8770 0.9064 0.9047 0.9048k = 4 a,k=xianjie(pi/4,pi/3,1e-4,10)a = 1.0472 0.8974 0.9052 0.9048 0.9048k = 4 a,k=xianjie(pi/4,pi,1e-4,10)a = 3.1416 0.8393 0.9086 0.9046 0.9048 0.9048k = 5(3)function a,k=kuaisuxianjie(x0,x,w,n) a=;a(1)=x0;for k=1:n if f(x)-f(x0)=0 disp(分母为零) end y=x-f(x)*(x-x
6、0)/(f(x)-f(x0);a(k+1)=y; if abs(y-x)w x0=a(k); x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=exp(x)-4*cos(x); a,k=kuaisuxianjie(pi/4,pi/2,1e-4,100)a = 0.7854 0.8770 0.9064 0.9048 0.9048k = 4 a,k=kuaisuxianjie(pi/4,pi,1e-4,100)a = 0.7854 0.8393 0.9086 0.9047 0.9048 0.9048k = 5第18题(1) fun
7、ction a,k=newdd(x,w,n) a=;a(1)=x;for k=1:n if df(x)=0 disp(分母为零) end y=x-f(x)/df(x);a(k+1)=y; if abs(y-x)w x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=x3+2*x2+10*x-20;function y=df(x) y=3*x2+4*x+10; a,k=newdd(2,1e-6,10)a = 2.0000 1.4667 1.3715 1.3688 1.3688 1.3688k = 5 a,k=newdd(0.5,1
8、e-6,10)a = 0.5000 1.6275 1.3869 1.3689 1.3688 1.3688k = 5 a,k=newdd(5,1e-6,10)a = Columns 1 through 6 5.0000 3.0476 1.9017 1.4404 1.3703 1.3688 Column 7 1.3688k = 6(2)function a,k=xianjie(x0,x,w,n) a=;a(1)=x;for k=1:n if f(x)-f(x0)=0 disp(分母为零) end y=x-f(x)*(x-x0)/(f(x)-f(x0);a(k+1)=y; if abs(y-x)w
9、x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=x3+2*x2+10*x-20; a,k=xianjie(0.5,1,1e-6,10)迭代失败a = Columns 1 through 6 1.0000 1.4746 1.3401 1.3767 1.3666 1.3694 Columns 7 through 11 1.3686 1.3689 1.3688 1.3688 1.3688k = 10 a,k=xianjie(0.5,1,1e-6,100)a = Columns 1 through 6 1.0000 1.4746 1
10、.3401 1.3767 1.3666 1.3694 Columns 7 through 12 1.3686 1.3689 1.3688 1.3688 1.3688 1.3688 Column 13 1.3688k = 12 a,k=xianjie(2,1,1e-6,100)a = Columns 1 through 6 1.0000 1.3043 1.3579 1.3670 1.3685 1.3688 Columns 7 through 10 1.3688 1.3688 1.3688 1.3688k = 9(3)function a,k=kuaisuxianjie(x0,x,w,n) a=;
11、a(1)=x0;for k=1:n if f(x)-f(x0)=0 disp(分母为零) end y=x-f(x)*(x-x0)/(f(x)-f(x0);a(k+1)=y; if abs(y-x)w x0=a(k); x=y; else break endendif k=n disp(迭代失败)endfunction y=f(x)y=x3+2*x2+10*x-20; a,k=kuaisuxianjie(16,1119,1e-6,100)a = Columns 1 through 6 16.0000 15.9963 10.3598 7.9764 5.6634 4.0780 Columns 7 t
12、hrough 12 2.8749 2.0646 1.5959 1.4093 1.3714 1.3688 Columns 13 through 14 1.3688 1.3688k = 13 a,k=kuaisuxianjie(16,11,1e-6,100)a = Columns 1 through 6 16.0000 8.3047 6.8065 4.6558 3.3676 2.3702 Columns 7 through 12 1.7615 1.4635 1.3789 1.3691 1.3688 1.3688k = 11 a,k=kuaisuxianjie(1,11,1e-6,100)a = C
13、olumns 1 through 6 1.0000 1.0419 1.4067 1.3651 1.3688 1.3688 Column 7 1.3688k = 6二、1、 a,k=newdd(0.5,1e-4,10)a = 0.5000 0.5710 0.5672 0.5671k = 3 a,k=newdd(1.5,1e-1,10)a = 1.5000 0.9893 0.6789 0.5766 0.5672k = 4 a,k=newdd(2,1e-1,10)a = 2.0000 1.3784 0.9048 0.6422 0.5715k = 42、 ftx17(0,1,8)ans = 1.3021 ftx17(0,1,16)ans = 1.3067 ftx17(0,1,4)ans =1.28363、 fsim17(0,1,4)ans = 1.3083 fsim17(0,1,8)ans =1.30834、 T2,k=tx17(0,1,1e-5)T2 = 1.3082k = 9 T2,k=tx17(0,1,1e-3)T2 = 1.3082k = 6 T2,k=tx17(0,1,1e-8)T2 = 1.3083k = 14
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1