如果给定函数 f(x) 的一阶信息,如何呈现多项式插值?

计算科学 插值
2021-12-06 13:06:08

假设,如何给出多项式插值使得 ? 以及如何进行错误分析?f(x1), f(x2), f(x3)p(x)p(x1)=f(x1), p(x2)=f(x2), p(x3)=f(x3)

1个回答

问题归结为线性系统方程的解。处获得函数值的一个方程,并从导数方程中获得另外三个方程。那么,三阶多项式是合适的。f(x1)

一种更暴力但不那么聪明的方法是使用计算机代数(例如 SymPy)来获得符号解决方案:

from sympy import *

N = 3 # degree of polynomial - for N + 1 'pieces of information', use a polynomial of degree N

known_x = symbols(["x_%i"%i for i in range(N)])  # create symbols for supporting points
known_derivs = symbols(["d_%i"%i for i in range(N)])  # create symbols for supporting points
y1 = Symbol("y_1")  # the single zeroth derivative we know

# create symbols for the independent variable and the coefficients:
x = Symbol("x")
a = [ Symbol("a_%i"%i) for i in range(N+1) ]

# generate polynomial and the derivative
p = sum([ coeff*x**i for i, coeff in enumerate(a) ])
p_prime = diff(p, x, 1)

# define the equations to solve:
eqns = [ p_prime.subs(x, xi) - d for xi, d in zip(known_x, known_derivs) ]  # equation for the first derivative
eqns.append(p.subs(x, known_x[0]) - y1)

# solve for the coefficients and print solution:
solution = solve(eqns, a)
pretty_sol = [ str(coeff) + " = " + str(value) + "\n"
               for coeff, value in zip(solution.keys(), solution.values()) ]
print("The coefficients are:\n" + "".join(pretty_sol))

这不是很优雅,但即使您为导数添加了另一个方程,这似乎也有效(因此可以对五个方程使用次数为“N = 4”的多项式)。