A.Kaw矩阵代数初步学习笔记 8. Gauss-Seidel Method
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授。
PDF格式学习笔记下载(Academia.edu)
第8章课程讲义下载(PDF)
Summary
- Algorithm
Given a general set of n equations and n unknowns {a11x1+a12x2+⋯+a1nxn=c1a21x1+a22x2+⋯+a2nxn=c2⋮an1x1+an2x2+⋯+annxn=cn If the diagonal elements are non-zero, each equation is rewritten for the corresponding unknown, that is, {x1=c1−a12x2−a13x3−⋯−a1nxna11x2=c2−a21x1−a23x3−⋯−a2nxna22⋮xn=cn−an1x1−an2x2−⋯−an,n−1xn−1a11 ⇒{x1=c1−n∑j=1,j≠1a1jxja11x2=c1−n∑j=1,j≠2a2jxja22⋮xn=cn−n∑j=1,j≠nanjxjann Hence for any row i, xi=ci−n∑j=1,j≠iaijxjaii where i=1, 2, ⋯, n. - Iteration
To find xi, we assume an initial guess for the xi and then use the rewritten equations to calculate the new estimates. We always use the most recent estimates to calculate the next estimates, xi. At the end of each iteration, we calculate the absolute relative approximate error for each xi as εi=|xnewi−xoldixnewi| where xnewi is the recently obtained value of xi, and xoldi is the previous value of xi. When the absolute relative approximate error for each xi is less than the pre-specified tolerance, the iterations are stopped. - Convergent
The coefficient matrix [A] in [A][X]=[B] must be diagonally dominant, that is, {|aii|≥n∑j=1,j≠iaijfor all i|aii|>n∑j=1,j≠iaijfor at least one i - An example
Suppose the following system of equations {12x1+3x2−5x3=1x1+5x2+3x3=283x1+7x2+13x3=76 Use [x1x2x3]=[101] as the initial guess and conduct two iterations.- Diagonally dominant test: {|a11|=12>|a12|+|a13|=3+5=8|a22|=5>|a21|+|a23|=1+3=4|a33|=13>|a31|+|a32|=3+7=10 Hence the solution should converge using Gauss Seidel method.
- Rewriting the equations: {x1=1−3x2+5x312x2=28−x1−3x35x3=76−3x1−7x213 And the initial value is [x1x2x3]=[101]
- Iteration 1: {x1=1−3×0+5×112=0.5x2=28−0.5−3×15=4.9x3=76−3×0.5−7×4.913=3.0923 Notice that the second and the third equations above, x1 and x2 are updated immediately. And the absolute relative approximate error is {ε1=|0.5−1|0.5=1ε2=|4.9−0|4.9=1ε3=|3.0923−1|3.0923=0.67662
- Iteration 2: {x1=1−3×4.9+5×3.092312=0.14679x2=28−0.14679−3×3.09235=3.7153x3=76−3×0.14679−7×3.715313=3.8118 And the absolute relative approximate error is {ε1=|0.14679−0.5|0.14679=2.4ε2=|3.7153−4.9|3.7153=0.31889ε3=|3.8118−3.0923|3.8118=0.18874
- Final result:
After 6 iterations, we have the solution [x1x2x3]=[0.999193.00014.0001] which is very close to the exact solution [x1x2x3]=[134]
- R code:
Some comments:- In the second function PrepA, we use the elementary row operation Ri+mRj if the diagonal element in Ri equals to zero.
- In the third function IterSolve, we use ε=∑|xnew−xold| instead of the absolute relative approximate error.
- x0 is a vector in the main function, which is the initial guess of the system. And eps is the tolerance of the error, which can be smaller or bigger in different cases. The last parameter is maxit is the number of iterations, it does not need to be too much in most cases.
- Using this code to calculate the previous example:
A = matrix(c(12, 1, 3, 3, 5, 7, -5, 3, 13), ncol = 3) b = matrix(c(1, 28, 76), ncol = 1) IterSolve(A, b, c(1, 0, 1))$x # Result # Converged after 11 iterations # [1] 1 3 4
Selected Problems
1. Given the system of equations {3x1+7x2+13x3=76x1+5x2+3x3=2812x1+3x2−5x3=1 find the solutions using the Gauss-Seidel method. Use [x1x2x3]=[101] as the initial guess.
Solution: Note that the coefficient matrix is not diagonal dominant: {|a11|=3<|a12|+|a13|=7+13=20|a33|=5<|a31|+|a32|=12+3=15 Hence it may diverge. Moreover, we can use our R code to test it:
A = matrix(c(3, 1, 2, 7, 5, 3, 13, 3, -5), ncol = 3) b = matrix(c(76, 28, 1), ncol = 1) IterSolve(A, b, c(1, 0, 1))$x # Result # [1] -2.496896e+172 1.261843e+171 -9.230477e+171 # Warning message: # In IterSolve(A, b, c(1, 0, 1)) : Maxit reached
2. Solve the following system equations using Gauss-Seidel method. {12x1+7x2+3x3=173x1+6x2+2x3=92x1+7x2−11x3=49 Choose the initial guess as [x1x2x3]=[135]
Solution:
Firstly, we test whether the coefficient matrix is diagonal dominant: {|a11|=12>|a12|+|a13|=10|a22|=6>|a21|+|a23|=5|a33|=11>|a31|+|a32|=9 which means it is diagonal dominant. Then we will conduct two iterations: I1={x1=17−7x2−3x312=17−7×3−3×512=−1.583333x2=9−3x1−2x36=9−3×(−1.583333)−2×56=0.625000x3=49−2x1−7x2−11=49−2×(−1.583333)−7×0.625000−11=−4.344697 I2={x1=17−7x2−3x312=17−7×0.625000−3×(−4.344697)12=2.138258x2=9−3x1−2x36=9−3×2.138258−2×(−4.344697)6=1.879104x3=49−2x1−7x2−11=49−2×2.138258−7×1.879104−11=−2.869978 Alternatively, we can use R code to solve it directly:
A = matrix(c(12, 3, 2, 7, 6, 7, 3, 2, -11), ncol = 3) b = matrix(c(17, 9, 49), ncol = 1) IterSolve(A, b, c(1, 3, 5), eps = 1e-8)$x # Result # Converged after 16 iterations # [1] 1 2 -3
That is, the solution is [x1x2x3]=[12−3]
3. Solve the following system equations using Gauss-Seidel method. {3x1+6x2+2x3=912x1+7x2+3x3=172x1+7x2−11x3=49 Choose the initial guess as [x1x2x3]=[1.12.1−2.9]
Solution:
We will use the R code to solve it directly:
A = matrix(c(3, 12, 2, 6, 7, 7, 2, 3, -11), ncol = 3) b = matrix(c(9, 17, 49), ncol = 1) IterSolve(A, b, c(1, 0, 1), eps = 1e-8)$x # Result # Error in IterSolve(A, b, c(1, 3, 5)) : The algorithm diverges
Recall the R function, the result is divergent when the solution in the iterations goes to infinity. Moreover, we can read off its non-convergent according to it is not diagonal dominant since {|a11|=3<|a12|+|a13|=8|a22|=7<|a21|+|a23|=15
作者:赵胤
出处:http://www.cnblogs.com/zhaoyin/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!