我现在正在研究三对角矩阵上的问题,我在Wiki https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm中使用了三对角矩阵算法来实现解决方案,并且我已经尝试过,但是我的解决方案并不完整。
我很困惑,我也需要帮助,这也是我使用jupyter笔记本的脚本
import numpy as np
# The diagonals and the solution vector
b = np.array((5,6,5),dtype=float) # Main Diagonal
a= np.array((1,1,1),dtype = float) # Lower Diagonal
c = np.array((1,dtype = float) # Upper Diagonal
d = np.array((3,2,3,3),dtype = float) # Solution Vector
#number of rows
n = d.size
newC = np.zeros(n,dtype= float)
newD = np.zeros(n,dtype = float)
x = np.zeros(n,dtype = float)
# the first coefficents
newC[0] = c[0] / b[0]
newD[0] = d[0] / b[0]
for i in range(1,n - 1):
newC[i] = c[i] / (b[i] - a[i] * newC[i - 1])
for i in range(1,n -1):
newD[i] = (d[i] - a[i] * newD[i - 1]) / (b[i] - a[i] * newC[i - 1])
x[n - 1] = newD[n - 1]
for i in reversed(range(0,n - 1)):
x[i] = newD[i] - newC[i] * x[i + 1]
x