数值计算·第九集:雅可比迭代(Numpy版)

ps:这是按照自己的理解写的程序,代码比较糙。献丑了,望海涵!

#Jacobi interation
import numpy as np
import sys

def Jacobi(x0,A,b):
    
    if A.shape[1] != x0.shape[0] and A.shape[0] != b.shape[0]:
        sys.exit('Please try again! Because the dimensions are not equal.')
    
    k = 0
    eps = 1e-6
    flag = True
    count = 1
    
    n = x0.shape[0]
    x = np.empty([n,1])
    
    
    while flag:
        for i in range(0,n):
            x[i] = (b[i] - A[i,:]@x0 + A[i,i]*x0[i])/A[i,i]
        
        print('No.{}:\nx = {}'.format(count,x))
        count += 1
        
        if np.linalg.norm(x-x0,np.inf)

 

你可能感兴趣的:(Numerical,Optimization,优化-计算(程序))