您好,欢迎访问三七文档
1实验十一一、编程并在计算机上调试修改运行1.P142例1、P145例2、P154例32.P1552、P1567、9二、阅读理解下列程序,并在计算机上运行.1.%利用ex的展开式计算函数ex的值(1)qexp.mfunction[E,k,F]=qexp(x,w,n)E=0;F=1;fork=1:nE=E+F;F=x*F/k;ifabs(E-exp(x))wbreakendend(2)qex.mfunction[E,k,F]=qex(x)E=0;F=1;k=1;whileabs(F)0E=E+F;F=x*F/k;k=k+1;end22.%利用sin(x)的展开式计算其函数值(1)qsin.mfunction[s,k,T]=qsin(x)s=0;T=x;k=1;whileabs(T)0s=s+T;T=-x*x*T/(2*k*(2*k+1));k=k+1;end(2)qsn.mfunction[s,k,T]=qsn(x,w,n)s=0;T=x;fork=1:ns=s+T;T=-x*x*T/(2*k*(2*k+1));ifabs(s-sin(x))wbreakendend33.play.mwhile1x=fix(100*rand);n=7;test=1;fork=1:7numb=int2str(n);disp(['Youhavearightto',numb,'guesses'])disp(['Aguessisanumberbetween0and100'])guess=input('Enteryourguess:');ifguessxdisp('Low')elseifguessxdisp('High')elsedisp('Youwon')test=0;break;endn=n-1;endiftest==1disp('Youlost')endm=input('继续?''Y''or''N''','s');ifm=='Y'continueelsebreakendend一、1.P142例14function[y,k]=Jacobi(A,b,x,e,N)D=diag(diag(A));L=tril(A)-D;U=triu(A)-D;fork=1:Ny=-(D^(-1))*(L+U)*x+(D^(-1))*b;ifnorm(x'-y',inf)ex=y;elsebreakendendifk==Ndisp('µü´úʧ°Ü')endA=[10-1-2;-110-2;-1-15];b=[7.28.34.2]';x=[000]';[y,k]=Jacobi(A,b,x,1e-3,100)y=1.09981.19981.2998k=8[y,k]=Jacobi(A,b,x,1e-4,100)y=1.10001.20001.3000k=510[y,k]=Jacobi(A,b,x,1e-6,100)y=1.10001.20001.3000k=15P145例2function[y,k]=GaussSeidel(A,b,x,e,N)D=diag(diag(A));L=tril(A)-D;U=triu(A)-D;fork=1:Ny=-((D+L)^(-1))*U*x+((D+L)^(-1))*b;ifnorm(x'-y',inf)ex=y;elsebreakendendifk==Ndisp('µü´úʧ°Ü')endA=[10-1-2;-110-2;-1-15];b=[7.28.34.2]';x=[000]';[y,k]=GaussSeidel(A,b,x,1e-3,100)y=61.09991.19991.3000k=5[y,k]=GaussSeidel(A,b,x,1e-4,100)y=1.10001.20001.3000k=7[y,k]=GaussSeidel(A,b,x,1e-6,100)y=1.10001.20001.3000k=9[y,k]=GaussSeidel(A,b,x,1e-7,100)y=1.10001.20001.30007k=10P154例3function[y,k]=SOR(A,b,x,w,e)%wΪËɳÚÒò×Ó£¬eΪ¾«¶ÈD=diag(diag(A));L=tril(A)-D;U=triu(A)-D;k=0;while1y=((D+w*L)^(-1))*((1-w)*D-w*U)*x+w*((D+w*L)^(-1))*b;ifnorm(x'-y',inf)ex=y;k=k+1;elsebreakendendA=[-4111;1-411;11-41;111-4];b=[1111]';x=[0000]';[y,k]=SOR(A,b,x,1,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=20[y,k]=SOR(A,b,x,1.1,1e-5)8y=-1.0000-1.0000-1.0000-1.0000k=16[y,k]=SOR(A,b,x,1.2,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=11[y,k]=SOR(A,b,x,1.3,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=11[y,k]=SOR(A,b,x,1.4,1e-5)y=9-1.0000-1.0000-1.0000-1.0000k=14[y,k]=SOR(A,b,x,1.5,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=17[y,k]=SOR(A,b,x,1.6,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=23[y,k]=SOR(A,b,x,1.7,1e-5)y=-1.000010-1.0000-1.0000-1.0000k=34[y,k]=SOR(A,b,x,1.8,1e-5)y=-1.0000-1.0000-1.0000-1.0000k=542.P155例2JacobiA=[1001-5;18-30;32-81;1-227];b=[-7112317]';x=[0000]';[y,k]=Jacobi(A,b,x,1e-3,100)y=1.00040.4998-1.99993.0002k=1112[y,k]=Jacobi(A,b,x,1e-5,100)y=1.00000.5000-2.00003.0000k=19[y,k]=Jacobi(A,b,[1010]',1e-5,100)y=1.00000.5000-2.00003.0000k=19[y,k]=Jacobi(A,b,[1111]',1e-5,100)y=1.00000.5000-2.00003.0000k=1812GaussSeildA=[1001-5;18-30;32-81;1-227];b=[-7112317]';x=[0000]';[y,k]=GaussSeidel(A,b,x,1e-3,100)y=1.00020.4999-1.99992.9999k=8[y,k]=GaussSeidel(A,b,x,1e-5,100)y=1.00000.5000-2.00003.0000k=12[y,k]=GaussSeidel(A,b,[1010]',1e-5,100)y=1.00000.5000-2.0000133.0000k=12[y,k]=GaussSeidel(A,b,[1111]',1e-5,100)y=1.00000.5000-2.00003.0000k=12P1567(1)JacobiA=[101;-110;12-3];b=[5-7-17]';x=[000]';[y,k]=Jacobi(A,b,x,1e-3,100)迭代失败y=1.9957-5.00243.0011k=10014[y,k]=Jacobi(A,b,x,1e-5,10000)y=2.0000-5.00003.0000k=209GaussSeildA=[101;-110;12-3];b=[5-7-17]';x=[000]';[y,k]=GaussSeidel(A,b,x,1e-3,100)迭代失败y=-1-80k=100[y,k]=GaussSeidel(A,b,x,1e-5,10000)迭代失败y=-1-8015k=10000(2)JacobiA=[10.50.5;0.510.5;0.50.51];b=[00.5-2.5]';x=[000]';[y,k]=Jacobi(A,b,x,1e-3,100)迭代失败y=1.33332.3333-3.6667k=100[y,k]=Jacobi(A,b,x,1e-3,10000)迭代失败y=1.33332.3333-3.6667k=1000016GaussSeildA=[10.50.5;0.510.5;0.50.51];b=[00.5-2.5]';x=[000]';[y,k]=GaussSeidel(A,b,x,1e-5,100)y=1.00002.0000-4.0000k=14[y,k]=GaussSeidel(A,b,x,1e-3,100)y=0.99972.0005-4.0001k=9P1569A=[430;34-1;0-14];b=[1620-12]';x=[000]';[y,k]=SOR(A,b,x,1,0.5e-4)y=171.50013.3333-2.1667k=21[y,k]=SOR(A,b,x,1.5,0.5e-4)y=1.50003.3333-2.1667k=17[y,k]=SOR(A,b,x,1.9,0.5e-4)y=1.50003.3333-2.1666k=113二、1、(1)[E,k,F]=qexp(5,1e-5,1000)18E=148.4132k=22F=2.1212e-006[E,k,F]=qexp(3,1e-4,1000)E=20.0855k=14F=5.4864e-005(2)[E,k,F]=qex(5)E=148.4132k=25519F=0[E,k,F]=qex(3)E=20.0855k=225F=02、(1)[s,k,T]=qsin(pi/3)s=0.8660k=91T=0[s,k,T]=qsin(pi/2)s=201.0000k=98T=0(2)[s,k,T]=qsn(pi/2,1e-4,100)s=1.0000k=5T=-3.5988e-006[s,k,T]=qsn(pi/3,1e-4,10)s=0.8660k=4T=4.1734e-006213、Youhavearightto7guessesAguessisanumberbetween0and100Enteryourguess:12LowYouhavearightto6guessesAguessisanumberbetween0and100Enteryourguess:56HighYouhavearightto5guessesAguessisanumberbetween0and100Enteryourguess:2LowYouhavearightto4guessesAguessisanumberbetween0and100Enteryourguess:1LowYouhavearightto3guessesAguessisanumberbetween0and100Enteryourguess:3LowYouhavearightto2guessesAguessisanumberbetween0and100Enteryourguess:6LowYouhavearightto1guessesAguessisanumberbetween0and100E
本文标题:计算方法实验11
链接地址:https://www.777doc.com/doc-5582700 .html