11重庆交通大学学生实验报告实验课名称计算方法A开课实验室数学实验室学院年级专业班级学生姓名学号开课时间至学年第学期评分细则评分报告表述的清晰程度和完整性(20分)程序设计的正确性(40分)实验结果的分析(30分)实验方法的创新性(10分)总成绩教师签名22实验一误差分析实验目的:掌握Newton迭代法,了解拟Newton迭代法,培养编程能力和用迭代法解非线性方程(组)的实践技能。实验环境:MATLAB实验1.1:用二分法求非线性方程f(x)=x3+x2−3x−3=0在1.5附近的根。实验代码:bisecMethod:functionbisecMethod(f_name,a,b)fprintf('二分法求非线性方程的根\n\n');tolerance=1e-6;it_limit=30;fprintf('It.acbfa=f(a)');fprintf('fb=f(b)abs(fb-fa)\n');it=0;Y_a=feval(f_name,a);Y_b=feval(f_name,b);if(Y_a*Y_b0)fprintf('\n\n停止,因为f(a)f(b)0\n');fprintf('\n重新给出端点a或者b.\n');elsewhile1it=it+1;c=(a+b)/2;Y_c=feval(f_name,c);fprintf('%3.0f%10.6f%10.6f',it,a,c);fprintf('%10.6f%10.6f%10.6f%12.3e\n',b,Y_a,Y_b,abs(Y_b-Y_a));%fprintf('%12.3e\n',abs(Y_b-Y_a))if(abs(b-a)=tolerance)fprintf('结果满足允许误差tolerance=1e-6.\n');breakendif(itit_limit)fprintf('迭代超过限定次数.\n');breakendif(Y_a*Y_c=0)b=c;Y_b=Y_c;elsea=c;Y_a=Y_c;endendfprintf('最终结果:x*=%12.6f\n',c);endchap4_fun1.mfunctionf=chap1_fun1(x)f=x^3+x^2-3*x-3;在命令窗口调用M文件bisecMethod(f_name’,a,b)bisecMethod('chap4_fun1',1,2)33二分法求非线性方程的根It.acbfa=f(a)fb=f(b)abs(fb-fa)11.0000001.5000002.000000-4.0000003.0000007.000e+0021.5000001.7500002.000000-1.8750003.0000004.875e+0031.5000001.6250001.750000-1.8750000.1718752.047e+0041.6250001.6875001.750000-0.9433590.1718751.115e+0051.6875001.7187501.750000-0.4094240.1718755.813e-0161.7187501.7343751.750000-0.1247860.1718752.967e-0171.7187501.7265631.734375-0.1247860.0220301.468e-0181.7265631.7304691.734375-0.0517550.0220307.379e-0291.7304691.7324221.734375-0.0149570.0220303.699e-02101.7304691.7314451.732422-0.0149570.0035131.847e-02111.7314451.7319341.732422-0.0057280.0035139.241e-03121.7319341.7321781.732422-0.0011090.0035134.622e-03131.7319341.7320561.732178-0.0011090.0012012.311e-03141.7319341.7319951.732056-0.0011090.0000461.155e-03151.7319951.7320251.732056-0.0005320.0000465.776e-04161.7320251.7320401.732056-0.0002430.0000462.888e-04171.7320401.7320481.732056-0.0000980.0000461.444e-04181.7320481.7320521.732056-0.0000260.0000467.221e-05191.7320481.7320501.732052-0.0000260.0000103.610e-05201.7320501.7320511.732052-0.0000080.0000101.805e-05211.7320501.7320501.732051-0.0000080.0000019.026e-06结果满足允许误差tolerance=1e-6.最终结果:x*=1.732050实验1.2:公元1225年,Lenardo宣布他求得方x3+2x2+10x−20=0实验代码:functionx=Newton(fname,dname,x0,e,N)ifnargin5,N=500;endifnargin4,e=1e-4;endx=x0;x0=x+2*e;k=0;fprintf('It.no=%2dx%[2d]=%12.9f\n',k,k,x)whileabs(x0-x)e&kN,k=k+1;x0=x;x=x0-feval(fname,x0)/feval(dname,x0);fprintf('It.no=%2dx[%2d]=%12.9f\n',k,k,x)endifk==N,fprintf('已达到迭代次数上限');end在命令窗口编写内嵌函数表达式,并调用函数M文件Newton:fun=inline('x^3+2*x^2+10*x-20');dfun=inline('3*x^2+4*x+10');44Newton(fun,dfun,1.5,0.5e-6);It.no=0xIt.no=1x[1]=1.373626374It.no=2x[2]=1.368814820It.no=3x[3]=1.368808108It.no=4x[4]=1.368808108实验二解线性方程组的迭代法实验目的:掌握列主元Gauss消去法,Jacobi迭代法,Gauss-Seidel迭代法和超松弛迭代法,培养编程能力和用迭代法解线性方程组的实践技能。实验环境:MATLAB实验2.1编写一个求解线性代数方程组的列主元Gauss消去法的通用程序,然后用该程序解如下线性代数方程组:000.18338.06867.01.121.1331.11119.081.0729.0321xxx实验代码:clearfprintf(‘增广矩阵’)A=[0.792,0.81,0.9,0.6867;1,1,1,0.8338;1.331,1.21,1.1,1.000]%输入增广矩阵%第一次选主元,第三行和第一行交换fprintf('第一次选主元后的增广矩阵')tempo=A(3,:);A(3,:)=A(1,:);A(1,:)=tempo;A%第一次消元fprintf('第一次消元后的增广矩阵')A(2,:)=A(2,:)-A(1,:)*A(2,1)/A(1,1);A(3,:)=A(3,:)-A(1,:)*A(3,1)/A(1,1);A%第二次选主元,第三行和第二行交换fprintf('第二次选主元后的增广矩阵')tempo=A(3,:);A(3,:)=A(2,:);A(3,:)=tempo%第二次消元fprintf('第二次消元后的增广矩阵')A(3,:)=A(3,:)-A(2,:)*A(3,2)/A(2,2);A%回代求值fprintf('回代求值')x(3)=A(3,4)/A(3,3);x(2)=(A(2,4)-A(2,3)*x(3))/A(2,2);x(1)=(A(1,4)-A(1,2:3)*x(2:3)')/A(1,1);x保存为文件名:chap3_155在命令窗口调用M文件chap3_1:chap3_1增广矩阵A=0.79200.81000.90000.68671.00001.00001.00000.83381.33101.21001.10001.0000第一次选主元后的增广矩阵A=1.33101.21001.10001.00001.00001.00001.00000.83380.79200.81000.90000.6867第一次消元后的增广矩阵A=1.33101.21001.10001.000000.09090.17360.082500.09000.24550.0917第二次选主元后的增广矩阵A=1.33101.21001.10001.000000.09090.17360.082500.09000.24550.0917第二次消元后的增广矩阵A=1.33101.21001.10001.000000.09090.17360.08250-0.00000.07360.0100代回求值x=0.04990.64810.1358实验2.2对于如下线性代数方程组:661166122111221321xx(1)用分量形式进行Jacobi迭代(2)用矩阵形式进行Jacobi迭代(3)用分量形式进行Gauss-Seidel迭代(4)用矩阵形式进行Gauss-Seidel迭代实验代码:(1)用分量形式进行Jacobi迭代:functionx=chap3_3clear;fprintf('Jacobi迭代法')x1_(1)=0;x2_(1)=0;x3_(1)=0;fori=1:10x1_(i+1)=6-2*x2_(i)+2*x3_(i);x2_(i+1)=6-x1_(i)-x3_(i);x3_(i+1)=11-2*x1_(i)-2*x2_(i);endx=[x1_',x2_',x3_']chap3_3Jacobi迭代法x=000661116-11-13231231231231231231231231实验结果ans=77000661116-11-13231231231231231231231231(2)用矩阵形式进行Jacobi迭代functionx=Jacobi(A,b,x0,eps)D=diag(diag(A));D=inv(D);L=triu(A,-1);U=triu(A,1);B=-D*(L+U);f=D*b;k=0;x0=x0;x=B*x0+f;fprintf('kx1_(k)x2_(k)x3_(k)\n')fprintf('%2d%4.0f%4.0f%4.0f\n',k,x0)whilenorm(x-x0)=epsx0=x;x=B*x0+f;k=k+1;fprintf('%2d%4.0f%4.0f%4.0f\n',k,x0)end实验结果A=[1,2,-2;1,1,1;2,2,1];b=[6;6;11];x0=[0;0;0];eps=1e-3;x=Jacobi(A,b,x0,eps);kx1_(k)x2_(k)x3_(k)000016611220-28-123503879(3)用分量形式进行Gauss-Seidel迭代88functionx=chap3_4clear;fprintf('Gauss-Seidel迭代法:')x1_(1)=0;x2_(1)=0;x3_(1)=0;fori=1:10x1_(i+1)=6-2*x2_(i)+2*x3_(i);x2_(i+1)=6-x1_(i)-x3_(i);x3_(i+1)=11