List of manipulations in Mathematica related to Lagrangian interpolation polynomials in mathematics

I am trying to use a list that is passed to a function in such a way that I can

  • get the length of this list
  • get separate x and y values ​​for control

The list I'm trying to manipulate can be seen below:

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

this question is a kind of continuation of the question under consideration. Ultimately, I want to write my own function in mathematics that generates a Lagrange polynomial for interpolation for a given set of points

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

I need a way to access the points above so that I can use the following code:

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}]
+1
source share
2 answers

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

,

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

tan

,

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 .

+4

l, , First@Dimensions@l. , {{x1,y1},{x2,y2},...}, x y index Part[l,index] l[[index]].

EDIT: - , Length@l .

+2

All Articles