第2讲 线性方程组的数值方法.ppt

上传人:精*** 文档编号:1135654 上传时间:2024-09-26 格式:PPT 页数:28 大小:617.50KB
下载 相关 举报
第2讲 线性方程组的数值方法.ppt_第1页
第1页 / 共28页
第2讲 线性方程组的数值方法.ppt_第2页
第2页 / 共28页
第2讲 线性方程组的数值方法.ppt_第3页
第3页 / 共28页
第2讲 线性方程组的数值方法.ppt_第4页
第4页 / 共28页
第2讲 线性方程组的数值方法.ppt_第5页
第5页 / 共28页
点击查看更多>>
资源描述

1、线性代数方程组的数值解法线性代数方程组的数值解法(1)Gauss 消去法消去法(Demos in Matlab:airfoil in 2D)线性代数方程组的数值解法线性代数方程组的数值解法直接法直接法:Gauss 消去法,SuperLU迭代法迭代法:定常迭代(Jacobi,GS,SOR,SSOR)Krylov 子空间方法(CG,MINRES,GMRES,QMR,BiCGStab)The Landscape of Ax=b SolversDirectA=LUIterativey=AyNon-symmetricSymmetricpositivedefiniteMore RobustLess Sto

2、rageMore RobustMore General刘徽刘徽(约220-280)Gaussian elimination,which first appeared in the text Nine Chapters on the Mathematical Art written in 200 BC,was used by Gauss in his work which studied the orbit of the asteroid Pallas.Using observations of Pallas taken between 1803 and 1809,Gauss obtained

3、a system of six linear equations in six unknowns.Gauss gave a systematic method for solving such equations which is precisely Gaussian elimination on the coefficient matrix.(The MacTutor History of Mathematics,http:/www-history.mcs.st-andrews.ac.uk/history/index.html)Gauss(1777-1855)今有上禾三秉,中禾二秉,下禾今有

4、上禾三秉,中禾二秉,下禾一秉,实三十九斗;上禾二秉,一秉,实三十九斗;上禾二秉,中禾三秉,下禾一秉,实三十四中禾三秉,下禾一秉,实三十四斗;上禾一秉,中禾二秉,下禾斗;上禾一秉,中禾二秉,下禾三秉,实二十六斗。问上、中、三秉,实二十六斗。问上、中、下禾实一秉各几何?答曰:上禾下禾实一秉各几何?答曰:上禾一秉九斗四分斗之一。中禾一秉一秉九斗四分斗之一。中禾一秉四斗四分斗之一。下禾一秉二斗四斗四分斗之一。下禾一秉二斗四分斗之三。四分斗之三。-九章算九章算术术一个两千年前的例子vBasic idea:Add multiples of each row to later rows to make A

5、upper triangular一个两千年前的例子(2)Solving linear equations is not trivial.Forsythe(1952)After k=1 After k=2 After k=3 After k=n-1Gauss 消去过程图示用矩阵变换表达消去过程利用Gauss 变换矩阵的性质:用矩阵变换表达消去过程(2)单位下三角形用Gauss消去法求解 A x=b -LU分解 A=L U (cost=2/3 n3 flops)-求解 L y=b (cost=n2 flops)-求解 U x=y (cost=n2 flops)v版本一for k=1 to n-1

6、for i=k+1 to n m=A(i,k)/A(k,k)for j=k to n A(i,j)=A(i,j)-m*A(k,j)算法实现:Gauss Elimination Algorithmfor k=1 to n-1 对第对第k列列,消去对角线以下元消去对角线以下元素素 (通过每行加上第通过每行加上第k行的倍数行的倍数)for i=k+1 to n 对第对第k行以下的每行以下的每一一行行i for j=k to n 第第k行的倍数加到第行的倍数加到第 i 行行 A(i,j)=A(i,j)-(A(i,k)/A(k,k)*A(k,j)v版本二:在内循环中去掉常量 A(i,k)/A(k,k)的

7、计算v上一版本Gauss Elimination Algorithm(2)for k=1 to n-1 for i=k+1 to n m=A(i,k)/A(k,k)for j=k to n A(i,j)=A(i,j)-m*A(k,j)for k=1 to n-1 for i=k+1 to n m=A(i,k)/A(k,k)for j=k+1 to n A(i,j)=A(i,j)-m*A(k,j)v版本三:第k列对角线以下为0,无需计算v上一版本Gauss Elimination Algorithm(3)for k=1 to n-1 for i=k+1 to n m=A(i,k)/A(k,k)f

8、or j=k+1 to n A(i,j)=A(i,j)-m*A(k,j)for k=1 to n-1 for i=k+1 to n A(i,k)=A(i,k)/A(k,k)for j=k+1 to n A(i,j)=A(i,j)-A(i,k)*A(k,j)v版本四:将乘子 m 存储在对角线以下备用v上一版本for k=1 to n-1 for i=k+1 to n A(i,k)=A(i,k)/A(k,k)for j=k+1 to n A(i,j)=A(i,j)-A(i,k)*A(k,j)for k=1 to n-1 for i=k+1 to n A(i,k)=A(i,k)/A(k,k)for

9、i=k+1 to n for j=k+1 to n A(i,j)=A(i,j)-A(i,k)*A(k,j)v版本五:Split loopGauss Elimination Algorithm(4)v上一版本v版本六:用矩阵运算for k=1 to n-1 for i=k+1 to n A(i,k)=A(i,k)/A(k,k)for i=k+1 to n for j=k+1 to n A(i,j)=A(i,j)-A(i,k)*A(k,j)Gauss Elimination Algorithm(5)for k=1 to n-1 A(k+1:n,k)=A(k+1:n,k)/A(k,k)BLAS 1(

10、scale a vector)A(k+1:n,k+1:n)=A(k+1:n,k+1:n)-A(k+1:n,k)*A(k,k+1:n)BLAS 2(rank-1 update)What we havent told youl 选主元策略选主元策略(当主元A(k)(k,k)为0或很小时)l 向后误差分析向后误差分析l 并行技术并行技术l 块算法块算法 l Sparse LU,Band LUSparse LU,Band LUl 最新进展最新进展(F.Gustavson&S.Toledo,Recursive Algorithm)l 还可用于矩阵求逆还可用于矩阵求逆,求行列式求行列式,秩秩l 定理定理:

11、主元A(k)(k,k)不为0的充要条件是顺序主子矩阵非奇异l 定理定理:分解的存在性和唯一性 Matlab 中的相应函数Linpack 中对应的函数 invluC,Fortran,Matlab 代码sgea.fsgefa.ffunction x=lsolve(A,b)%x=lsolve(A,b)returns the solution to the equation Ax=b,%where A is an n-by-b matrix and b is a column vector of%length n(or a matrix with several such columns).%Gaus

12、sian elimination with partial pivotingn,n=size(A);for k=1:n-1%find index of largest element below diagonal in column kmax=k;for i=k+1:nif abs(A(i,k)abs(A(max,k)max=i;endend%swap with row kA(k max,:)=A(max k,:);b(k max)=b(max k);%zero out entries of A and b using pivot A(k,k)A(k+1:n,k)=A(k+1:n,k)/A(k

13、,k);b(k+1:n)=b(k+1:n)-A(k+1:n,k)*b(k);A(k+1:n,k+1:n)=A(k+1:n,k+1:n)-A(k+1:n,k)*A(k,k+1:n);%for i=k+1:n%alpha=A(i,k)/A(k,k);%b(i)=b(i)-alpha*b(k);%A(i,:)=A(i,:)-alpha*A(k,:);%end end%back substitutionx=zeros(size(b);for i=n:-1:1j=i+1:n;x(i)=(b(i)-A(i,j)*x(j)/A(i,i);end/*Computer Soft/c2-1.c Gauss Eli

14、mination*/#include#include#include#define TRUE 1/*aij:matrix element,a(i,j)n:order of matrix eps:machine epsilon det:determinant */void main()int i,j,_i,_r;static n=3;static float a_init1011=1,2,3,6,2,2,3,7,3,3,3,9;static double a1011;void gauss();/*static int _aini=1;*/printf(nComputer Soft/C2-1 Ga

15、uss Elimination nn);printf(Augmented matrixn);for(i=1;i=n;i+)for(j=1;j=n+1;j+)aij=a_initi-1j-1;printf(%13.5e,aij);printf(n);gauss(n,a);printf(Solutionn);printf(-n);printf(i x(i)n);printf(-n);for(i=1;i 0)eps=eps/2.0;ep1=eps*0.98+1;ep1=ep1-1;eps=eps*2;eps2=eps*2;printf(Machine epsilon=%g n,eps);det=1;

16、/*Initialization of determinant*/for(i=1;i=(n-1);i+)pv=i;for(j=i+1;j=n;j+)if(fabs(apvi)fabs(aji)pv=j;if(pv!=i)for(jc=1;jc=(n+1);jc+)tm=aijc;aijc=apvjc;apvjc=tm;det=-det;if(aii=0)/*Singular matrix*/printf(Matrix is singular.n);exit(0);for(jr=i+1;jr=n;jr+)/*Elimination of below-diagonal.*/if(ajri!=0)r

17、=ajri/aii;for(kc=i+1;kc=(n+1);kc+)temp=ajrkc;ajrkc=ajrkc-r*aikc;if(fabs(ajrkc)eps2*temp)ajrkc=0.0;/*If the result of subtraction is smaller than*2 times machine epsilon times the original*value,it is set to zero.*/for(i=1;i=1;nv-)va=anvn+1;for(k=nv+1;k=n;k+)va=va-anvk*akn+1;anvn+1=va/anvnv;printf(De

18、terminant=%g n,det);return;CC PAGE 220-223:NUMERICAL MATHEMATICS AND COMPUTING,CHENEY/KINCAID,1985CC FILE:GAUSS.FORCC GAUSSIAN ELIMINATION WITH SCALED PARTIAL PIVOTING(GAUSS,SOLVE,TSTGAUS)C DIMENSION A1(4,4),A2(4,4),A3(4,4),B1(4),B2(4),B3(4)DIMENSION L(4),S(4),X(4)DATA(A1(I,J),I=1,4),J=1,4)/3.,1.,6.

19、,0.,4.,5.,3.,0.,3.,-1.,7.,A 0.,0.,0.,0.,0./DATA(B1(I),I=1,4)/16.,-12.,102.,0./DATA(A2(I,J),I=1,4),J=1,4)/3.,2.,1.,0.,2.,-3.,4.,0.,-5.,1.,-1.,A 0.,0.,0.,0.,0./DATA(B2(I),I=1,4)/4.,8.,3.,0./DATA(A3(I,J),I=1,4),J=1,4)/1.,3.,5.,4.,-1.,2.,8.,2.,2.,1.,6.,A 5.,1.,4.,3.,3./DATA(B3(I),I=1,4)/5.,8.,10.,12./C

20、CALL TSTGAUS(3,A1,4,L,S,B1,X)CALL TSTGAUS(3,A2,4,L,S,B2,X)CALL TSTGAUS(4,A3,4,L,S,B3,X)END SUBROUTINE TSTGAUS(N,A,IA,L,S,B,X)DIMENSION A(IA,N),B(N),X(N),S(N),L(N)PRINT 10,(A(I,J),J=1,N),I=1,N)PRINT 10,(B(I),I=1,N)CALL GAUSS(N,A,IA,L,S)CALL SOLVE(N,A,IA,L,B,X)PRINT 10,(X(I),I=1,N)RETURN10 FORMAT(5X,3

21、(F10.5,2X)END SUBROUTINE GAUSS(N,A,IA,L,S)DIMENSION A(IA,N),L(N),S(N)DO 3 I=1,N L(I)=I SMAX=0.0 DO 2 J=1,N SMAX=AMAX1(SMAX,ABS(A(I,J)2 CONTINUE S(I)=SMAX 3 CONTINUE DO 7 K=1,N-1 RMAX=0.0 DO 4 I=K,N R=ABS(A(L(I),K)/S(L(I)IF(R.LE.RMAX)GO TO 4 J=I RMAX=R 4 CONTINUE LK=L(J)L(J)=L(K)L(K)=LK DO 6 I=K+1,N

22、XMULT=A(L(I),K)/A(LK,K)DO 5 J=K+1,N A(L(I),J)=A(L(I),J)-XMULT*A(LK,J)5 CONTINUE A(L(I),K)=XMULT 6 CONTINUE 7 CONTINUE RETURN END SUBROUTINE SOLVE(N,A,IA,L,B,X)DIMENSION A(IA,N),L(N),B(N),X(N)DO 3 K=1,N-1 DO 2 I=K+1,N B(L(I)=B(L(I)-A(L(I),K)*B(L(K)2 CONTINUE 3 CONTINUE X(N)=B(L(N)/A(L(N),N)DO 5 I=N-1,1,-1 SUM=B(L(I)DO 4 J=I+1,N SUM=SUM-A(L(I),J)*X(J)4 CONTINUE X(I)=SUM/A(L(I),I)5 CONTINUE RETURN END

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 教学课件 > 其他教案课件

版权声明:以上文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如有侵权请立即联系:2622162128@qq.com ,我们立即下架或删除。

Copyright© 2022-2024 www.wodocx.com ,All Rights Reserved |陕ICP备19002583号-1 

陕公网安备 61072602000132号     违法和不良信息举报:0916-4228922