Bootstrap

计算方法 - 雅可比迭代法 / 高斯-赛德尔迭代法(求解线性方程组)

直接法:求解低阶稠密方程组
迭代法:求解高阶稀疏方程组

迭代法:

A X = b AX = b AX=b转化为 X = B X + f X = BX+f X=BX+f对X进行迭代求解

雅可比迭代法:

雅可比迭代公式:

矩阵形式:
X k + 1 = B J X k + f J X^{k+1} = B_JX^{k}+f_J Xk+1=BJXk+fJ其中: B J = D − 1 ( L + U ) ( 称 为 雅 可 比 迭 代 矩 阵 ) , f J = D − 1 b B_J = D^{-1}(L+U)(称为雅可比迭代矩阵),f_J = D^{-1}b BJ=D1(L+U)(,fJ=D1b
元素形式:
x i k + 1 = 1 a i i ( b i − ∑ j = 1 , j ! = i n a i j x j k ) ( i = 1 , 2 , . . . , n ; k = 0 , 1 , 2 , . . . ) x_i^{k+1} = \frac{1}{a_{ii}}(b_i - \sum_{j=1,j!=i}^na_{ij}x_{j}^k) (i=1,2,...,n;k=0,1,2,...) xik+1=aii1(bij=1,j!=inaijxjk)(i=1,2,...,n;k=0,1,2,...)

题目:

【问题描述】已知线性方程组Ax=b,采用Jacobi法求解x,使得||x_{k+1}-x_{k}||_inf<1e-5
【输入形式】任意阶矩阵A按行输入,b按行输入
【输出形式】保留5位小数
【样例输入】

5.0 2 0 3 10 3 0 4 20

3 5 7

0 0 0

【样例输出】

x:

0.49512

0.26219

0.29756

【样例说明】3阶矩阵A元素5.0 2 0 3 10 3 0 4 20,向量b元素3 5 7,初始值0 0 0

Python实现代码:

import numpy as np


class Jacobi(object):
    def __init__(self, A, X, b):
        self.A = A
        self.X = X
        self.b = b
        self._X = np.zeros(X.shape[0])

    def function(self):
        n = self.A.shape[0]
        for i in range(n):
            self.X[i] = self._X[i]
        for i in range(n):
            self._X[i] = self.b[i]
            for j in range(n):
                if j != i:
                    self._X[i] -= self.A[i][j] * self.X[j]
            self._X[i] /= self.A[i][i]

    def start(self, error):
        self.function()
        while abs(np.max(self.X) - np.max(self._X)) > error:
            self.function()


if __name__ == '__main__':
    A = np.array(input().split(" "))
    b = np.array(input().split(" "))
    X = np.array(input().split(" "))
    A = A.astype(np.float64)
    b = b.astype(np.float64)
    X = X.astype(np.float64)
    n = X.shape[0]
    A = A.reshape((n, n))
    jacobi = Jacobi(A, X, b)
    jacobi.start(1e-5)
    ANS = np.round(jacobi._X, 5)
    print("x:")
    for i in range(ANS.shape[0]):
        print(ANS[i])

高斯-赛德尔迭代法:

高斯-赛德尔迭代公式:

矩阵形式:
X k + 1 = B G X k + f G X^{k+1} = B_GX^{k}+f_G Xk+1=BGXk+fG其中: B G = ( D − L ) − 1 U ( 称 为 高 斯 − 赛 德 尔 迭 代 矩 阵 ) , f G = ( D − L ) − 1 b B_G = (D-L)^{-1}U(称为高斯-赛德尔迭代矩阵),f_G = (D-L)^{-1}b BG=(DL)1U(,fG=(DL)1b
元素形式:
x i k + 1 = 1 a i i ( b i − ∑ j = 1 , j ! = i i − 1 a i j x j k + 1 − ∑ j = i + 1 , j ! = i n a i j x j k ) ( i = 1 , 2 , . . . , n ; k = 0 , 1 , 2 , . . . ) x_i^{k+1} = \frac{1}{a_{ii}}(b_i - \sum_{j=1,j!=i}^{i-1}a_{ij}x_{j}^{k+1} - \sum_{j=i+1,j!=i}^na_{ij}x_{j}^k) (i=1,2,...,n;k=0,1,2,...) xik+1=aii1(bij=1,j!=ii1aijxjk+1j=i+1,j!=inaijxjk)(i=1,2,...,n;k=0,1,2,...)

题目:

【问题描述】已知线性方程组Ax=b,采用G-S法求解x,使得||x_{k+1}-x_{k}||_inf<1e-5
【输入形式】任意阶矩阵A按行输入,b按行输入
【输出形式】保留5位小数
【样例输入】
5.0 2 0 3 10 3 0 4 20
3 5 7
0 0 0
【样例输出】
x:
0.49512
0.26220
0.29756
【样例说明】3阶矩阵A元素5.0 2 0 3 10 3 0 4 20,向量b元素3 5 7,初始值0 0 0

Python实现代码:

import numpy as np


class GaussSeidel(object):
    def __init__(self, A, X, b):
        self.A = A
        self.X = X
        self.b = b
        self._X = np.zeros(X.shape[0])

    def function(self):
        n = self.A.shape[0]
        for i in range(n):
            self.X[i] = self._X[i]
        for i in range(n):
            self._X[i] = self.b[i]
            for j in range(0, i):
                self._X[i] -= self.A[i][j] * self._X[j]
            for j in range(i+1, n):
                self._X[i] -= self.A[i][j] * self.X[j]
            self._X[i] /= self.A[i][i]

    def start(self, error):
        self.function()
        while abs(np.max(self.X) - np.max(self._X)) > error:
            self.function()


if __name__ == '__main__':
    A = np.array(input().split(" "))
    b = np.array(input().split(" "))
    X = np.array(input().split(" "))
    A = A.astype(np.float64)
    b = b.astype(np.float64)
    X = X.astype(np.float64)
    n = X.shape[0]
    A = A.reshape((n, n))
    gs = GaussSeidel(A, X, b)
    gs.start(1e-5)
    ANS = np.round(gs._X, 5)
    ANS = ANS.astype(str)
    print("x:")
    for i in range(ANS.shape[0]):
        while len(ANS[i]) < 7:
            ANS[i] += "0"
        print(ANS[i])

;