多项式插值法的Python程序

多项式插值法

多项式插值法的Python程序_第1张图片
多项式插值法的Python程序_第2张图片
多项式插值法的Python程序_第3张图片

例子

多项式插值法的Python程序_第4张图片
求t=16时,速度v的值

#多项式插值
from numpy import *
from numpy.linalg import *
x = [0, 10, 15, 20, 22.5, 30]
y = [0, 227.04, 362.78, 517.35, 602.97, 901.67]
n = len(x)
A = zeros((n, n))
for i in range(n):
    for j in range(n):
        A[i][j] = x[i]**(n-1-j)
B = zeros((n,1))
for b in range(len(y)):
    B[b][0] = y[b]
a =  solve(A, B)

def f(a,x):
    f = 0
    for m in range(len(a)):
        f += a[m][0] * x**(n-m-1)
    return f
print(f(a,16))

#误差
max = 0
for t in range(len(y)):
    e = abs(y[t] - f(a,x[t]))
    if e > max:
        max = e
    else:
        pass
print(max)

结果:

392.07057891555553
5.684341886080802e-14

你可能感兴趣的:(算法,python)