大连理工大学矩阵与数值分析大作业.pdf
《大连理工大学矩阵与数值分析大作业.pdf》由会员分享,可在线阅读,更多相关《大连理工大学矩阵与数值分析大作业.pdf(45页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、矩阵与数值分析上机实习矩阵与数值分析上机实习1.设,其精确值为.(1)编制按从大到小的顺序程序(2)编制按从小到大的顺序的通用程序(3)按两种顺序分别计算(4)通过本上机题,你明白了什么?从小到大,代码:%1-SN=%N=input(please input a number(N=2)if(N=2)102N=100S:7.4005e-001 clearplease input a number(N=2)104N=10000S:7.4990e-001 clearplease input a number(N=2)106N=1000000S:7.5000e-001从大到小代码:%1-SN=%eps
2、(single)N=input(please input a number(N=2)if(N=2)102N=100S:7.4005e-001 clearans=1.1921e-007please input a number(N=2)104N=10000S:7.4990e-001 clearans=1.1921e-007please input a number(N=2)106N=1000000S:7.5000e-001(4)计算的顺序不同,结果有可能不同。2.秦九韶算法。已知 n 次多项式,用秦九韶算法编写通用的程序计算函数在点的值,并计算在点的值.(提示:编写程序时,输入系数向量和点,输出
3、结果,多项式的次数可以通过向量的长度来判断)代码代码:A=input(请输入系数,由高次幂开始);n=input(请输入计算变量的值);len=length(A);val=zeros(len);val(1)=A(1);%printf(len=%c,len)for i=2:1:len%disp(val(i-1)%disp(n)val(i)=val(i-1)*n+val(i);end%printf(%f,val(len)val(len)结果结果:请输入系数,由高次幂开始7 3-5 11请输入计算变量的值23ans=851693.分别用 Gauss 消元法和列主元消去法编程求解方程组Ax=b,其中,
4、.高斯消去法代码:A=31-13 0 0 0-10 0 0 0 -13 35-9 0-11 0 0 0 0 0-9 31-10 0 0 0 0 0 0 0-10 79-30 0 0 0-9 0 0 0-30 57-7 0-5 0 0 0 0 0-7 47-30 0 0 0 0 0 0 0-30 41 0 0 0 0 0 0-5 0 0 27-2 0 0 0-9 0 0 0-2 29 ;B=-15,27,23,0,-20,12,-7,7,10;B=B.;%disp(A)%disp(B)C=A,B;n=size(A,1);ra=rank(A);rc=rank(C);x=zeros(1,n);if
5、ra=rc disp(no solution)elsefor i=1:1:(n-1)for j=i+1:1:n temp=(-C(j,i)/C(i,i);for k=i:1:(n+1)C(j,k)=C(i,k)*temp+C(j,k);endendendendfor i=n:-1:1if i=n disp(C(i,(n+1)disp(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)disp(C(i,i)end x(i)=(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)/C(i,i);enddisp(solution)disp(x)结果:-2
6、.2051e-0029.4726e-0011.0410e+0007.4481e-002-2.6617e-0012.0020e-001-2.4242e-0022.3844e-0013.8439e-001列主元消去法代码:%t4-max%A=31-13 0 0 0-10 0 0 0 -13 35-9 0-11 0 0 0 0 0-9 31-10 0 0 0 0 0 0 0-10 79-30 0 0 0-9 0 0 0-30 57-7 0-5 0 0 0 0 0-7 47-30 0 0 0 0 0 0 0-30 41 0 0 0 0 0 0-5 0 0 27-2 0 0 0-9 0 0 0-2 29
7、 ;B=-15,27,23,0,-20,12,-7,7,10;B=B.;%disp(A)%disp(B)C=A,B;n=size(A,1);ra=rank(A);rc=rank(C);x=zeros(1,n);if ra=rc disp(no solution)elsefor i=1:1:(n-1)%make max value in col in the row%max=0;inx=i;for j=i:nif(abs(C(j,i)max)max=abs(C(j,i);inx=j;endendif inx ifor j=i:(n+1)swap=C(i,j);C(i,j)=C(inx,j);C(
8、inx,j)=swap;endendfor j=i+1:1:n temp=(-C(j,i)/C(i,i);for k=i:1:(n+1)C(j,k)=C(i,k)*temp+C(j,k);endendendendfor i=n:-1:1if i=n disp(C(i,(n+1)disp(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)disp(C(i,i)end x(i)=(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)/C(i,i);enddisp(solution)disp(x)结果:solution-2.2051e-0029.4726
9、e-0011.0410e+0007.4481e-002-2.6617e-0012.0020e-001-2.4242e-0022.3844e-0013.8439e-0014.编程求解题 3 中矩阵的 LU 分解及列主元的 LU 分解(求出 L,U 和 P),并用 LU 分解的方法求 A 的逆矩阵及 A 的行列式LU 分解:代码:clearA=31-13 0 0 0-10 0 0 0 -13 35-9 0-11 0 0 0 0 0-9 31-10 0 0 0 0 0 0 0-10 79-30 0 0 0-9 0 0 0-30 57-7 0-5 0 0 0 0 0-7 47-30 0 0 0 0 0
10、 0 0-30 41 0 0 0 0 0 0-5 0 0 27-2 0 0 0-9 0 0 0-2 29 ;B=-15,27,23,0,-20,12,-7,7,10;B=B.;%disp(A)%disp(B)C=A,B;n=size(A,1);ra=rank(A);rc=rank(C);x=zeros(1,n);L=eye(n,n);if ra=rc disp(no solution)elsefor i=1:1:(n-1)for j=i+1:1:n temp=(-C(j,i)/C(i,i);for k=i:1:(n+1)C(j,k)=C(i,k)*temp+C(j,k);end L(j,i)=
11、-(1*temp);endendend for i=n:-1:1if i=n disp(C(i,(n+1)disp(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)disp(C(i,i)end x(i)=(C(i,(n+1)-sum(C(i,(i+1):n).*x(i+1):n)/C(i,i);end%disp(solution)%disp(x)disp(C)disp(C)disp(L)disp(L)结果:L1.0000e+0000000000-4.1935e-0011.0000e+000000000000-3.0459e-0011.0000e+00000000000
12、-3.5387e-0011.0000e+00000000000-3.9755e-0011.0000e+00000000000-1.5694e-0011.0000e+00000000000-6.5398e-0011.0000e+000000000-1.1210e-001-1.7545e-002-2.4619e-0021.0000e+0000000-1.1927e-001-8.3391e-002-1.4227e-002-1.9962e-002-9.2316e-0021.0000e+000U3.1000e+001-1.3000e+001000-1.0000e+00100002.9548e+001-9
13、.0000e+0000-1.1000e+001-4.1935e+000000002.8259e+001-1.0000e+001-3.3504e+000-1.2773e+0000000007.5461e+001-3.1186e+001-4.5200e-00100-9.0000e+00000004.4602e+001-7.1797e+0000-5.0000e+000-3.5780e+0000000-8.8818e-0164.5873e+001-3.0000e+001-7.8472e-001-5.6154e-00100000-3.5527e-0152.1381e+001-5.1319e-001-3.
14、6724e-00100000002.6413e+001-2.4200e+000000000002.7390e+001LUP 分解代码:结果L=1.0000e+00000000000-4.1935e-0011.0000e+00000000000-3.0459e-0011.0000e+00000000000-3.5387e-0011.0000e+00000000000-3.9755e-0011.0000e+00000000000-1.5694e-0011.0000e+00000000000-6.5398e-0011.0000e+000000000-1.1210e-001-1.7545e-002-2
15、.4619e-0021.0000e+0000000-1.1927e-001-8.3391e-002-1.4227e-002-1.9962e-002-9.2316e-0021.0000e+000U=3.1000e+001-1.3000e+001000-1.0000e+00100002.9548e+001-9.0000e+0000-1.1000e+001-4.1935e+000000002.8259e+001-1.0000e+001-3.3504e+000-1.2773e+0000000007.5461e+001-3.1186e+001-4.5200e-00100-9.0000e+00000004
16、.4602e+001-7.1797e+0000-5.0000e+000-3.5780e+000000004.5873e+001-3.0000e+001-7.8472e-001-5.6154e-0010000002.1381e+001-5.1319e-001-3.6724e-00100000002.6413e+001-2.4200e+000000000002.7390e+001P=100000000010000000001000000000100000000010000000001000000000100000000010000000001求 A 的逆矩阵:通过 LU 分解获得 LU,LUX=I
17、,LY=I,UX=Y 用高斯下去法解得 Y 和 Xfor i=1:n RA,RB,n,X=gaus(L,I(1:n,i);Y(1:n,i)=X;endfor i=1:n RA,RB,n,X=gaus(U,Y(1:n,i);A_(1:n,i)=X;endA_3.8952e-0021.5961e-0025.8083e-0033.6407e-0037.2823e-0031.2867e-0021.4396e-0031.2292e-0031.5863e-0023.7828e-0021.3083e-0026.5129e-0031.2133e-0027.1149e-0032.4090e-0032.1874e
18、-0034.8698e-0031.1613e-0023.8126e-0027.7386e-0036.9188e-0032.8373e-0031.4666e-0032.5028e-0038.1938e-0041.9539e-0036.4150e-0031.8128e-0021.0528e-0022.3922e-0032.3786e-0035.7899e-0034.5601e-0041.0874e-0033.5701e-0031.0089e-0022.4339e-0025.1100e-0034.7635e-0033.4595e-0031.2743e-0043.0388e-0049.9769e-00
19、42.8193e-0036.8017e-0033.0639e-0021.3312e-0039.6677e-0049.3244e-0052.2235e-0047.3002e-0042.0629e-0034.9768e-0034.6809e-0029.7403e-0047.0739e-0041.0381e-0042.4755e-0048.1276e-0042.2967e-0034.7736e-0031.0064e-0033.8169e-0023.3451e-0032.6145e-0046.2346e-0042.0469e-0035.7843e-0033.5966e-0038.1180e-0043.
20、3705e-0033.6510e-002|A|=|U|=6.1817e+0135.编制程序求解矩阵 A 的 cholesky 分解,并用程序求解方程组Ax=b,其中,%5-choleky%A=2 1-5 1 1-5 0 71.7585e-0029.7237e-0033.8776e-0033.2693e-0036.9837e-0034.1874e-0023.0639e-0021.3755e-0031.1095e-003 0 2 1-1 1 6-1-4 ;B=13,-9,6,0;%disp(A)dim=size(A,1);L=zeros(dim,dim);y=x=zeros(dim,1);for
21、i=1:1:dimfor j=1:1:dimif i=(j+1)L(i,j)=(A(i,j)-sum(L(i,1:(j-1).*L(j,1:(j-1)/L(j,j);else L(j,j)=sqrt(A(j,j)-sum(L(j,1:j-1).*L(j,1:j-1);endendendL_T=L.;for i=1:1:dimif i=1 y(1)=B(1)/L(1,1);else y(i)=(B(i)-sum(L(i,1:(i-1).*y(1:(i-1)/L(i,i);endendfor i=dim:-1:1if i=dim x(i)=y(i)/L(i,i);else x(i)=(y(i)-s
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 大连理工大学 矩阵 数值 分析 作业
限制150内