当前位置:网站首页>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
边栏推荐
- The digital collection of sunanmin's lotus heart clearing was launched on the Great Wall Digital Art
- The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded
- 力扣 428. 序列化和反序列化 N 叉树 DFS
- 7. development of mobile login function
- The URL copied by the browser and pasted into the document is a hyperlink
- The human agent of kDa, Jinbei kd6, takes you to explore the metauniverse
- unable to convert expression into double array
- 新冠无情人有情,芸众惠爱心善举暖人间——捐赠商丘市儿童福利院公益行动
- Jump table introduction
- The rising star of Goldshell STC box
猜你喜欢

逸仙电商发布一季报:坚持研发及品牌投入,实现可持续高质量发展

采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.

Theme Studio

‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868

KOREANO ESSENTIAL打造气质职场范

9. cache optimization

机械臂速成小指南(五):末端执行器

Rider打开Unity脚本后没有提示

陈颢天 荣获第七届少儿模特明星盛典全国总决赛 全国总冠军

Koreano essential creates a professional style
随机推荐
栈题目:字符串解码
MIT-6874-Deep Learning in the Life Sciences Week4
MySQL advanced SQL statement of database (2)
G 代码解释|最重要的 G 代码命令列表
Js获取指定字符串指定字符位置&指定字符位置区间的子串【简单详细】
Why can't you rob scientists of NFT
Automated stock trading ensemble strategy based on Reinforcement Learning
MIT-6874-Deep Learning in the Life Sciences Week4
一些国内镜像源
L'activité "Kunming City coffee map" a rouvert
‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
keras ‘InputLayer‘ object is not iterable
The preliminary round of the sixth season of 2022 perfect children's model Hefei competition area was successfully concluded
Appium automation test foundation - 12 Introduction to appium automated testing framework
采坑:Didn‘t receive robot state (joint angles) with recent timestamp within 1 seconds.
移植完整版RT-Thread到GD32F4XX(详细)
“昆明城市咖啡地图”再度开启,咖啡拉近城市距离
How to seize the opportunity of NFT's "chaos"?
About Jul
李沐《动手学习深度学习》d2lbook环境搭建