鑑於your last question,我猜你的意思是一個Lagrange Polynomial,所以
LagrangePoly[pts_?MatrixQ, var_: x] /; MatchQ[Dimensions[pts], {_, 2}] :=
With[{k = Length[pts]}, Sum[pts[[j, 2]] Product[
If[j != m, (var - pts[[m, 1]])/(pts[[j, 1]] - pts[[m, 1]]), 1],
{m, 1, k}], {j, 1, k}]]
我們可以測試它反對切線˚F油膏,
In[2]:= points = Table[{x, Tan[x]}, {x, -1.2, 1.2, .2}]
Out[2]= {{-1.2, -2.57215}, {-1., -1.55741}, {-0.8, -1.02964},
{-0.6, -0.684137}, {-0.4, -0.422793}, {-0.2, -0.20271},
{0., 0.}, {0.2, 0.20271}, {0.4, 0.422793},
{0.6, 0.684137}, {0.8, 1.02964}, {1., 1.55741}, {1.2, 2.57215}}
In[3]:= Plot[Evaluate[Expand[LagrangePoly[points, x]]], {x, -1.2, 1.2},
Epilog -> Point[points]]
在這種情況下,插好,從原來的功能,最大偏差
In[4]:= FindMaximum[{Abs[Tan[x] - LagrangePoly[points, x]], -1.2<x<1.2}, x]
Out[4]= {0.000184412, {x -> 0.936711}}
還要注意,interpolating polynomials實際建成Mathematica:
In[5]:= InterpolatingPolynomial[points, x]-LagrangePoly[points, x]//Expand//Chop
Out[5]= 0
我不得不在比較之前擴展它們,因爲InterpolatingPolynomial
返回效率爲HornerForm
的結果,而我的LagrangePoly
以非常低效的形式返回。
馬修,你能澄清一下你的問題嗎?[對後人有用](http://xkcd.com/979/)? – Simon
Matthew:我會在你解決問題之前降低你的問題... – Simon
@Simon我會立即修復它! –