当前位置:网站首页>Implementation of iterative method for linear equations
Implementation of iterative method for linear equations
2022-06-30 10:23:00 【Yangxiangrui】
The theory part has been introduced in detail before
Iterative solution theory of linear equations
Here are 3 × 3 3 \times 3 3×3 Matrix as the iterative solution of coefficient matrix python Realization
1. Jacobian iteration
The solution equation is A x ⃗ = b ⃗ A\vec{x} = \vec{b} Ax=b, The specific values are as follows
A = [ 5 2 1 − 1 4 2 2 − 3 10 ] b ⃗ = [ − 12 20 3 ] A = \left[\begin{matrix} 5& 2& 1\\ -1& 4& 2\\ 2& -3& 10 \end{matrix}\right] \quad \vec{b} = \left[\begin{matrix} -12\\ 20\\ 3 \end{matrix}\right] A=⎣⎡5−1224−31210⎦⎤b=⎣⎡−12203⎦⎤
The symbols in the code are the same as those in the derivation , The specific meaning is as follows
{ x ⃗ k + 1 = B x ⃗ k + f ⃗ B = D − 1 ( L + U ) f ⃗ = D − 1 b ⃗ \begin{cases} \vec{x}_{k + 1} = B\vec{x}_k + \vec{f}\\ B = D^{-1}(L + U)\\ \vec{f} = D^{-1}\vec{b} \end{cases} ⎩⎪⎨⎪⎧xk+1=Bxk+fB=D−1(L+U)f=D−1b
import numpy as np
ERROR = 0.001
A = np.array([[5, 2, 1],
[-1, 4, 2],
[2, -3, 10]])
b = np.array([[-12], [20], [3]])
m, n = np.shape(A)
D = np.mat(np.zeros((m, n)))
L = np.mat(np.zeros((m, n)))
U = np.mat(np.zeros((m, n)))
for i in range(m):
for j in range(n):
if i == j:
D[i, j] = A[i, j]
if i < j:
L[i, j] = -A[i, j]
if i > j:
U[i, j] = -A[i, j]
x0 = np.array(np.zeros((m, 1)))
xk = np.array(np.zeros((m, 1)))
B = np.dot(D.I, (L + U))
f = np.dot(D.I, b)
print("B:", B)
print("f:", f)
iter_time = 1
xk = np.dot(B, x0) + f
while(np.linalg.norm((xk - x0)) >= ERROR):
iter_time += 1
print(xk)
x0 = xk
xk = np.dot(B, xk) + f
print("The iteration time: %d" %iter_time)
In the running results, you can see that the total number of iterations 14 Time , Intermediate process output is omitted , Only the last two vectors are displayed x ⃗ \vec{x} x Iterative results of
[[-4.00122406]
[ 3.00000859]
[ 2.00098719]]
[[-4.00020088]
[ 2.99920039]
[ 2.00024739]]
The iteration time: 14
2. G-S Iterative method
The solution equation is A x ⃗ = b ⃗ A\vec{x} = \vec{b} Ax=b, The specific values are as follows
A = [ 5 2 1 − 1 4 2 2 − 3 10 ] b ⃗ = [ − 12 20 3 ] A = \left[\begin{matrix} 5& 2& 1\\ -1& 4& 2\\ 2& -3& 10 \end{matrix}\right] \quad \vec{b} = \left[\begin{matrix} -12\\ 20\\ 3 \end{matrix}\right] A=⎣⎡5−1224−31210⎦⎤b=⎣⎡−12203⎦⎤
The symbols in the code are the same as those in the derivation , The specific meaning is as follows
{ x ⃗ k + 1 = B x ⃗ k + f ⃗ B = ( D − L ) − 1 U f ⃗ = ( D − L ) − 1 b ⃗ \begin{cases} \vec{x}_{k + 1} = B\vec{x}_k + \vec{f}\\ B = (D - L)^{-1}U\\ \vec{f} = (D - L)^{-1}\vec{b} \end{cases} ⎩⎪⎨⎪⎧xk+1=Bxk+fB=(D−L)−1Uf=(D−L)−1b
import numpy as np
ERROR = 0.001
A = np.array([[5, 2, 1],
[-1, 4, 2],
[2, -3, 10]])
b = np.array([[-12], [20], [3]])
m, n = np.shape(A)
D = np.mat(np.zeros((m, n)))
L = np.mat(np.zeros((m, n)))
U = np.mat(np.zeros((m, n)))
for i in range(m):
for j in range(n):
if i == j:
D[i, j] = A[i, j]
if i < j:
L[i, j] = -A[i, j]
if i > j:
U[i, j] = -A[i, j]
x0 = np.array(np.zeros((m, 1)))
xk = np.array(np.zeros((m, 1)))
B = np.dot((D - L).I, U)
f = np.dot((D - L).I, b)
print("B:", B)
print("f:", f)
iter_time = 1
xk = np.dot(B, x0) + f
while(np.linalg.norm((xk - x0)) >= ERROR):
iter_time += 1
print(xk)
x0 = xk
xk = np.dot(B, xk) + f
print("The iteration time: %d" %iter_time)
In the running results, you can see that the total number of iterations 7 Time , It shows that the convergence speed of bijacobi iterative method is faster . Intermediate process output is omitted , Only the last two vectors are displayed x ⃗ \vec{x} x Iterative results of
[[-4.00004 ]
[ 3.00207406]
[ 1.99605188]]
[[-3.999996 ]
[ 2.99967489]
[ 2.00063022]]
The iteration time: 7
3. Successive over relaxation iterative method (SOR)
The solution equation is A x ⃗ = b ⃗ A\vec{x} = \vec{b} Ax=b, The specific values are as follows
A = [ 5 2 1 − 1 4 2 2 − 3 10 ] b ⃗ = [ − 12 20 3 ] A = \left[\begin{matrix} 5& 2& 1\\ -1& 4& 2\\ 2& -3& 10 \end{matrix}\right] \quad \vec{b} = \left[\begin{matrix} -12\\ 20\\ 3 \end{matrix}\right] A=⎣⎡5−1224−31210⎦⎤b=⎣⎡−12203⎦⎤
The symbols in the code are the same as those in the derivation , The specific meaning is as follows
{ x ⃗ k + 1 = B x ⃗ k + f ⃗ B = ( D − w L ) − 1 ( ( 1 − w ) D + w U ) f ⃗ = ( D − s L ) − 1 w b ⃗ \begin{cases} \vec{x}_{k + 1} = B\vec{x}_k + \vec{f}\\ B = (D - wL)^{-1}((1-w)D + wU)\\ \vec{f} = (D - sL)^{-1}w\vec{b} \end{cases} ⎩⎪⎨⎪⎧xk+1=Bxk+fB=(D−wL)−1((1−w)D+wU)f=(D−sL)−1wb
import numpy as np
ERROR = 0.001
w = 0.9
A = np.array([[5, 2, 1],
[-1, 4, 2],
[2, -3, 10]])
b = np.array([[-12], [20], [3]])
m, n = np.shape(A)
D = np.mat(np.zeros((m, n)))
L = np.mat(np.zeros((m, n)))
U = np.mat(np.zeros((m, n)))
for i in range(m):
for j in range(n):
if i == j:
D[i, j] = A[i, j]
if i < j:
L[i, j] = -A[i, j]
if i > j:
U[i, j] = -A[i, j]
# print(A)
# print(D)
# print(L)
# print(U)
x0 = np.array(np.zeros((m, 1)))
xk = np.array(np.zeros((m, 1)))
temp_mat = (D - w * L).I
B = np.dot(temp_mat, (1 - w) * D + w * U)
f = np.dot(temp_mat, b)
print("B:", B)
print("f:", f)
iter_time = 1
xk = np.dot(B, x0) + f
while(np.linalg.norm((xk - x0)) >= ERROR):
iter_time += 1
print(xk)
x0 = xk
xk = np.dot(B, xk) + f
print("The iteration time: %d" %iter_time)
In the running results, you can see that the total number of iterations 7 Time , It shows that the convergence speed of bijacobi iterative method is faster , however SOR Iteration and G-S The iterative method is almost the same , Actually G-S The iterative method is just w = 1 At the time of the SOR Iterative method . Intermediate process output is omitted , Only the last two vectors are displayed x ⃗ \vec{x} x Iterative results of
[[-4.44425225]
[ 3.33368384]
[ 2.22092673]]
[[-4.44445216]
[ 3.33344291]
[ 2.22215271]]
The iteration time: 7
边栏推荐
- log4j
- 事件委托的使用与说明》
- Didn't receive robot state (joint angles) with recent timestamp within 1 seconds
- Highlight display of Jinbei LB box, adhering to mini special effects
- 2022第六季完美童模 托克逊赛区 决赛圆满落幕
- GD32 RT-Thread PWM驱动函数
- The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded
- JS obtient la chaîne spécifiée spécifiant la position du caractère & sous - chaîne spécifiant la plage de position du caractère 【 détails simples 】
- WGet -- 404 not found due to spaces in URL
- [AGC] build service 3- authentication service example
猜你喜欢

调试方法和技巧详解

Appium automation test foundation - ADB shell command

Basic MySQL operation commands of database
![[AGC] build service 3- authentication service example](/img/32/44547c00476a055557dd1790e18849.png)
[AGC] build service 3- authentication service example

孙安民作品《莲花净心》数字藏品上线长城数艺

Jump table introduction

A brief introduction to database mysql

MIT-6874-Deep Learning in the Life Sciences Week5

Quick completion guide for manipulator (4): reducer of key components of manipulator

新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
随机推荐
G 代码解释|最重要的 G 代码命令列表
Tooltips in the era of touch
事件委托的使用与说明》
跳跃表介绍
Use and description of event delegation
Chen Haotian won the national championship of the national finals of the 7th children's model star ceremony
keras ‘InputLayer‘ object is not iterable
AttributeError: ‘Version‘ object has no attribute ‘major‘
South China Industrial Group launched digital economy and successfully held the city chain technology conference
基于强化学习的股票量化交易Automated-Stock-Trading-Ensemble-Strategy
JS get the substring of the specified character position and the specified character position interval of the specified string [simple and detailed]
C语言实现扫雷游戏,附详解及完整代码
MIT-6874-Deep Learning in the Life Sciences Week6
Applying applet container technology to IOT ecological construction
文章内容无法复制复制不了
GD32 RT-Thread RTC驱动函数
Magnetic levitation 3D lamp
Appium automation test foundation - 12 Introduction to appium automated testing framework
机械臂速成小指南(四):机械臂关键部件之减速机
光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述