列表操作Mathematica中Mathematica中有关拉格朗日插值多项式(List Manip

2019-09-26 21:38发布

我想用它传递给函数以这样的方式列表,我可以

  • 获取列表的长度
  • 获得个人x和y的值来操纵

我试图操纵该列表可以看到下面:

dataTan = Table[{x, Tan[x]}, {x, -1.5, 1.5, .75}];

这个问题是一个不大不小的后续看到的问题在这里 。 我终究要在写数学我自己的函数产生的拉格朗日插值多项式对于一个给定的点

 {{x0, y0}, ... , {xn, yn}}

我需要一些方法来访问上面,这样我可以使用下面的代码点:

Sum[Subscript[y, j]*Product[If[j != m, (x - Subscript[x, m])/
       (Subscript[x, j] - Subscript[x, m]), 1], {m, 0, k}], {j, 0, k}]

Answer 1:

鉴于你的最后一个问题 ,我猜你的意思是一个拉格朗日多项式 ,所以

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}]]

我们可以测试它反对正切函数,

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}}

还要注意, 插值多项式实际上是内置的Mathematica:

In[5]:= InterpolatingPolynomial[points, x]-LagrangePoly[points, x]//Expand//Chop
Out[5]= 0

我比较之前扩大他们两个,因为InterpolatingPolynomial返回有效结果HornerForm ,而我的LagrangePoly是一个非常低效的形式返回。



Answer 2:

如果你有一个清单l ,那么你可以通过获取列表的长度First@Dimensions@l 。 假设列表的形式{{x1,y1},{x2,y2},...}那么你可以得到任何的xy值在位置index仅仅通过使用Part[l,index]或它的简写l[[index]]

编辑:如果你想要做的事情,你可以想见,使用简单的方法Length@l为列表的长度。



文章来源: List Manipulation in Mathematica pertaining to Lagrange Interpolation Polynomials in Mathematica