大连理工大学-矩阵大作业

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

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

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

资源描述

(1)从大到小的顺序的计算程序:functiony=snd(n)formatlongy=0;ifn2disp('请输入大于1的数!')elses=0;i=2;whilei=ns=single(s+(1/(i^2-1)));i=i+1;endy=s;end(2)从小到大的顺序的计算程序:functiony=snx(n)formatlongy=0;ifn2disp('请输入大于1的数!')elses=0;i=n;while1s=single(s+(1/(i^2-1)));i=i-1;ifi==1breakendendy=s;end(3)按两种顺序分别计算并指出有效位数(编制程序时用单精度)①210S的计算结果:②410S的计算结果:③610S的计算结果:计算时的有效位数为七位数。①秦九昭算法计算程序:functiony=qjz(a,x)j=3;i=size(a,2);switchicase1y=a(1);case2y=a(1)*x+a(2);otherwisep=a(1)*x+a(2);whilej=ip=p*x+a(j);j=j+1;endy=p;end②计算在点23的值。计算结果如下:当23x时86652xf。①Gauss法计算程序和结果:程序:A(1,:)=[31,-13,0,0,0,-10,0,0,0];A(2,:)=[-13,35,-9,0,-11,0,0,0,0];A(3,:)=[0,-9,31,-10,0,0,0,0,0];A(4,:)=[0,0,-10,79,-30,0,0,0,-9];A(5,:)=[0,0,0,-30,57,-7,0,-5,0];A(6,:)=[0,0,0,0,-7,47,-30,0,0];A(7,:)=[0,0,0,0,0,-30,41,0,0];A(8,:)=[0,0,0,0,-5,0,0,27,-2];A(9,:)=[0,0,0,-9,0,0,0,-2,29];B=[-15;27;-23;0;-20;12;-7;7;10];[a,b]=gauss(A,B);j=size(a,2);whilej=1k=j+1;s=b(j);whilek=9s=s-x(k)*a(j,k);k=k+1;endx(j)=s/a(j,j);j=j-1;enddisp(x)function[x,y]=gauss(a,b)num_i=size(a,1);j=1;whilej=(num_i-1)i=j+1;whilei=num_ir=a(i,j)/a(j,j);a(i,:)=a(i,:)-r*a(j,:);b(i,:)=b(i,:)-r*b(j,:);i=i+1;endj=j+1;endx=a;y=b;运行的结果为:Tx290.0201.0058.0154.0430.0221.0713.0345.0289.0。②列主元消去法计算程序和结果:A(1,:)=[31,-13,0,0,0,-10,0,0,0];A(2,:)=[-13,35,-9,0,-11,0,0,0,0];A(3,:)=[0,-9,31,-10,0,0,0,0,0];A(4,:)=[0,0,-10,79,-30,0,0,0,-9];A(5,:)=[0,0,0,-30,57,-7,0,-5,0];A(6,:)=[0,0,0,0,-7,47,-30,0,0];A(7,:)=[0,0,0,0,0,-30,41,0,0];A(8,:)=[0,0,0,0,-5,0,0,27,-2];A(9,:)=[0,0,0,-9,0,0,0,-2,29];B=[-15;27;-23;0;-20;12;-7;7;10];[a,b]=lzy(A,B);j=size(a,2);whilej=1k=j+1;s=b(j);whilek=9s=s-x(k)*a(j,k);k=k+1;endx(j)=s/a(j,j);j=j-1;enddisp(x)function[a1,b1]=lzy(a,b)[num_i,num_j]=size(a);ab=zeros(num_i,num_j+1);fork=1:num_jab(:,k)=a(:,k);endab(:,num_j+1)=b(:,1);j=1;whilejnum_j[max,max_i]=searmax(j,j,ab);i_nu=ab(j,:);ab(j,:)=ab(max_i,:);ab(max_i,:)=i_nu;m=j+1;whilem=num_iforn=j:num_j+1ab(m,n)=ab(m,n)-(ab(m,j)/max)*ab(j,n);endm=m+1;endj=j+1;enda1=zeros(num_i,num_j);fork=1:num_ia1(:,k)=ab(:,k);endb1=ab(:,num_i+1);function[b,c]=searmax(i,j,a)num_i=size(a,1);k=i;m=abs(a(k,j));c=k;whileknum_ik=k+1;ifm=abs(a(k,j))continueelsem=abs(a(k,j));c=k;endendb=a(c,j);运行的结果为:Tx345.0285.0171.0146.0308.0078.0767.0477.0237.0(1)LU分解的计算程序及结果:function[l,u]=lufz(a)[num_i,num_j]=size(a);l=eye(num_i);u=eye(num_i);forj=1:num_ju(1,j)=a(1,j);l(j,1)=a(j,1)/u(1,1);endi=2;whilei=num_ij=i;whilejnum_is=0;fork=1:i-1s=s+l(i,k)*u(k,j);endu(i,j)=a(i,j)-s;s=0;fork=1:i-1s=s+l(j+1,k)*u(k,i);endl(j+1,i)=(a(j+1,i)-s)/u(i,i);j=j+1;ends=0;fork=1:i-1s=s+l(i,k)*u(k,num_i);endu(i,num_i)=a(i,num_i)-s;i=i+1;end输入要求解的矩阵后运行的结果为:1092.0020.0014.0083.0119.00001025.0018.0112.000001655.0000001157.000001398.00001354.0001305.001419.01L390.27420.2413.26367.0513.0381.21562.0785.0308732.45578.350180.7602.44900452.0186.31461.75000277.1350.310259.28000194.41109548.29000100001331U(2)带列主元的LU分解计算程序及结果由于Matlab中自带带列主元的LU分解函数,故计算程序如下:A(1,:)=[31,-13,0,0,0,-10,0,0,0];A(2,:)=[-13,35,-9,0,-11,0,0,0,0];A(3,:)=[0,-9,31,-10,0,0,0,0,0];A(4,:)=[0,0,-10,79,-30,0,0,0,-9];A(5,:)=[0,0,0,-30,57,-7,0,-5,0];A(6,:)=[0,0,0,0,-7,47,-30,0,0];A(7,:)=[0,0,0,0,0,-30,41,0,0];A(8,:)=[0,0,0,0,-5,0,0,27,-2];A(9,:)=[0,0,0,-9,0,0,0,-2,29];[L,U,P]=lu(A);运行结果如下:1092.0020.0014.0083.0119.00001025.0018.0112.000001655.0000001157.000001398.00001354.0001305.001419.01L390.27420.2413.26367.0513.0381.21562.0785.0308732.45578.350180.7602.44900452.0186.31461.75000277.1350.310259.28000194.41109548.29000100001331U100000000010000000001000000000100000000010000000001000000000100000000010000000001P为单位阵。(3)求A的逆矩阵的计算程序及结果:A(1,:)=[31,-13,0,0,0,-10,0,0,0];A(2,:)=[-13,35,-9,0,-11,0,0,0,0];A(3,:)=[0,-9,31,-10,0,0,0,0,0];A(4,:)=[0,0,-10,79,-30,0,0,0,-9];A(5,:)=[0,0,0,-30,57,-7,0,-5,0];A(6,:)=[0,0,0,0,-7,47,-30,0,0];A(7,:)=[0,0,0,0,0,-30,41,0,0];A(8,:)=[0,0,0,0,-5,0,0,27,-2];A(9,:)=[0,0,0,-9,0,0,0,-2,29];[num_i,num_j]=size(A);A_n=eye(num_i);E=eye(num_i);[L,U]=lufz(A);fori=1:num_iy=hd(1,L,E(:,i));A_n(:,i)=hd(0,U,y);enddisp(A_n)function[l,u]=lufz(a)[num_i,num_j]=size(a);l=eye(num_i);u=eye(num_i);forj=1:num_ju(1,j)=a(1,j);l(j,1)=a(j,1)/u(1,1);endi=2;whilei=num_ij=i;whilejnum_is=0;fork=1:i-1s=s+l(i,k)*u(k,j);endu(i,j)=a(i,j)-s;s=0;fork=1:i-1s=s+l(j+1,k)*u(k,i);endl(j+1,i)=(a(j+1,i)-s)/u(i,i);j=j+1;ends=0;fork=1:i-1s=s+l(i,k)*u(k,num_i);endu(i,num_i)=a(i,num_i)-s;i=i+1;endfunctionx=hd(f,a,b)[num_i,num_j]=size(a);x=zeros(num_i,1);switchfcase0i=num_i-1;x(num_i)=b(num_i)/a(num_i,num_j);whilei=1s=0;fork=i+1:num_is=s+a(i,k)*x(k);endx(i)=(b(i)-s)/a(i,i);i=i-1;endcase1x(1)=b(1)/a(1,1);j=2;whilej=num_is=0;fork=1:j-1s=s+a(j,k)*x(k);endx(j)=(b(j)-s)/a(j,j);j=j+1;endotherwisedisp('error!请输入正确的参数!')end运行结果:0365.00034.00008.00011.00036.00058.00020.00006.00003.00033.00382.00010.00014.00048.00023.00008.00002.0000

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

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

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

×
保存成功