1、 u(k,j)=A(k,j)-sum; A(k,j)=u(k,j); for i=1: l(i,i)=1; for i=(k+1): sum=sum+A(i,r)*u(r,k); l(i,k)=(A(i,k)-sum)/u(k,k); A(i,k)=l(i,k);end求A的列主元素三角分解:A=1 1 1 1 1;1 2 3 4 5;1 3 6 10 15;1 4 10 20 35;1 5 15 35 70;L,U=myfun(A)结果:L = 1.0000 0 0 0 0 1.0000 1.0000 0 0 0 1.0000 0.5000 1.0000 0 0 1.0000 0.7500
2、0.7500 1.0000 0 1.0000 0.2500 0.7500 -1.0000 1.0000U = 1.0000 1.0000 1.0000 1.0000 1.0000 0 4.0000 14.0000 34.0000 69.0000 0 0 -2.0000 -8.0000 -20.5000 0 0 0 -0.5000 -2.3750 0 0 0 0 -0.2500(2)求矩阵的逆矩阵A-1:inv(A)结果为:ans = 5 -10 10 -5 1 -10 30 -35 19 -4 10 -35 46 -27 6 -5 19 -27 17 -4 1 -4 6 -4 1(3)检验结果
3、:E=diag(1 1 1 1 1) AE2:程序:function d=myfun(a,b,c,d,n)for i=2: l(i)=a(i)/b(i-1); a(i)=l(i); u(i)=b(i)-c(i-1)*a(i); b(i)=u(i); y(i)=d(i)-a(i)*d(i-1); d(i)=y(i);x(n)=d(n)/b(n);d(n)=x(n);for i=(n-1):-1:1 x(i)=(d(i)-c(i)*d(i+1)/b(i); d(i)=x(i);求各段电流量程序:8a(i)=-2;b=2 5 5 5 5 5 5 5;c=-2 -2 -2 -2 -2 -2 -2;V=
4、220;R=27;d=V/R 0 0 0 0 0 0 0;n=8;I=myfun(a,b,c,d,n)运行程序得:I =8.1478 4.0737 2.0365 1.0175 0.5073 0.2506 0.1194 0.04773:(1)求矩阵A和向量b的matlab程序:function A b=myfun(n)for i=1:X(i)=1+0.1*i; A(i,j)=X(i)(j-1);b(i)=sum(A(i,:);求n=5时A1,b1及A1的2-条件数程序运行结果如下:n=5;A1,b1=myfun(n)A1 = 1.0000 1.1000 1.2100 1.3310 1.4641
5、1.0000 1.2000 1.4400 1.7280 2.0736 1.0000 1.3000 1.6900 2.1970 2.8561 1.0000 1.4000 1.9600 2.7440 3.8416 1.0000 1.5000 2.2500 3.3750 5.0625b1 =6.1051 7.4416 9.0431 10.9456 13.1875cond2=cond(A1,2)cond2 = 5.3615e+005求n=10时A2,b2及A2的2-条件数程序运行结果如下:n=10;A2,b2=myfun(n)A2 = 1.0000 1.1000 1.2100 1.3310 1.464
6、1 1.6105 1.7716 1.9487 2.1436 2.3579 1.0000 1.2000 1.4400 1.7280 2.0736 2.4883 2.9860 3.5832 4.2998 5.1598 1.0000 1.3000 1.6900 2.1970 2.8561 3.7129 4.8268 6.2749 8.1573 10.6045 1.0000 1.4000 1.9600 2.7440 3.8416 5.3782 7.5295 10.5414 14.7579 20.6610 1.0000 1.5000 2.2500 3.3750 5.0625 7.5938 11.3906
7、 17.0859 25.6289 38.4434 1.0000 1.6000 2.5600 4.0960 6.5536 10.4858 16.7772 26.8435 42.9497 68.7195 1.0000 1.7000 2.8900 4.9130 8.3521 14.1986 24.1376 41.0339 69.7576 118.5879 1.0000 1.8000 3.2400 5.8320 10.4976 18.8957 34.0122 61.2220 110.1996 198.3593 1.0000 1.9000 3.6100 6.8590 13.0321 24.7610 47
8、.0459 89.3872 169.8356 322.6877 1.0000 2.0000 4.0000 8.0000 16.0000 32.0000 64.0000 128.0000 256.0000 512.0000b2 =1.0e+003 *0.0159 0.0260 0.0426 0.0698 0.1133 0.1816 0.2866 0.4451 0.6801 1.0230cond2=cond(A2,2) 8.6823e+011求n=20时A3,b3及A3的2-条件数程序运行结果如下:n=20;A3,b3=myfun(n)A3 = 1.0e+009 * Columns 1 throu
9、gh 10 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 11 through 20 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0002 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0003
10、 0.0005 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0003 0.0006 0.0013 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0003 0.0007 0.0015 0.0032 0.0000 0.0000 0.0000 0.0001 0.0001 0.0003 0.0006 0.0014 0.0032 0.0075 0.0000 0.0000 0.0000 0.0001 0.0002 0.0005 0.0012 0.0029 0.0070 0.0167 0.0000 0.000
11、0 0.0001 0.0001 0.0004 0.0009 0.0023 0.0058 0.0146 0.0364 0.0000 0.0000 0.0001 0.0002 0.0006 0.0017 0.0044 0.0113 0.0295 0.0766 0.0000 0.0001 0.0002 0.0004 0.0011 0.0030 0.0080 0.0215 0.0581 0.1570 0.0000 0.0001 0.0002 0.0007 0.0018 0.0051 0.0143 0.0400 0.1119 0.3133 0.0000 0.0001 0.0004 0.0010 0.00
12、30 0.0086 0.0250 0.0726 0.2105 0.6103 0.0001 0.0002 0.0005 0.0016 0.0048 0.0143 0.0430 0.1291 0.3874 1.1623b3 =0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0002 0.0004 0.00100.0025 0.0059 0.0132 0.0287 0.0606 0.1246 0.2494 0.4874 0.9316 1.7434cond2=cond(A3,2) 3.2395e+022由上述运行结果可知:它们是病态的,而且随着n的
13、增大,矩阵的病态变得严重。当n=5时:x1=A1b1x1 = 1.00001.0000当n=10时:x2=A2b2x2 = 1.0001 0.9999当n=20时:x3=A3b3x3 = 1.0e+005 * 0.0203 -0.1756 0.7034 -1.7228 2.8742 -3.4342 2.9927 -1.8765 0.7820 -0.1396 -0.0720 0.0745 -0.0350 0.0108 -0.0023 0.0003 -0.0000 0.0000由运行结果可见:x1与精确解吻合,x2与精确解稍有差异,x3与精确解差别很大。可见随着n的增大,矩阵病态越来越严重。A2(
14、2,2)=A(2,2)+1e-8;A2(10,10)=A(10,10)+1e-8;x=A2b2x = 1.0137 0.9197 1.2089 0.6844 1.3053 0.8039 1.0837 0.9771 1.0036 0.9997比较可见,系数矩阵出现微小变动,导致解出现较大变化。说明n=10时,系数矩阵是病态的。4:(1)A=10 7 8 7;7 5 6 5;8 6 10 9;7 5 9 10;b=32 23 33 31;det(A) 1cond(A) 2.9841e+003eig(A) 0.0102 0.8431 3.8581 30.2887A1=10 7.2 8.1 6.9;7
15、.08 5.07 6.02 5;8.2 5.89 9.96 9.01;6.98 5.04 8.97 9.98;x=1 1 1 1x1=A1b 0.0077 2.3117 1.0211 1.0157dx=x1-xdx = -0.9923 1.3117 0.0211 0.0157dA=A1-AdA = 0 0.2000 0.1000 -0.1000 0.0800 0.0700 0.0200 0 0.2000 -0.1100 -0.0400 0.0100 -0.0200 0.0400 -0.0300 -0.0200根据式(2-39)知:当dA充分小,使得|A-1|*|A|dA=0.5*1e-4*ra
16、nd(4);A1=A+dA 10.0000 7.0000 8.0000 7.0000 7.0000 5.0000 6.0000 5.0000 8.0000 6.0000 10.0000 9.0000 7.0000 5.0000 9.0000 10.0000 0.9996 1.0007 0.9998dx=x-x1 1.0e-003 * 0.4360 -0.6743 0.1508 -0.0952norm(dx) 8.2256e-004 4.1128e-004 0.0122 0.0121由计算结果可知dA充分小,使得|A-1|*|A|=0.01211时,有:第三章(1)用jacobi迭代法: 编写j
17、acobi迭代法的m文件如下:function x1=jacobi(A,b,n,x,e,N)N if(j=i) continue; sum=sum+A(i,j)*x(j); x1(i)=(b(i)-sum)/A(i,i); if(norm(x1-x)e) break; x=x1;保存为jacobi.m文件。然后在matlab命令窗口中编程计算: A=10 1 2 3 4;1 9 -1 2 -3;2 -1 7 3 -5;3 2 3 12 -1;4 -3 -5 -1 15; b=12 -27 14 -17 12; x=0 0 0 0 0; x1=jacobi(A,b,5,x,1e-6,15)1.0
18、318 -2.0297 2.9451 -1.9920 0.9620即用jacobi迭代法求得解为:1.0318 -2.0297 2.9451 -1.9920 0.9620(2)用Gauss-Seidel迭代法解:编写Gauss-Seidel迭代法的m文件如下:function x1=gausdel(A,b,n,x,e,N) for j=2: sum=sum+A(1,j)*x(j); x1(1)=(b(1)-sum)/A(1,1); for i=2:n-1 f=0;g=0;i-1 f=f+A(i,j)*x1(j); for j=i+1: g=g+A(i,j)*x(j); x1(i)=(b(i)-
19、f-g)/A(i,i); sum=sum+A(n,j)*x1(j); x1(n)=(b(n)-sum)/A(n,n);保存为gausdel.m文件。 x2=gausdel(A,b,5,x,1e-6,15)1.0055 -2.0046 2.9921 -1.9993 0.9950即用Gauss-Seidel迭代法求得解为:1.0055 -2.0046 2.9921 -1.9993 0.9950(3)用共轭梯度法解:编写共轭梯度法的m文件如下:function x=gonger(A,b,x0,e)r0=b-(A*x0)d0=r0;z0=r0*d0/(d0*(A*d0x1=x0+z0*d0;r1=b-(A*x1while(norm(r1
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1