# 拉格朗日插值 python scipy

## 拉格朗日插值数学原理：

y=a0+a1x+a2x2+...+an1xn1

y1=a0+a1x1+a2x21+...+an1xn11

y2=a0+a1x2+a2x22+...+an1xn12

......

yn=a0+a1xn+a2x2n+...+an1xn1n

L(x)=y1(xx2)(xx3)...(xxn)(x1x2)(x1x3)...(x1xn)+y2(xx1)(xx3)...(xxn)(x2x1)(x2x3)...(x2xn)+......

+yn(xx1)(xx2)...(xxn1)(xnx2)(xnx3)...(xnxn1)

=i=0nyij=0,jixxixixj

f(3)=10,f(6)=8,f(9)=4$f(3)=10, f(6) = 8, f(9)=4$

l0(3)=(x6)(x9)(36)(39)$l_0(3)= \frac{(x-6)(x-9)}{(3-6)(3-9)}$
l1(6)=(x3)(x9)(63)(69)$l_1(6)= \frac{(x-3)(x-9)}{(6-3)(6-9)}$
l2(9)=(x3)(x6)(93)(96)$l_2(9)= \frac{(x-3)(x-6)}{(9-3)(9-6)}$

L(x)=f(3)l0(3)+f(6)l1(6)+f(9)l2(9)$L(x)=f(3)l_0(3)+f(6)l_1(6)+f(9)l_2(9)$
=10(x6)(x9)(36)(39)+8(x3)(x9)(63)(69)+4(x3)(x6)(93)(96)$=10 \frac{(x-6)(x-9)}{(3-6)(3-9)}+8 \frac{(x-3)(x-9)}{(6-3)(6-9)}+4 \frac{(x-3)(x-6)}{(9-3)(9-6)}$
=x2+3x+909$=\frac{-x^2+3x+90}{9}$

## python 实现

y=a0+a1x+a2x2

from scipy.interpolate  import lagrange
x = [3, 6, 9]
y = [10, 8, 4]
lagrange(x,y)
#poly1d([ -0.11111111,   0.33333333,  10.        ])

lagrange(x, y)(10)
# 2.222222