雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现

雅可比迭代法:

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第1张图片

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第2张图片

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第3张图片

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第4张图片

高斯-赛德尔迭代法:

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第5张图片

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第6张图片

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第7张图片

SOR迭代法:

雅可比迭代法、高斯 - 赛德尔迭代法和SOR迭代法的实现_第8张图片

import numpy as np
import numpy.linalg as nl

def MaxOfList(A):
    max = A[0]
    for i in range(0, len(A)):
        if A[i] > max:
            max = A[i]
    return max

def Jacobi(A, b, n):
    X = np.zeros(n)
    Y = np.zeros(n)
    D = np.zeros(n)
    X1 = np.zeros([n, 1])
    b1 = np.zeros([n, 1])
    for i in range(0, n):
        b1[i][0] = b[i]
    k = 0
    while (True):
        for i in range(0, n):
            tmp = 0
            for j in range(0, n):
                if j != i:
                    tmp += A[i][j] * X[j]
            Y[i] = (b[i] - tmp) / A[i][i]
        for i in range(0, n):
            X1[i][0] = Y[i]
        D = np.dot(A, X1)
        D = D - b1
        k = k + 1
        if k >= 100:
            break
        if  (nl.norm(D) / nl.norm(b) < 1e-6) & (nl.norm(D) / nl.norm(b) > -(1e-6)):
            break
        X = Y.copy()
    return Y
        
def GaussSeidel(A, b, n):
    X = np.zeros(n)
    Y = np.zeros(n)
    D = np.zeros(n)
    X1 = np.zeros([n, 1])
    b1 = np.zeros([n, 1])
    for i in range(0, n):
        b1[i][0] = b[i]
    k = 0
    while (True):
        for i in range(0, n):
            tmp = 0
            for j in range(0, n):
                if j < i:
                    tmp += A[i][j] * Y[j]
                if j > i:
                    tmp += A[i][j] * X[j]
            Y[i] = (b[i] - tmp) / A[i][i]
        for i in range(0, n):
            X1[i][0] = Y[i]
        D = np.dot(A, X1)
        D = D - b1
        k = k + 1
        if k >= 50:
            break
        if  (nl.norm(D) / nl.norm(b) < 1e-6) & (nl.norm(D) / nl.norm(b) > -(1e-6)):
            break
        X = Y.copy()
    return Y

def SOR(A, b, n, w):
    X = np.zeros(n)
    Y = np.zeros(n)
    D = np.zeros(n)
    X1 = np.zeros([n, 1])
    b1 = np.zeros([n, 1])
    for i in range(0, n):
        b1[i][0] = b[i]
    k = 0
    while (True):
        for i in range(0, n):
            tmp1 = 0
            tmp2 = 0
            for j in range(0, n):
                if j < i:
                    tmp1 += A[i][j] * Y[j]
                if j > i:
                    tmp2 += A[i][j] * X[j]
            Y[i] = X[i] + (w * (b[i] - tmp1 - tmp2)) / A[i][i]
        for i in range(0, n):
            X1[i][0] = Y[i]
        D = np.dot(A, X1)
        D = D - b1
        k = k + 1
        if k >= 50:
            break
        if  (nl.norm(D) / nl.norm(b) < 1e-6) & (nl.norm(D) / nl.norm(b) > -(1e-6)):
            break
        X = Y.copy()
    return Y
    
if __name__ == "__main__":
    # 构造矩阵A
    A = np.zeros([100, 100]);
    for i in range(0, 100):
        A[i][i] = 2
        if i == 0:
            A[i][i + 1] = -1
        elif i == 99:
            A[i][i - 1] = -1
        else:
            A[i][i - 1] = A[i][i + 1] = -1
    # 构造向量b
    b = np.zeros(100);
    for i in range(0, 100):
        b[i] = 1
    result1 = Jacobi(A, b, 100)
    result2 = GaussSeidel(A, b, 100)
    result3 = SOR(A, b, 100, 0.07)
    print("雅可比迭代结果:")
    print(result1)
    print("高斯-赛德尔迭代结果:")
    print(result2)
    print("SOR迭代结果:")
    print(result3)

 

你可能感兴趣的:(数值计算与优化)