Previous Up Next

17.1.1  Lagrange polynomial

The lagrange command finds the Lagrange polynomial which interpolates given data.

You can use the interp command as a synonym for lagrange.

Examples

lagrange([[1,3],[0,1]])

or:

lagrange([1,3],[0,1])
     
x−1
2
          

since x−1/2=0 for x=1 and x−1/2=1 for x=3.

factor(lagrange([1,2,3,4],[1,a,-2a,0],t))
     

t−4

a t2−30 a t+21 at2+5 t−6
6
          
Remark.

An attempted function definition such as f:=lagrange([1,2],[3,4],y) does not return a function but an expression with respect to y. To define f as a function, input:

f:=unapply(lagrange([1,2],[3,4],x),x)

Avoid f(x):=lagrange([1,2],[3,4],x) since then the Lagrange polynomial would be computed each time f is called (indeed in a function definition, the second member of the assignment is not evaluated). Note also that g(x):=lagrange([1,2],[3,4]) would not work since the default argument of lagrange would be global, hence not the same as the local variable used for the definition of g.


Previous Up Next