我想为三对角矩阵向量实现求解器,即Ax = b。在编写循环部分时,它显示'标量变量的无效索引。'我想知道为什么以及如何修复它。这是我的代码。
=VLOOKUP(B2,[file2.xlsx]Sheet1!$A$2:$B$4,2)
问题显示在import numpy as np
def tmv_solver(a,b,c,d):
a = np.array(a)
b = np.array(b)
c = np.array(c)
d = np.array(d)
n = len(a)
x = []
b_star = []
d_star = []
b_star = b[0]
d_star = d[0]
for i in range(1,n):
b_star = np.append(b_star,b[i]-a[i+1]/b_star[i]*c[i])
d_star = np.append(d_star,d[i+1]-a[i+1]/b_star[i]*d_star[i])
if b_star[i] == 0:
return 'Error'
x[-1] = d_star[-1]/b_star[-1]
for i in range(n-1,-1):
x.append((d_star[i]-b_star[i+1]*x[i])/b_star[i])
return x
#small test
a = [1,1,1]
b = [-2,-2,-2,-2]
c = [1,1,1]
d = [0.4,0.4,0.4,0.4]
y = tmv_solver(a,b,c,d)
print y
处,标题变量的索引无效。
任何帮助都会很感激!