当前位置:网站首页>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
边栏推荐
- Rider打开Unity脚本后没有提示
- Xinguan has no lover, and all the people benefit from loving deeds to warm the world -- donation to the public welfare action of Shangqiu children's welfare home
- Right click to open CMD (command line)
- 华南产业集团发力数字经济,城链科技发布会成功召开
- C語言實現掃雷遊戲,附詳解及完整代碼
- G 代码解释|最重要的 G 代码命令列表
- 光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
- Article content cannot be copied
- unable to convert expression into double array
- 戴森设计大奖,以可持续化设计改变世界
猜你喜欢

Jump table introduction

G code explanation | list of the most important G code commands

How to seize the opportunity of NFT's "chaos"?

unable to convert expression into double array

Basic MySQL operation commands of database

MySQL index, transaction and storage engine of database (2)

Deploy lvs-dr cluster

Eth is not connected to the ore pool

戴森设计大奖,以可持续化设计改变世界
![[JVM] G1 garbage collector](/img/fc/ea1f8cee0f207e4a5c804f88f2871c.png)
[JVM] G1 garbage collector
随机推荐
OSError: [Errno 28] No space left on device
光明行动:共同呵护好孩子的眼睛——广西实施光明行动实地考察调研综述
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
The URL copied by the browser and pasted into the document is a hyperlink
G 代码解释|最重要的 G 代码命令列表
Theme Studio
MySQL advanced SQL statement of database (1)
长城数艺数字藏品平台发布创世徽章
The famous painter shiguoliang's "harvest season" digital collection was launched on the Great Wall Digital Art
Highlight display of Jinbei LB box, adhering to mini special effects
Enter the world of helium (hNT) hotspot servers to bring you different benefits
unable to convert expression into double array
Deploy lvs-dr cluster
Harvester ch1 of CKB and HNS, connection tutorial analysis
“昆明城市咖啡地圖”活動再度開啟
Detailed explanation of SolidWorks mass characteristics (inertia tensor, moment of inertia, inertia spindle)
Guolin was crowned the third place of global popularity of perfect master in the third quarter of 2022
AttributeError: ‘Version‘ object has no attribute ‘major‘
6.Redis新数据类型
2022 Season 6 perfect children's model toxon division finals came to a successful conclusion