当前位置:网站首页>Using MATLAB to realize: Jacobi, Gauss Seidel iteration
Using MATLAB to realize: Jacobi, Gauss Seidel iteration
2022-07-02 07:23:00 【Drizzle】
Use Matlab Realization :Jacobi、Gauss-Seidel iteration
Example
Equations { 5 x 1 + 2 x 2 + x 3 = − 12 − x 1 + 4 x 2 + 2 x 3 = 20 2 x 1 − 3 x 2 + 10 x 3 = 3 \begin{cases} 5x_1 + 2x_2 + x_3 = -12\\ -x_1 + 4x_2 + 2x_3 = 20\\ 2x_1 - 3x_2 + 10x_3 = 3\\ \end{cases} ⎩⎪⎨⎪⎧5x1+2x2+x3=−12−x1+4x2+2x3=202x1−3x2+10x3=3 solve , When m a x ∣ x i ( k + 1 ) − x i ( k ) ∣ ≤ 1 0 − 5 max|x_i^{(k + 1)} - x_i^{(k)}| \leq 10^{-5} max∣xi(k+1)−xi(k)∣≤10−5 When iteration ends .
The following solution process , Superscript indicates the number of iterations , Subscript indicates serial number .
Jacobi iteration
Defining variables :
D = d i a g ( a 11 , a 22 , . . . , a n n ) , L = [ 0 − a 21 0 . . . − a i 1 . . . − a i , i − 1 0 . . . − a n 1 . . . − a n , i − 1 . . . − a n , n − 1 0 ] , U = [ 0 − a 12 . . . − a 1 , i . . . − a 1 , n . . . 0 − a i − 1 , i . . . − a i − 1 , n . . . 0 − a n − 1 , n . . . 0 ] D = diag(a_{11}, a_{22}, ..., a_{nn}),\\ L = \left[\begin{array}{cccccc} 0\\ -a_{21} & 0\\ ...\\ -a_{i1} & ... & -a_{i,i-1} & 0\\ ...\\ -a_{n1} & ... & -a_{n,i-1} & ... & -a_{n,n-1} & 0\\ \end{array}\right],\\ U = \left[\begin{array}{cccccc} 0 &-a_{12} & ... & -a_{1,i} & ... & -a_{1,n}\\ ...\\ & & 0 & -a_{i-1,i} & ... & -a_{i-1,n}\\ ...\\ & & & & 0 & -a_{n-1,n}\\ ...\\ & & & & & 0\\ \end{array}\right] D=diag(a11,a22,...,ann),L=⎣⎢⎢⎢⎢⎢⎢⎡0−a21...−ai1...−an10......−ai,i−1−an,i−10...−an,n−10⎦⎥⎥⎥⎥⎥⎥⎤,U=⎣⎢⎢⎢⎢⎢⎢⎢⎢⎡0.........−a12...0−a1,i−ai−1,i......0−a1,n−ai−1,n−an−1,n0⎦⎥⎥⎥⎥⎥⎥⎥⎥⎤
Its matrix iteration form is :
x ( k + 1 ) = B J ⋅ x ( k ) + f J B J = D − 1 ⋅ ( L + U ) , f J = D − 1 ⋅ b x^{(k+1)} = B_J \cdot x^{(k)} + f_J\\ B_J = D^{-1} \cdot (L + U), \quad f_J = D^{-1} \cdot b x(k+1)=BJ⋅x(k)+fJBJ=D−1⋅(L+U),fJ=D−1⋅b.
Write the component form :
{ x 1 ( k + 1 ) = 1 5 ( − 12 − 2 x 2 ( k ) − x 3 ( k ) ) x 2 ( k + 1 ) = 1 4 ( 20 + x 1 ( k ) − 2 x 3 ( k ) ) x 3 ( k + 1 ) = 1 10 ( 3 − 2 x 1 ( k ) + 3 x 2 ( k ) ) \begin{cases} x_1^{(k + 1)} = \frac15 (-12 - 2 x_2^{(k)} - x_3^{(k)} )\\ x_2^{(k + 1)} = \frac14 (20 + x_1^{(k)} - 2x_3^{(k)} )\\ x_3^{(k + 1)} = \frac1{10} (3 - 2 x_1^{(k)} + 3x_2^{(k)} )\\ \end{cases} ⎩⎪⎨⎪⎧x1(k+1)=51(−12−2x2(k)−x3(k))x2(k+1)=41(20+x1(k)−2x3(k))x3(k+1)=101(3−2x1(k)+3x2(k))
In matrix form :
[ x 1 ( k + 1 ) x 2 ( k + 1 ) x 3 ( k + 1 ) ] = [ 0 − 0.4 − 0.2 0.25 0 − 0.5 − 0.2 0.3 0 ] ⋅ [ x 1 ( k ) x 2 ( k ) x 3 ( k ) ] + [ − 2.4 5 0.3 ] \left[\begin{array}{c} x_1^{(k + 1)}\\ x_2^{(k + 1)}\\ x_3^{(k + 1)}\\ \end{array}\right] = \left[\begin{array}{cccc} 0 & -0.4 & -0.2\\ 0.25 & 0 & -0.5\\ -0.2 & 0.3 & 0\\ \end{array}\right] \cdot \left[\begin{array}{c} x_1^{(k)}\\ x_2^{(k)}\\ x_3^{(k)}\\ \end{array}\right] + \left[\begin{array}{c} -2.4\\ 5\\ 0.3\\ \end{array}\right] ⎣⎢⎡x1(k+1)x2(k+1)x3(k+1)⎦⎥⎤=⎣⎡00.25−0.2−0.400.3−0.2−0.50⎦⎤⋅⎣⎢⎡x1(k)x2(k)x3(k)⎦⎥⎤+⎣⎡−2.450.3⎦⎤
Take the initial vector : x 0 = ( 0 , 0 , 0 ) T x^0 = (0, 0, 0)^T x0=(0,0,0)T, Iterate according to the above formula in turn . Use Matlab Program to solve .
a=[0,-0.4,-0.2;0.25,0,-0.5;-0.2,0.3,0];
b = [-2.4;5;0.3];
x = [0;0;0];
xx = a * x + b;
i = 0;
while norm(x - xx, inf) >= 1e-5
x = xx;
xx = a * x + b;
i = i +1;
end
Above code , Final x = x i , x x = x ( i + 1 ) x = x^{i}, xx = x^{(i + 1)} x=xi,xx=x(i+1) , Final iteration order i + 1 i + 1 i+1 Time , If you need to see a longer decimal position , You can use the following Matlab Code , Said the use of 15 Bit floating-point or fixed-point number .
format long g
The running result is :
That is, the exact solution is x = ( − 4 , 3 , 2 ) T x = (-4,3,2)^T x=(−4,3,2)T .
Gauss-Seidel iteration
Its matrix iteration form is :
x ( k + 1 ) = B G ⋅ x ( k ) + f G B G = ( D − L ) − 1 ⋅ U , f G = ( D − L ) − 1 ⋅ b x^{(k+1)} = B_G \cdot x^{(k)} + f_G\\ B_G = (D - L) ^{-1} \cdot U, \quad f_G = (D - L) ^{-1} \cdot b x(k+1)=BG⋅x(k)+fGBG=(D−L)−1⋅U,fG=(D−L)−1⋅b
Use Matlab Programming to solve :
d = [5,0,0;0,4,0;0,0,10];
l = [0,0,0;1,0,0;-2,3,0];
u = [0,-2,-1;0,0,-2;0,0,0];
b = [-12;20;3];
t = inv(d - l);
bg = t * u;
fg = t * b;
x = [0;0;0];
xx = [-2.4;4.4;2.1];
i = 0;
while norm(x - xx, inf) >= 1e-5
x = xx;
xx = bg * x + fg;
i = i +1;
end
The running result is :
The exact solution is also obtained as x = ( − 4 , 3 , 2 ) T x = (-4,3,2)^T x=(−4,3,2)T .
边栏推荐
- Cognitive science popularization of middle-aged people
- SSM garbage classification management system
- Oracle EBS数据库监控-Zabbix+zabbix-agent2+orabbix
- Delete the contents under the specified folder in PHP
- Oracle RMAN semi automatic recovery script restore phase
- 2021-07-17c /cad secondary development creation circle (5)
- 【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
- ORACLE 11G SYSAUX表空间满处理及move和shrink区别
- ORACLE APEX 21.2安裝及一鍵部署
- ARP攻击
猜你喜欢

读《敏捷整洁之道:回归本源》后感

Implementation of purchase, sales and inventory system with ssm+mysql

DNS attack details

MySQL无order by的排序规则因素

MapReduce与YARN原理解析

Sqli-labs customs clearance (less1)

离线数仓和bi开发的实践和思考

Cognitive science popularization of middle-aged people

SSM second hand trading website

SSM garbage classification management system
随机推荐
Transform the tree structure into array in PHP (flatten the tree structure and keep the sorting of upper and lower levels)
parser. parse_ Args boolean type resolves false to true
MapReduce与YARN原理解析
架构设计三原则
Yaml file of ingress controller 0.47.0
【BERT,GPT+KG调研】Pretrain model融合knowledge的论文集锦
How to efficiently develop a wechat applet
The first quickapp demo
SSM学生成绩信息管理系统
Alpha Beta Pruning in Adversarial Search
Oracle 11g sysaux table space full processing and the difference between move and shrink
Oracle apex 21.2 installation and one click deployment
Two table Association of pyspark in idea2020 (field names are the same)
Error in running test pyspark in idea2020
数仓模型事实表模型设计
【信息检索导论】第七章搜索系统中的评分计算
【信息检索导论】第六章 词项权重及向量空间模型
[paper introduction] r-drop: regulated dropout for neural networks
CRP implementation methodology
Pyspark build temporary report error