As everybody knows, the formula for Lagrangian interpolation though points , where all the values are distinct, is given by

It is easy to see that this works, as the product is equal to zero if and is equal to one if .

This isn’t particularly easy to program, and in fact methods such as Newton’s divided differences or Neville’s algorithm are used to obtain the polynomial.

But a method introduced by Gabor Szegö provides a very simple form of Lagrange’s polynomial.

Start with defining

.

Then

This is obtained immediately from the product rule for arbitrary products, and using the fact that the derivative of each individual term of is one. This means that in particular

because all other terms of contain an term and therefore produce zero for .

This means that the coefficient of in the Lagrangian polynomial can be written as

and so the polynomial can be written as

.

Here is an example, with and . First, Maxima:

`(%o1) xs:[-2,1,3,4];`

(%o2) ys:[-3,-27,-23,3];

(%o3) define(pi(x),product(x-xs[i],i,1,4));

(%o4) define(pid(x),diff(pi(x),x));

(%o5) define(P(x),sum(pi(x)/(x-xs[i])/pid(xs[i])*ys[i],i,1,4));

(%o6) ratsimp(P(x));

Now Sage:

sage: xs = [-2,1,3,4]
sage: ys = [-3, -27, -23, 3]
sage: pi(x) = prod(x-i for i in xs); pi(x)
(x - 4)*(x - 3)*(x - 1)*(x + 2)
sage: pid(x) = diff(pi(x),x);
sage: P(x) = sum(pi(x)/(x-i)/pid(i)*j for (i,j) in zip(xs,ys))
sage: P(x).collect(x)
x^3 - 11*x - 17

And finally, on the TI-nspire CAS calculator:

Note that all the mathematical symbols and programming constructs are available through the calculator’s menus.

Notice that this approach requires no programming with loops or conditions at all – it’s all done with simple sums and products.

### Like this:

Like Loading...

*Related*

Filed under: Computation, Maths teaching, Maxima, Sage

Natalie Schneider, on September 21, 2012 at 4:24 am said:Placing this in a program editor would make it even easier to use in the Nspire CAS. Thanks for the syntax of how to do it though.

use instead a program editor, with the two inputs as the lists

also use n:= count(xs) to determine the length rather than typing a new length every time.

for some reason when I used the input variables directly it wouldn’t work, so I redefined them and it did. Maybe you can make it better, but here is the code for that program which will work on any two lists of the same length (Crashes if y is shorter)

-code

Define laginterp(xls,yls)=

Prgm

:xs:=xls

:ys:=yls

:n:=count(xs)

:Define q(x)=∏(x-xs[i],i,1,n)

:qs:=seq(d/dx(q(x),x)|x=xs[i],i,1,n)

:Define p(x)=∑(((q(x))/((x-xs[i])*qs[i]))*ys[i],i,1,n)

:Disp p(x)

:EndPrgm