Maxima Function
lagrange (points)
lagrange(points,option)
Computes the polynomial interpolation by the Lagrangian method. Argument points must be either:
a two column matrix, p:matrix([2,4],[5,6],[9,3])
,
a list of pairs, p: [[2,4],[5,6],[9,3]]
,
a list of numbers, p: [4,6,3]
, in which case the abscissas will be assigned automatically to 1, 2, 3, etc.
In the first two cases the pairs are ordered with respect to the first coordinate before making computations.
With the option argument it is possible to select the name for the independent variable, which is 'x
by default; to define another one, write something like varname='z
.
Note that when working with high degree polynomials, floating point evaluations are inestable.
Examples:
(%i1) load(interpol)$ (%i2) p:[[7,2],[8,2],[1,5],[3,2],[6,7]]$ (%i3) lagrange(p); 4 3 2 73 x 701 x 8957 x 5288 x 186 (%o3) ----- - ------ + ------- - ------ + --- 420 210 420 105 5 (%i4) f(x):=''%; 4 3 2 73 x 701 x 8957 x 5288 x 186 (%o4) f(x) := ----- - ------ + ------- - ------ + --- 420 210 420 105 5 (%i5) /* Evaluate the polynomial at some points */ map(f,[2.3,5/7,%pi]); 919062 (%o5) [- 1.567534999999992, ------, 84035 4 3 2 73 %pi 701 %pi 8957 %pi 5288 %pi 186 ------- - -------- + --------- - -------- + ---] 420 210 420 105 5 (%i6) %,numer; (%o6) [- 1.567534999999992, 10.9366573451538, 2.89319655125692] (%i7) load(draw)$ /* load draw package */ (%i8) /* Plot the polynomial together with points */ draw2d( color = red, key = "Lagrange polynomial", explicit(f(x),x,0,10), point_size = 3, color = blue, key = "Sample points", points(p))$ (%i9) /* Change variable name */ lagrange(p, varname=w); 4 3 2 73 w 701 w 8957 w 5288 w 186 (%o9) ----- - ------ + ------- - ------ + --- 420 210 420 105 5