这里再求解矩阵时出现错误,目前尚未解决,有好心的大佬帮我看下代码中的额逆矩阵乘积那块为何出错,谢谢了!
Code
from array import array
from numpy import *
x = []
y = []
n = input()
n = int(n)
for i in range(n):
vx = input()
vy = input()
vx = float(vx)
x.append(vx)
vy = float(vy)
y.append(vy)
_y0 = input()
_y1 = input()
_y0 = float(_y0)
_y1 = float(_y1)
h = []
h.append(x[1] - x[0])
h.append(x[2] - x[1])
h.append(x[3] - x[2])
u1 = h[0] / (h[0] + h[1])
u2 = h[1] / (h[1] + h[2])
r1 = 1 - u1
r2 = 1 - u2
d1 = ((6 / (h[0] + h[1])) * ((y[2] - y[1]) / h[1] - (y[1] - y[0]) / h[0]))
d2 = ((6 / (h[1] + h[2])) * ((y[3] - y[2]) / h[2] - (y[2] - y[1]) / h[1]))
d0 = (6 / h[0]) * ((y[1] - y[0]) / h[0] - _y0)
d3 = ((6 / h[2]) * (_y1 - (y[3] - y[2]) / h[2]))
A = mat([[2, 1, 0, 0], [u1, 2, r1, 0], [0, u2, 2, r2], [0, 0, 1, 2]])
#print(A)
C = mat([[d0], [d1], [d2], [d3]])
#print(C)
res = A.I * C
#print(res)
res = array(res)
#print(res)
#此处求出的结果与书上例子不一致,待解决
M = []
M.append(res[0][0])
M.append(res[1][0])
M.append(res[2][0])
M.append(res[3][0])
def f(v) :
if v >= x[0] and v < x[1] :
i = 0
if v >= x[1] and v < x[2] :
i = 1
if v >= x[2] and v < x[3] :
i = 2
return (1 / 6 * h[i]) * v**3 + (1 / 2 * h[i]) * (M[i] * x[i + 1] - M[i + 1] * x[i]) * v**2 + ((1 / 2 * h[i]) * (M[i + 1] * x[i] * x[i] - M[i] * x[i + 1] * x[i + 1]) + (1 / h[i]) * (y[i + 1] - y[i]) + (h[i] / 6) * (M[i] - M[i + 1])) * v + (1 / 6 * h[i]) * (x[i] * M[i + 1] - x[i + 1] * M[i])
print(f(0.5))
print(f(1.5))
"""
data:
4
-1
0
0
0.5
1
2
2
1.5
0.5
-0.5
"""