python - 如何在 Python 中求解一个几乎是三对角矩阵?家庭作业
问题描述
我应该找到不同的温度节点。
import numpy as np
## Tri Diagonal Matrix Algorithm(a.k.a Thomas algorithm) solver
def TDMAsolver(a, b, c, d):
nf = len(d) # number of equations
ac, bc, cc, dc = (x.astype(float) for x in (a, b, c, d)) # copy arrays & cast to floats
for it in range(1, nf):
mc = ac[it-1]/bc[it-1]
bc[it] = bc[it] - mc*cc[it-1]
dc[it] = dc[it] - mc*dc[it-1]
xc = bc
xc[-1] = dc[-1]/bc[-1]
for il in range(nf-2, -1, -1):
xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]
return xc
a = np.array([1,1,1,1,-62500])
b = np.array([1,-0.136,-0.136,-0.136,-0.136,77500]) # main diagonal
c = np.array([0,1,1,1,1])
d = np.array([1600,78.9,78.9,78.9,78.9,12300000]) # right side of equation
x = TDMAsolver(a,b,c,d) # pass in same order
print(x)
A = np.diag(b,0) + np.diag(a,-1) + np.diag(c,1)
x = np.linalg.solve(A,d)
print(x)
这就是我所拥有的。
[ 1600. 871.54649514 -1402.56967666 -983.39597117
1347.72782458 1245.58695531]
这是我的输出
([[1600. ],
[1099.27474358],
[ 920.27399177],
[ 856.78887848],
[ 835.68455321],
[ 832.64883323]])
我有点想要的输出。
解决方案
推荐阅读
- reactjs - Rollup 和 typescript 包没有编辑器代码完成并跳转到声明
- amazon-web-services - 在资源声明之外将环境变量附加到 Lambda
- firefox - 使用 policy.json 设置 Firefox 首选项
- r - 用 R + ggplot 在折线图中画一个点
- javascript - 无法取消选中所有复选框
- css - 如何使用 vuetify 和 Vue.delete() 使用效果淡出警报
- ms-access - 文本框中的表达式未将值传递给表
- c# - 查找和替换作业中的值
- chef-infra - 当重试次数大于 0 且 ignore_failure 为真时,是否会重复执行 ruby_block?
- amazon-web-services - 使用相同参数更新 CloudFormation 自定义资源