计算方法实验学号:17姓名:李敏丽班级:09信计第二章题目:求方程f(x)=x^3-sinx-12x+1的全部根,ε=1e-6算法:(1)用一般迭代法;(2)用牛顿迭代法;并比较两种迭代的收敛速度。程序:(1)一般迭代法clcx0=-3.5;iter_max=100;ep=1e-6;k=0;whilek=iter_maxx1=(sin(x0)+12*x0-1).^(1/3);ifabs(x1-x0)epbreak;endx0=x1;k=k+1;endx_star=x1,iter=k运行结果:x_star=3.4101+0.0000i;iter=14(2)牛顿迭代法12cos3)(112sin)(2'3xxxfxxxxf①在区间[-4,-3]内clcx1=-3.5;k=0;whilek=100x0=x1;f0=x0.^3-sin(x0)-12*x0+1;f1=3*x0.^2-cos(x0)-12;x1=x0-f0/f1;ifabs(x1-x0)1.0e-6break;endk=k+1;endx_star=x1,iter=k运行结果:x_star=-3.4911;iter=2②在区间[0,1]内clcx1=0.5;k=0;whilek=100x0=x1;f0=x0.^3-sin(x0)-12*x0+1;f1=3*x0.^2-cos(x0)-12;x1=x0-f0/f1;ifabs(x1-x0)1.0e-6break;endk=k+1;endx_star=x1,iter=k运行结果:x_star=0.0770;iter=3③在区间[3,4]内clcx1=3.5;k=0;whilek=100x0=x1;f0=x0.^3-sin(x0)-12*x0+1;f1=3*x0.^2-cos(x0)-12;x1=x0-f0/f1;ifabs(x1-x0)1.0e-6break;endk=k+1;endx_star=x1,iter=k运行结果:x_star=3.4101;iter=3分析:牛顿迭代法的收敛速度比一般迭代法快第三章题目:1、已知42158721048361261120A对矩阵A做LU分解。2、用追赶法解下述方程组Axb,并给出n=10的结果,其中2112112112A,5557b程序:(1)function[L,U]=LU(A)A[n,n]=size(A);L=zeros(n,n);U=zeros(n,n);fori=1:nL(i,i)=1;endfork=1:nforj=k:nU(k,j)=A(k,j)-sum(L(k,1:k-1).*U(1:k-1,j)');endfori=k+1:nL(i,k)=(A(i,k)-sum(L(i,1:k-1).*U(1:k-1,k)'))/U(k,k);endendA=[4215;87210;4836;1261120];[L,U]=LU(A)运行结果:A=42158721048361261120L=1000210012103041U=4215030000210001(2)clca=[0111111111];b=[2222222222];c=[1111111110];r=[-7-5-5-5-5-5-5-5-5-5];u=[0000000000];v=[0000000000];x=[0000000000];u(1)=r(1)/b(1);v(1)=c(1)/b(1);fork=2:10u(k)=(r(k)-u(k-1)*a(k))/(b(k)-v(k-1)*a(k));v(k)=c(k)/(b(k)-v(k-1)*a(k));endx(10)=u(10);fork=1:9x(k)=u(k)-v(k)*x(k+1);endx运行结果:x=[-3.5000-1.0000-3.0000-1.6000-2.8333-1.8571-2.7500-2.0000-0.8182-2.0909]第四章题目:用迭代法解Ax=b,其中b=(5,5,……,5)T,113241113224111134224111134224111342211342A给定误差1010,用Jacobi和SOR两种迭代法计算,并给出n=10的结果。程序:(1)雅可比迭代clcA=[3-1/2-1/40000000;-1/23-1/2-1/4000000;-1/4-1/23-1/2-1/400000;0-1/4-1/23-1/2-1/40000;00-1/4-1/23-1/2-1/4000;000-1/4-1/23-1/2-1/400;0000-1/4-1/23-1/2-1/40;00000-1/4-1/23-1/2-1/4;000000-1/4-1/23-1/2;0000000-1/4-1/23];x0=[0000000000]';b=[5555555555]';L=[0000000000;1/2000000000;1/41/200000000;01/41/20000000;001/41/2000000;0001/41/200000;00001/41/20000;000001/41/2000;0000001/41/200;00000001/41/20];U=[01/21/40000000;001/21/4000000;0001/21/400000;00001/21/40000;000001/21/4000;0000001/21/400;00000001/21/40;000000001/21/4;0000000001/2;0000000000];D=[3000000000;0300000000;0030000000;0003000000;0000300000;0000030000;0000003000;0000000300;0000000030;0000000003];BJ=inv(D)*(L+U);FJ=inv(D)*b;N=1000;ep=1e-10;k=0;whilek=iter_maxx1=BJ*x0+fJ;ifnorm((x1-x0),'inf')epbreak;endx0=x1;k=k+1;endx_star=x1,iter=k运行结果:x_star=2.40792.86633.16213.25583.29293.29293.25583.16212.86632.4079iter=31即T12.4079)2.86633.16213.25583.29293.29293.25583.16212.86632.4079(x(2)超松弛迭代法clcA=[3-1/2-1/40000000;-1/23-1/2-1/4000000;-1/4-1/23-1/2-1/400000;0-1/4-1/23-1/2-1/40000;00-1/4-1/23-1/2-1/4000;000-1/4-1/23-1/2-1/400;0000-1/4-1/23-1/2-1/40;00000-1/4-1/23-1/2-1/4;000000-1/4-1/23-1/2;0000000-1/4-1/23];x0=[0000000000]';b=[5555555555]';L=[0000000000;1/2000000000;1/41/200000000;01/41/20000000;001/41/2000000;0001/41/200000;00001/41/20000;000001/41/2000;0000001/41/200;00000001/41/20];U=[01/21/40000000;001/21/4000000;0001/21/400000;00001/21/40000;000001/21/4000;0000001/21/400;00000001/21/40;000000001/21/4;0000000001/2;0000000000];D=[3000000000;0300000000;0030000000;0003000000;0000300000;0000030000;0000003000;0000000300;0000000030;0000000003];w=1.3;Bw=(inv(D-w*L))*((1-w)*D+w*U);Fw=w*(inv(D-w*L))*b;iter_max=1000;ep=1e-10;k=0;whilek=iter_maxx1=Bw*x0+fw;ifnorm((x1-x0),'inf')epbreak;endx0=x1;k=k+1;endx_star=x1,iter=k运行结果:x_star=2.40792.86633.16213.25583.29293.29293.25583.16212.86632.4079iter=25分析:超松弛迭代比雅克比迭代的收敛速度快第五章题目:]5,5[,15)(2xxxf,将]5,5[10等分,作Lagrange插值,将插值函数的图形与)(xfy的图形比较,并给出结论。11,()()nnjkkjjkkjxxyxyxx程序:functiony=lagrange(x0,y0,x)n=length(x0);m=length(x);fori=1:mz=x(i);s=0.0;fork=1:np=1.0;forj=1:nifj~=kp=p*(z-x0(j))/(x0(k)-x0(j));endends=p*y0(k)+s;endy(i)=s;endx=[-5:1:5];y=1./(1+x.^2);x0=[-5:0.1:5];y0=lagrange(x,y,x0);y1=1./(1+x0.^2);%绘制图形plot(x0,y0,'--r')%插值曲线holdonplot(x0,y1,'-b')%原曲线运行结果:第六章题目:分别用复化梯形公式、复化辛卜生公式计算21dxxex,其中71105.0||kkxx.(用区间逐步分半递推算法)程序:⑴复化梯形代码clca=1;b=2;m=1;h=0.5;ep=0.5e-7;f(a)=exp(1);f(b)=2*exp(2);x0=h*(f(a)+f(b));iter_max=100;i=0;whilei=iter_maxk=1;F=0;whilek=2^(m-1)F=F+(a+(2*k-1)*h)*exp(a+(2*k-1)*h);k=k+1;endx1=0.5*x0+h*F;ifabs(x1-x0)epbreak;endm=m+1;h=h/2;x0=x1;i=i+1;endx1i结果:x1=7.3891;i=13(i为迭代次数)⑵复化辛卜生代码clca=1;b=2;n=2;h=(b-a)/n;f1=exp(1);f2=2*exp(2);f=6.7225;S1=(b-a)/6*[f1+4*f+f2];i=0;ep=0.5e-7;whilei100;P=(a+h)*exp(a+h);Q=0;j=2;whilejnP=P+(a+(j+1)*h)*exp(a+(j+1)*h);Q=Q+(a+j*h)*exp(a+j*h);j=j+2;endS=h/3*[f1+4*P+2*Q+f2];if(abs(S1-S)ep)break;endh=h/2;n=2*n;S1=S;i=i+1;endSi运行结果:S=7.3891;i=6分析:复化辛卜生迭代速度比复化梯形公式迭代速度快