# Lagrange polynomial: lagrange interp

### 5.27.29 Lagrange polynomial: lagrange interp

The lagrange command finds the Lagrange polynomial which interpolates given data.
interp is a synonym for lagrange.

- lagrange takes two mandatory arguments and one optional argument:
  - l1 and l2, two lists of the same size. These can be given as a matrix with two rows.
    The first list (resp. row) corresponds to the abscissa values xk (k=1..n), and the second list (resp. row) corresponds to ordinate values yk (k=1..n).
  - Optionally x, the name of a variable (by default x).
- lagrange(l1,l2 ⟨,x⟩) returns a polynomial expression P with respect to x of degree n-1, such that P(xi)=yi.

#### Examples.

- Input:
  lagrange([[1,3],[0,1]])
  or:
  lagrange([1,3],[0,1])
  Output:

  x−1
  2

  since x−1/2=0 for x=1 and x−1/2=1 for x=3.
- Input:
  lagrange([1,3],[0,1],y)
  Output:

  y−1
  2

Warning.
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.