数值计算方法上机答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

本科实验报告课程名称:实验项目:实验地点:机房专业班级:采矿1206学号:2012002896学生姓名:康明月指导教师:2014年7月3日一、求非线性方程的根。1、求方程()cos0fxxx在01.5x附近的是根,要求精度满足3110kkxx.(牛顿切线法)f=inline('x-cos(x)');%f(x)df=inline('1+sin(x)');%f'(x)n=1;x0=input('x0=');del=input('del=');N=input('N=');fprintf('\nkx(k)');fprintf('\n%2d%f',0,x0);F0=f(x0);dF0=df(x0);whilenNifdF0==0fprintf('导数为0,迭代无法继续进行.');return;endx1=x0-F0/dF0;F1=f(x1);dF1=df(x1);if((abs(x1-x0)del)|abs(F1)del)fprintf('\n\n结果:%f\n',x1);return;endfprintf('\n%2d%f',n,x1);n=n+1;x0=x1;F0=F1;dF0=dF1;endfprintf('\n\n%d次迭代后未达到精度要求.\n',N);NewtonIterationx0=1.5del=1e-4N=100kx(k)01.50000010.78447220.739519结果:0.7390852、求方程32()0.80fxxx在01x附近的是根,求出具有思维有效数字的根近似值..(简单迭代法)clearclcphi=inline('(0.8+x^2)^(1/3)');%迭代函数x0=input('x0=');del=input('del=');N=input('N=');n=1;fprintf('\n%2d%f',0,x0);whilenNx=phi(x0);ifabs(x-x0)delfprintf('\n\n近似解=%f\n',x);returnendfprintf('\n%2d%f',n,x);n=n+1;x0=x;endfprintf('\n\n%fd次迭代后未达到精度要求.\n',N)x0=1del=1e-4N=10001.00000011.21644021.31611631.36300441.38518051.39568861.40067171.40303481.40415591.404687101.404939111.405059近似解=1.405116100.000000d次迭代后未达到精度要求.二、求解线性方程组(直接法或迭代法)1、22118118344108318311231224321xxxxx(列主元素消元法)a=input('a=')%[2,2,1,-3,8;-2,1,-1,-3,1;8,-1,3,8,-1;10,4,4,3,8];[p,n]=size(a);forw=1:p[x,y]=find(a(w:p,w)==max(max(a(w:p,w))));q=a(w,:);a(w,:)=a(x,:);a(x,:)=q;endforj=1:(p-1)fori=(j+1):pa(i,:)=a(j,j)/a(i,j).*a(i,:)-a(j,:);endendm=p;whilem1s(m)=a(m,n);j=p;while((j2)&(j=m+1)&(jn))s(m)=s(m)-a(m,j)*x(j);j=j-1;endx(m)=s(m)/a(m,m);m=m-1;xenda=[221-38;-21-1-31;8-138-1;104438]a=221-38-21-1-318-138-1104438x=1.0000-1.00002.0000-2.0000。误差限精确解、)(61,654321818.117304.29236.24054322.2223.0803.2155.43211.0918.491.0017.0812.0101.0999.817.12705.61907.311.3501.45.115.0135.007.13.61.21211.3410006.1007.1991.2615.0031.17,2EExbAbAxfunction[x,det,flag]=Gauss(A,b)[n,m]=size(A);nb=length(b);ifn~=merror('therowsandcloumsofAmustbeequal!');return;endifm~=nberror('therowsandcloumsofAmustbeequalthelengthofb!');return;endflag='OK';det=1;x=zeros(n,1);fork=1:(n-1)max1=0;fori=k:nifabs(A(i,k))max1max1=abs(A(i,k));r=i;endendifmax11e-6flag='failure';return;endifrkforj=k:nz=A(k,j);A(k,j)=A(r,j);A(r,j)=z;endz=b(k);b(k)=b(r);b(r)=z;det=-det;endfori=k+1:nm=A(i,k)/A(k,k);forj=k+1:nA(i,j)=A(i,j)-m*A(k,j);endb(i)=b(i)-m*b(k);enddet=det*A(k,k);enddet=det*A(n,n);ifabs(A(n,n))1e-6flag='faliure';return;endfork=n:-1:1forj=k+1:nb(k)=b(k)-A(k,j)*x(j);endx(k)=b(k)/A(k,k);endx(k)=b(k)/A(k,k);endXDetflagA=vpa([17.031-0.615-2.9911.007-1.0060;-134.211-1-2.16.3-1.7;00.513-0.51-1.5;4.5013.11-3.907-61.70512.178.999;0.101-0.812-0.017-0.914.9180.1;1234.5521.80],6)b=vpa([0.23-22.32254240.23629.304-117.818],6);A=[17.031,-0.615,-2.991,1.007,-1.006,0][-1.0,34.211,-1.0,-2.1,6.3,-1.7][0,0.5,13.0,-0.5,1.0,-1.5][4.501,3.11,-3.907,-61.705,12.17,8.999][0.101,-0.812,-0.017,-0.91,4.918,0.1][1.0,2.0,3.0,4.5,5.0,21.8]Gauss(A,b)A=[17.031,-0.615,-2.991,1.007,-1.006,0][-1.0,34.211,-1.0,-2.1,6.3,-1.7][0,0.5,13.0,-0.5,1.0,-1.5][4.501,3.11,-3.907,-61.705,12.17,8.999][0.101,-0.812,-0.017,-0.91,4.918,0.1][1.0,2.0,3.0,4.5,5.0,21.8]ans=1.0000-2.00002.9999-4.00015.0000-6.0008418823.40443.77424.24767.87589.70308.24955.0,1992.42173526137114371107111115219821543811221712421157131143111142112528114112711027321341173EExbAT其中精确解、clearclcn=input('n=');A=input('A=');b=input('b=');x=input('x=');epsilon=input('\n精度=');N=input('\n最大迭代次数N=');fprintf('\n%d:',0);fori=1:nfprintf('%f',x(i));end%以下是迭代过程fork=1:N%这是第k步迭代,迭代前的向量在x0[]中,迭代后的向量在x[]中;normal=0;fori=1:nt=x(i);x(i)=b(i);forj=1:nifj~=ix(i)=x(i)-A(i,j)*x(j);endendx(i)=x(i)/A(i,i);temp=abs(x(i)-t);%求范数于迭代在同一个循环中;iftempnormalnormal=temp;%这里用的是无穷范数endend%第i不迭代结束;fprintf('\n%d:',k);fori=1:nfprintf('%f',x(i));%输出迭代过程endifnormalepsilonreturnendendfprintf('\n\n迭代%d次后仍未求得满足精度的解\n',N);n=8A=[17,1,4,3,-1,2,3,-7;210-17-211-4;-11-82-52-11;241-11134-1;1317-151-24;-217-1212-18;345128-192;5111-11-710]b=[71;43;-11;-37;-61;52;-73;21]x0=[0;0;0;0;0;0;0;0]精度=1e-4最大迭代次数N=1000:0.0000000.0000000.0000000.0000000.0000000.0000000.0000000.0000001:4.1764714.3000001.3750003.3636364.0666674.3333333.8421052.1000002:2.9225162.0834500.5552548.5693667.2030702.3916968.4193711.7707083:1.796703-1.159744-1.3226198.9108208.2232983.3576457.2909535.8926324:4.2118430.507676-1.2417916.9272028.8477911.5521346.9022046.1490355:4.8681201.868149-2.5922027.3796888.4218241.2958416.8839584.9358786:4.5347650.742110-2.3382647.8659948.5206412.9955406.6619884.5317467:4.1340460.204098-1.9753807.8475618.5522223.1606677.1482374.4215358:3.9348030.230528-2.0501637.8467958.3502823.0342277.1274764.9682709:4.1827670.456461-1.8566067.7032878.4770762.7098147.0649175.05072110:4.2398730.623716-2.0374607.7409308.4934282.5261337.0804484.90047111:4.2239090.527844-2.0911437.7663918.4818132.7290846.9876304.90038812:4.2294720.494477-2.0251787.7442958.4957

1 / 23
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功