0% found this document useful (0 votes)
90 views

Math - List Manipulation in Mathematica Pertaining To Lagrange Interpolation Polynomials in Mathematica - Stack Overflow

The document discusses generating Lagrange interpolation polynomials in Mathematica. It provides code for a LagrangePoly function that takes a list of points and variable and returns the Lagrange interpolation polynomial. The function is tested on a list of points for the tangent function and works well, with small maximum deviation from the original function. Built-in InterpolatingPolynomial also gives the same result after expanding. The document also discusses how to get the length of a list and individual x and y values from a list of points using Length, Part, and Part shorthand in Mathematica.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Math - List Manipulation in Mathematica Pertaining To Lagrange Interpolation Polynomials in Mathematica - Stack Overflow

The document discusses generating Lagrange interpolation polynomials in Mathematica. It provides code for a LagrangePoly function that takes a list of points and variable and returns the Lagrange interpolation polynomial. The function is tested on a list of points for the tangent function and works well, with small maximum deviation from the original function. Built-in InterpolatingPolynomial also gives the same result after expanding. The document also discusses how to get the length of a list and individual x and y values from a list of points using Length, Part, and Part shorthand in Mathematica.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

22/09/2019 math - List Manipulation in Mathematica pertaining to Lagrange Interpolation Polynomials in Mathematica - Stack Overflow

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


that I can

get the length of that list


get the individual x and y values to manipulate

The list I am trying to manipulate can be seen below:

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

this question is a sort of a follow-up to the question seen here. I


eventually want to write my own function in mathematica that
generates the Lagrange Interpolation polynomial for a given set of
points

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

I need some way to access the points above so that I may utilize the
code below:

Sum[Subscript[y, j]*Product[If[j != m, (x - Subscript[x, m])/


(Subscript[x, j] - Subscript[x, m]), 1], {m, 0, k}], {j,

math wolfram-mathematica

share improve this question edited May 23 '17 at 12:03 asked Dec 7 '11 at 5:01
Community ♦ Matthew Kemnetz
1 1 545 1 10 28

add a comment

2 Answers active oldest votes

Given your last question, I'm guessing that you mean a Lagrange
Polynomial, so

4 LagrangePoly[pts_?MatrixQ, var_: x] /; MatchQ[Dimensions[pts],


With[{k = Length[pts]}, Sum[pts[[j, 2]] Product[
If[j != m, (var - pts[[m, 1]])/(pts[[j, 1]] - pts[[m, 1]]
{m, 1, k}], {j, 1, k}]]

We can test it against the tangent function,

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},
https://stackoverflow.com/questions/8410613/list-manipulation-in-mathematica-pertaining-to-lagrange-interpolation-polynomial 1/3
22/09/2019 math - List Manipulation in Mathematica pertaining to Lagrange Interpolation Polynomials in Mathematica - Stack Overflow

{-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,

In[3]:= Plot[Evaluate[Expand[LagrangePoly[points, x]]], {x, -1


Epilog -> Point[points]]

In this case, the interpolation is good, the maximum deviation from


the original function is

In[4]:= FindMaximum[{Abs[Tan[x] - LagrangePoly[points, x]], -1


Out[4]= {0.000184412, {x -> 0.936711}}

Also note, that interpolating polynomials are actually built into


Mathematica:

In[5]:= InterpolatingPolynomial[points, x]-LagrangePoly[points


Out[5]= 0

I had to expand them both before comparison,


since InterpolatingPolynomial returns the result in the
efficient HornerForm , while my LagrangePoly is returned in a very
inefficient form.

share improve this answer edited May 23 '17 at 12:01


Community ♦
1 1

answered Dec 7 '11 at 5:17


Simon
13.4k 3 33 93

add a comment

https://stackoverflow.com/questions/8410613/list-manipulation-in-mathematica-pertaining-to-lagrange-interpolation-polynomial 2/3
22/09/2019 math - List Manipulation in Mathematica pertaining to Lagrange Interpolation Polynomials in Mathematica - Stack Overflow

If you have a list l then you can get the length of that list by
using First@Dimensions@l . Assuming that the list has the
2 form {{x1,y1},{x2,y2},...} you can then get any of
the x and y values at position index just by
using Part[l,index] or its shorthand l[[index]] .

EDIT: If you want to do things the simple way you could conceivably
use Length@l for the length of the list.

https://stackoverflow.com/questions/8410613/list-manipulation-in-mathematica-pertaining-to-lagrange-interpolation-polynomial 3/3

You might also like