17.1.1 Lagrange polynomial
The lagrange command
finds the Lagrange polynomial which interpolates given data.
-
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(x) of degree n−1
such that P(xi)=yi.
You can use the interp command as a synonym for lagrange.
Examples
or:
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 | ⎞
⎠ | ⎛
⎝ | 9 a t2−30 a t+21 a−t2+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.