import numpy as np
A = np.matrix([[1,1,-1],[0,1,3],[0,0,-6]])
b = np.array([9,3,8])
def back_sub(A,b):
n = len(A)
print('n is:', n)
x = [0]*n
for i in range(n-1,-1,-1): #this refers to the rows; i goes 2,1,0
for j in range(i+1,n):
b[i] = b[i] - A[i,j]*x[j]
x[i] = b[i]/A[i,i]
return x
我试图替换上三角矩阵A,并得出答案:x1 = 4/3,x2 = 7,x3 = -4 / 3
当我运行我的代码时,它给出了x2的正确答案 和x3 ,但是x1 出现为0,这是令人困惑的。我试图调试我的代码,它似乎运行良好,直到我= 0 。为什么这段代码没有给我正确的x1值 ?