当前位置:网站首页>数值计算方法 Chapter6. 解线性方程组的迭代法
数值计算方法 Chapter6. 解线性方程组的迭代法
2022-06-12 07:33:00 【Espresso Macchiato】
0. 问题描述
这一章节要解的问题和上一章是一样的,依然还是 n n n元线性方程组的求解问题。
{ a 11 x 1 + a 12 x 2 + . . . + a 1 n x n = y 1 a 21 x 1 + a 22 x 2 + . . . + a 2 n x n = y 2 . . . a n 1 x 1 + a n 2 x 2 + . . . + a n n x n = y n \left\{ \begin{aligned} a_{11}x_1 + a_{12}x_2 + ... + a_{1n}x_n &= y_1 \\ a_{21}x_1 + a_{22}x_2 + ... + a_{2n}x_n &= y_2 \\ ... \\ a_{n1}x_1 + a_{n2}x_2 + ... + a_{nn}x_n &= y_n \end{aligned} \right. ⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧a11x1+a12x2+...+a1nxna21x1+a22x2+...+a2nxn...an1x1+an2x2+...+annxn=y1=y2=yn
但是,上一章主要是通过矩阵的线性变换转换成可以快速求解的三角阵或者对角阵的方式进行求解,其计算结果是精确的结果。
而本章则是的思路则是将问题 A x = y Ax=y Ax=y转换成 x = A ′ x x = A'x x=A′x的迭代形式,从而,我们就可以给出迭代数组 x k + 1 = A ′ x k x_{k+1} = A'x_{k} xk+1=A′xk。
此时,如果 x k x_k xk满足收敛条件,那么 x k x_{k} xk就会收敛到 x = A ′ x x = A'x x=A′x的一组解当中,上述问题同样可以得到解答。
1. Jacobi迭代
1. Jacobi迭代方法
对原始方程进行线性变换,我们显然有:
{ x 1 = − a 12 a 11 x 2 − . . . − a 1 n a 11 x n + y 1 a 11 x 2 = − a 21 a 22 x 2 − . . . − a 2 n a 22 x n + y 2 a 22 . . . x n = − a n 1 a n n x 1 − a n 2 a n n x 2 − . . . + y n a n n \left\{ \begin{aligned} x_{1} = & &-\frac{a_{12}}{a_{11}}x_2 &-... &-\frac{a_{1n}}{a_{11}}x_n &+ \frac{y_1}{a_{11}} \\ x_{2} = &-\frac{a_{21}}{a_{22}}x_2 & &- ... &-\frac{a_{2n}}{a_{22}}x_n &+ \frac{y_2}{a_{22}} \\ ... \\ x_{n} = &-\frac{a_{n1}}{a_{nn}}x_1 &-\frac{a_{n2}}{a_{nn}}x_2 &- ... & &+ \frac{y_n}{a_{nn}} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎧x1=x2=...xn=−a22a21x2−annan1x1−a11a12x2−annan2x2−...−...−...−a11a1nxn−a22a2nxn+a11y1+a22y2+annyn
我们将其写成矩阵形式即为:
( x 1 x 2 . . . x n ) = ( 0 − a 12 a 11 . . . − a 1 n a 11 − a 21 a 22 0 . . . − a 2 n a 22 . . . . . . . . . . . . − a n 1 a n n − a n 2 a n n . . . 0 ) ( x 1 x 2 . . . x n ) + ( y 1 a 11 y 2 a 22 . . . y n a n n ) \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} = \begin{pmatrix} 0 & -\frac{a_{12}}{a_{11}} & ... & -\frac{a_{1n}}{a_{11}} \\ -\frac{a_{21}}{a_{22}} & 0 & ... & -\frac{a_{2n}}{a_{22}} \\ ... & ... & ... & ... \\ -\frac{a_{n1}}{a_{nn}} & -\frac{a_{n2}}{a_{nn}} & ... & 0 \end{pmatrix} \begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \end{pmatrix} + \begin{pmatrix} \frac{y_1}{a_{11}} \\ \frac{y_2}{a_{22}} \\ ... \\ \frac{y_n}{a_{nn}} \end{pmatrix} ⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞=⎝⎜⎜⎛0−a22a21...−annan1−a11a120...−annan2............−a11a1n−a22a2n...0⎠⎟⎟⎞⎝⎜⎜⎛x1x2...xn⎠⎟⎟⎞+⎝⎜⎜⎛a11y1a22y2...annyn⎠⎟⎟⎞
我们将其简化为符号表达即为:
x = B x + g x = Bx + g x=Bx+g
基于此,我们就可以给出Jacobi迭代公式如下:
x k + 1 = B x k + g x_{k+1} = Bx_{k} + g xk+1=Bxk+g
2. Jacobi迭代矩阵
我们给出更加严格的数据表达如下,令:
D = ( a 11 a 22 . . . a n n ) D = \begin{pmatrix} a_{11} \\ & a_{22} \\ & & ... \\ & & & a_{nn} \end{pmatrix} D=⎝⎜⎜⎛a11a22...ann⎠⎟⎟⎞
则有:
A x = ( D + A − D ) x = y Ax = (D + A - D)x = y Ax=(D+A−D)x=y
进而可以导出:
D x = ( D − A ) x + y Dx = (D-A)x + y Dx=(D−A)x+y
两边左乘 D − 1 D^{-1} D−1即有:
x = D − 1 ( D − A ) x + D − 1 y x = D^{-1}(D-A)x + D^{-1}y x=D−1(D−A)x+D−1y
记 B = D − 1 ( D − A ) B=D^{-1}(D-A) B=D−1(D−A), g = D − 1 y g=D^{-1}y g=D−1y,即可得到上一小节中一模一样的内容。
迭代矩阵 B B B即为Jacobi迭代矩阵。
3. Jacobi迭代收敛条件
Jacobi迭代收敛的充要条件为:
迭代矩阵 B B B的谱半径 ρ ( B ) < 1 \rho(B) < 1 ρ(B)<1。
亦即:
- 对于迭代矩阵 B B B的所有本征值 λ i \lambda_i λi,均有 ∣ λ i ∣ < 1 |\lambda_i| < 1 ∣λi∣<1,或者等价的 m a x ( ∣ λ i ∣ ) < 1 max(|\lambda_i|) < 1 max(∣λi∣)<1。
不过,在一些特定的情况下,上述充要条件可以有适当的放宽。
具体而言,有如下定理:
定理 6.1
若方程组 A x = y Ax=y Ax=y的系数矩阵 A A A,满足下列条件之一,则其Jacobi迭代收敛:
(1) A为行对角优阵,即 ∣ a i i ∣ > ∑ j ≠ i ∣ a i j ∣ |a_{ii}| > \sum_{j \neq i} |a_{ij}| ∣aii∣>∑j=i∣aij∣, i = 1 , 2 , . . . , n i=1,2,...,n i=1,2,...,n;
(2) A为列对角优阵,即 ∣ a j j ∣ > ∑ i ≠ j ∣ a i j ∣ |a_{jj}| > \sum_{i \neq j} |a_{ij}| ∣ajj∣>∑i=j∣aij∣, j = 1 , 2 , . . . , n j=1,2,...,n j=1,2,...,n;
4. python伪代码实现
最后,我们给出Jacobi迭代的python伪代码如下:
def jacobi_iter(A, y, epsilon=1e-6):
n = len(A)
B = [[0 for _ in range(n)] for _ in range(n)]
g = [0 for _ in range(n)]
for i in range(n):
for j in range(n):
if j == i:
continue
B[i][j] = -A[i][j] / A[i][i]
g[i] = y[i] / A[i][i]
x = [0 for _ in range(n)]
for _ in range(10**6):
z = [0 for _ in range(n)]
for i in range(n):
z[i] += g[i]
for j in range(n):
z[i] += B[i][j] * x[j]
if max(abs(z[i] - x[i]) for i in range(n)) < epsilon:
break
x = z
return z
2. Gauss-Seidel迭代
1. Gauss-Seidel迭代方法
Gauss-Seidel迭代方程和上述Jacobi迭代事实上是非常相似的,唯一的区别在于说Jacobi迭代是以 x ( k ) x^{(k)} x(k)为整体每次一起进行迭代更新的,而Guass-Seidel迭代则是在计算每一个 x i ( k ) x^{(k)}_{i} xi(k)的时候就是用当前已经迭代计算完成的所有的 x j ( k ) x^{(k)}_{j} xj(k)的结果。
即是说,以每一个元素为单位不断进行迭代更新。
用公式表达即为:
{ x 1 ( k + 1 ) = − a 12 a 11 x 2 ( k ) − . . . − a 1 n a 11 x n ( k ) + y 1 a 11 x 2 ( k + 1 ) = − a 21 a 22 x 1 ( k + 1 ) − . . . − a 2 n a 22 x n ( k ) + y 2 a 22 . . . x n ( k + 1 ) = − a n 1 a n n x 1 ( k + 1 ) − a n 2 a n n x 2 ( k + 1 ) − . . . + y n a n n \left\{ \begin{aligned} x^{(k+1)}_{1} = & &-\frac{a_{12}}{a_{11}}x^{(k)}_2 &-... &-\frac{a_{1n}}{a_{11}}x^{(k)}_n &+ \frac{y_1}{a_{11}} \\ x^{(k+1)}_{2} = &-\frac{a_{21}}{a_{22}}x^{(k+1)}_1 & &- ... &-\frac{a_{2n}}{a_{22}}x^{(k)}_n &+ \frac{y_2}{a_{22}} \\ ... \\ x^{(k+1)}_{n} = &-\frac{a_{n1}}{a_{nn}}x^{(k+1)}_1 &-\frac{a_{n2}}{a_{nn}}x^{(k+1)}_2 &- ... & &+ \frac{y_n}{a_{nn}} \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎧x1(k+1)=x2(k+1)=...xn(k+1)=−a22a21x1(k+1)−annan1x1(k+1)−a11a12x2(k)−annan2x2(k+1)−...−...−...−a11a1nxn(k)−a22a2nxn(k)+a11y1+a22y2+annyn
2. Gauss-Seidel迭代矩阵
同样的,我们给出更加严格的数学推导如下:
A = D + L + U = ( a 11 a 22 . . . a n n ) + ( 0 a 21 0 . . . a n 1 a n 2 . . . 0 ) ( 0 a 12 . . . a 1 n 0 . . . a 2 n . . . 0 ) \begin{aligned} A &= D + L + U \\ &= \begin{pmatrix} a_{11} \\ & a_{22} \\ & & ... \\ & & & a_{nn} \end{pmatrix} + \begin{pmatrix} 0 \\ a_{21} & 0 \\ ... \\ a_{n1} & a_{n2} & ... & 0 \end{pmatrix} \begin{pmatrix} 0 & a_{12} & ... & a_{1n} \\ & 0 & ... & a_{2n} \\ & & ... \\ & & & 0 \end{pmatrix} \end{aligned} A=D+L+U=⎝⎜⎜⎛a11a22...ann⎠⎟⎟⎞+⎝⎜⎜⎛0a21...an10an2...0⎠⎟⎟⎞⎝⎜⎜⎛0a120.........a1na2n0⎠⎟⎟⎞
从而,我们有:
A x = ( D + L + U ) x = y Ax = (D+L+U)x = y Ax=(D+L+U)x=y
亦即:
( D + L ) x = − U x + y (D+L)x = -Ux + y (D+L)x=−Ux+y
两边同乘以 ( D + l ) − 1 (D+l)^{-1} (D+l)−1即有:
x = − ( D + L ) − 1 U x + ( D + L ) − 1 y x = -(D+L)^{-1}Ux + (D+L)^{-1}y x=−(D+L)−1Ux+(D+L)−1y
令 S = − ( D + L ) − 1 U S = -(D+L)^{-1}U S=−(D+L)−1U, f = ( D + L ) − 1 y f = (D+L)^{-1}y f=(D+L)−1y,我们即可写出Gauss-Seidel迭代公式:
x ( k + 1 ) = S x ( k ) + f x^{(k+1)} = Sx^{(k)} + f x(k+1)=Sx(k)+f
称迭代矩阵 S S S即为Gauss-Seidel迭代矩阵。
当然,如上一小节所述,实际在算法实现当中并没有必要计算出 S S S和 f f f,只需要根据前面 J a c o b i Jacobi Jacobi迭代矩阵进行实时参数更新即可。
3. Gauss-Seidel迭代收敛条件
同样的,我们给出书中关于Gauss-Seidel迭代的收敛条件如下:
定理6.2
若方程组系数矩阵为行或列对角优时,则Gauss-Seidel迭代收敛。
定理6.3
若方程组系数矩阵 A A A为对称正定阵,则Gauss-Seidel迭代收敛。
4. 伪代码实现
同样的,我们用python给出伪代码如下:
def gauss_seidel_iter(A, y, epsilon=1e-6):
n = len(A)
B = [[0 for _ in range(n)] for _ in range(n)]
g = [0 for _ in range(n)]
for i in range(n):
for j in range(n):
if j == i:
continue
B[i][j] = -A[i][j] / A[i][i]
g[i] = y[i] / A[i][i]
x = [0 for _ in range(n)]
cnt = 0
for _ in range(10**6):
for i in range(n):
z = g[i]
for j in range(n):
z += B[i][j] * x[j]
if abs(z-x[i]) < epsilon:
cnt += 1
else:
cnt = 0
x[i] = z
if cnt >= n:
break
if cnt >= n:
break
return x
3. 松弛迭代
1. 松弛迭代方法
松弛迭代的原型依然还是之前的Jacobi迭代,不过,和Gauss-Seidel迭代的实时参数更新不同,松弛迭代在这里是对Jacobi迭代式的批次更新以及Gauss-Seidel迭代式的实时更新取了一个折中,通过一个超参 w w w来进行参数更新速度的控制。
具体而言,即为:
{ x 1 ( k + 1 ) = ( 1 − w ) x 1 ( k ) + w ( b 12 x 2 ( k ) + b 13 x 3 ( k ) + . . . + b 1 n x n ( k ) + g 1 ) x 2 ( k + 1 ) = ( 1 − w ) x 2 ( k ) + w ( b 21 x 1 ( k + 1 ) + b 23 x 3 ( k ) + . . . + b 2 n x n ( k ) + g 2 ) . . . x n ( k + 1 ) = ( 1 − w ) x n ( k ) + w ( b n 1 x 1 ( k + 1 ) + b n 2 x 2 ( k + 1 ) + . . . + b n , n − 1 x n − 1 ( k + 1 ) + g n ) \left\{ \begin{aligned} x^{(k+1)}_1 &= (1-w)x^{(k)}_1 + w(b_{12}x^{(k)}_2 + b_{13}x^{(k)}_3 + ... + b_{1n}x^{(k)}_n + g_1) \\ x^{(k+1)}_2 &= (1-w)x^{(k)}_2 + w(b_{21}x^{(k+1)}_1 + b_{23}x^{(k)}_3 + ... + b_{2n}x^{(k)}_n + g_2) \\ ... \\ x^{(k+1)}_n &= (1-w)x^{(k)}_n + w(b_{n1}x^{(k+1)}_1 + b_{n2}x^{(k+1)}_2 + ... + b_{n,n-1}x^{(k+1)}_{n-1} + g_n) \end{aligned} \right. ⎩⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎧x1(k+1)x2(k+1)...xn(k+1)=(1−w)x1(k)+w(b12x2(k)+b13x3(k)+...+b1nxn(k)+g1)=(1−w)x2(k)+w(b21x1(k+1)+b23x3(k)+...+b2nxn(k)+g2)=(1−w)xn(k)+w(bn1x1(k+1)+bn2x2(k+1)+...+bn,n−1xn−1(k+1)+gn)
2. 松弛迭代矩阵
同样的,我们给出松弛迭代的数学表达如下:
x ( k + 1 ) = ( 1 − w ) x ( k ) + w ( L ~ x ( k + 1 ) + U ~ x ( k ) + g ) x^{(k+1)} = (1-w)x^{(k)} + w(\tilde{L}x^{(k+1)} + \tilde{U}x^{(k)} + g) x(k+1)=(1−w)x(k)+w(L~x(k+1)+U~x(k)+g)
仿照Gauss-Seidel迭代,我们同样可以给出其迭代矩阵格式如下:
x ( k + 1 ) = S w x ( k ) + f x^{(k+1)} = S_w x^{(k)} + f x(k+1)=Swx(k)+f
其中,
{ S w = ( I + w D − 1 L ) − 1 [ ( 1 − w ) I − w D − 1 U ] f = w ( I + w D − 1 L ) − 1 g \left\{ \begin{aligned} S_w &= (I + wD^{-1}L)^{-1}[(1-w)I - wD^{-1}U] \\ f &= w(I + wD^{-1}L)^{-1}g \end{aligned} \right. { Swf=(I+wD−1L)−1[(1−w)I−wD−1U]=w(I+wD−1L)−1g
3. 松弛迭代的收敛条件
而松弛迭代的收敛判断存在如下两个定理:
定理 6.4
松弛迭代收敛的必要条件为 0 < w < 2 0 < w < 2 0<w<2;
定理 6.5
若 A A A为对称正定矩阵,则当 0 < w < 2 0 < w < 2 0<w<2时,松弛迭代恒收敛;
特别的,当 0 < w < 1 0 < w < 1 0<w<1时,称其为亚松弛迭代;当 1 < w < 2 1 < w < 2 1<w<2时,称其为超松弛迭代;当 w = 1 w=1 w=1时,迭代退化为Gauss-Seidel迭代。
4. 伪代码实现
最后,我们同样给出松弛迭代的伪代码实现如下:
def loose_iter(A, y, w=0.5, epsilon=1e-6):
n = len(A)
B = [[0 for _ in range(n)] for _ in range(n)]
g = [0 for _ in range(n)]
for i in range(n):
for j in range(n):
if j == i:
continue
B[i][j] = -A[i][j] / A[i][i]
g[i] = y[i] / A[i][i]
x = [0 for _ in range(n)]
cnt = 0
for _ in range(10**6):
for i in range(n):
z = (1-w) * x[i] + w * g[i]
for j in range(n):
z += w * B[i][j] * x[j]
if abs(z-x[i]) < epsilon:
cnt += 1
else:
cnt = 0
x[i] = z
if cnt >= n:
break
if cnt >= n:
break
return x
4. 逆矩阵计算
最后,我们来考察一下逆矩阵计算。
逆矩阵的计算原则上来说其实算是上述解线性方程组的一个特殊应用,事实上解 n n n个单元向量然后将其解拼接一下就能得到我们的逆矩阵了。
我们这里就不进行详述了,只给出python伪代码实现如下:
def cal_inverse_matrix(A):
n = len(A)
res = [[0 for _ in range(n)] for _ in range(n)]
for j in range(n):
y = [0 for _ in range(n)]
y[j] = 1
x = gauss_seidel_iter(A, y)
for i in range(n):
res[i][j] = x[i]
return res
边栏推荐
- Keil installation of C language development tool for 51 single chip microcomputer
- 5 lines of code identify various verification codes
- 晶闸管,它是很重要的,交流控制器件
- paddlepaddl 28 支持任意维度数据的梯度平衡机制GHM Loss的实现(支持ignore_index、class_weight,支持反向传播训练,支持多分类)
- BI技巧丨当月期初
- Principle and application of PWM
- 【高考那些事】准大学生看过来,选择方向和未来,自己把握
- Voice assistant -- Qu -- query error correction and rewriting
- Use case design of software testing interview questions
- [yolo-v5 learning notes]
猜你喜欢

Arrangement of statistical learning knowledge points gradient descent, least square method, Newton method

RT thread studio learning (VII) using multiple serial ports

Detailed explanation of 8086/8088 system bus (sequence analysis + bus related knowledge)

Detailed explanation of addressing mode in 8086

Detailed explanation of TF2 command line debugging tool in ROS (parsing + code example + execution logic)

2022r2 mobile pressure vessel filling test question simulation test platform operation

Design an open source continuous deployment pipeline based on requirements

2022 electrician (elementary) examination question bank and simulation examination

Golang quickly generates model and queryset of database tables

Voice assistant - those classification models used in the assistant
随机推荐
tmux 和 vim 的快捷键修改
Stm32cubemx learning (I) USB HID bidirectional communication
Improved schemes for episodic memory based lifelong learning
Vs 2019 MFC connects and accesses access database class library encapsulation through ace engine
linux下怎么停止mysql服务
Non IID data and continuous learning processes in federated learning: a long road ahead
FCPX插件:简约线条呼出文字标题介绍动画Call Outs With Photo Placeholders for FCPX
Complete set of typescript Basics
Velocity autocorrelation function lammps v.s MATALB
Summary of semantic segmentation learning (I) -- basic concepts
Summary of machine learning + pattern recognition learning (IV) -- decision tree
Summary of machine learning + pattern recognition learning (I) -- k-nearest neighbor method
Right click the general solution of file rotation jam, refresh, white screen, flash back and desktop crash
Use case design of software testing interview questions
Use of gt911 capacitive touch screen
Detailed explanation of 14 registers in 8086CPU
2022r2 mobile pressure vessel filling test question simulation test platform operation
Modelarts training task 1
Vs2019 MFC IP address control control inherits cipaddressctrl class redrawing
Day 5 of pyhon