All published worksheets from http://sagenb.org
Image: ubuntu2004
Taylor Polynomials
Since Taylor polynomials concentrate their accuracy around a point (making sure the function and polynomial have the same derivatives up to the th derivative at that point), a Taylor polynomial may differ from a function significantly away from the point.
In the example below, we calculate and plot the Taylor polynomial approximating the function around the point . We also print the error in the approximation at , which is . Higher order Taylor polynomials usually give better approximations farther from the point, but in this case, as we increase the order of the approximation, the error gets much, much worse. Check this out by moving the slider to change the order of the approximation.
Lagrange Polynomials
Instead of concentrating our accuracy around a specific point, let's make sure that our approximation function is exact at a number of specified points. The Lagrange polynomial for data points is the unique th degree polynomial that passes through those points. We define the polynomial by taking linear combinations of functions so that we have the right values at the points we specify.
1 | 1 |
2 | \frac{1}{2} |
3 | \frac{1}{3} |
4 | \frac{1}{4} |
5 | \frac{1}{5} |
For each -coordinate, there is a polynomial that is 1 at that -coordinate and zero at all of the others. Here is the polynomial that is zero at and 1 at .
Here is a plot of all of the polynomials, one for each .
The Lagrange polynomial is just a linear combination of each of switching polynomials. This guarantees that we have the correct values at the points , , , , and .
Sage also has a builtin function to generate Lagrange polynomials as part of its polynomial objects.
Sage automatically evaluates a Polynomial object using a technique that is at least as efficient as Horner's form (if there are lots of zero coefficients, then it is more efficient by constructing a graph; type "sage.rings.polynomial.polynomial_compiled.CompiledPolynomialFunction??" to see the details and code). Here we evaluate the polynomial at "", and you can see that it looks like Horner's form is being used.
Let's plot this approximation to see how good it is.
These approximations can be pretty bad for some functions. A famous example is the Runge function
It's actually much better to take points that are projections of points evenly spaced around the unit circle (the roots of the "Chebyshev" polynomial). Try putting in "[cos(i/20*pi.n()) for i in [1,3,..,19]]" for the x_values above.
This work by Jason Grout is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.