Hello internet, this is Oscar Veliz again showing you how to use Lagrange Polynomials
to approximate functions using polynomial interpolation.
Lagrange Polynomials are relatively simple to understand. If you have k different points
from some function (you don't have to know what the function is) you can write an approximation
of that function to give you good guesses around those points.
Lagrange Polynomial Form, looks like this: where you have f of x zero times x minus x
one times x minus x two all the way until x minus x n, divided by, in this case we're
gonna use x zero (since this is our first term), x zero minus x one times x zero minus
x two all the way until x zero minus x n. And in the next term we'll swap x zero with
x one for all of our points.
Now we'll try it with another example. In this case we're given an actual function but
we're gonna pretend like we don't know what the function is, and we'll have three points
from that function.
So given those three points, we'll plug them into our Lagrange Polynomial Form to come
up with this equation, which we can simplify further.
Now if we try to graph this Lagrange Polynomial we'll come up with a function that looks like
this; where if we overlap our original function they overlap exactly.
So in this case our Lagrange Polynomial ended up being exactly equal to our original function
but that is not always gonna be the case.
Why does this work? Well Lagrange Polynomial is designed that when whenever we have an
x that is equal to one of our starting points the other terms will simply cancel out to
zero. This way the Lagrange Polynomial is always guaranteed to go through the points
that we start with.
Now lets look at Lagrange Polynomials for another equation, this time the natural log
of x. So if we're given three points and we try to approximate it, it looks like our Lagrange
Polynomial is a really bad approximation of our function. But that is only because we're
evaluating it relatively far away from our starting points.
We should probably only use it closer to the points that we start with. In this case, Lagrange
Polynomial actually looks like it's a very good approximation of f of x. But how accurate
is it?
Well our error is our original function subtracted by our Lagrange Polynomial for all x's. And
if we do some fancy math we're able to come up with this formula. So if we use our function
from our previous example and evaluate the error at the point x is equal to one point
zero five, we'll simply plug everything into this equation. Notice though that this equation
also requires the third derivative at some point. That point is somewhere between our
x domain. So its going to be the point that maximizes that third derivative. And it turns
out that this is equal to point zero zero zero one two five, so the most that this equation
could be off is about point zero zero zero one two five which is actually fairly accurate.
Now we have another example of the function f of x is equal to cosine pi x. And I've drawn
in a Lagrange polynomial of degree four. I've also exaggerated the error a little bit just
to give you the idea that the Lagrange Polynomial isn't always going to be exact. So how accurate
would this Lagrange Polynomial be?
Find the error. Well, given our function f of x is equal to cosine pi x, and we know
that each x is point one distance apart, we're gonna use our original formula only this time
tweaked a little bit since we weren't given exact points, all we were given is that each
one is about point one apart. So if we try to maximize the error as a great approximation
then we can say that since each of them is point one apart, the most that each of those
points could be is about point four (so we're being a little lazy here) and although we
don't know a good approximation for this derivative's maximum point we always know that sine of
something isn't going to be greater than one. So in this case we can put one in there instead.
And this gives us an error of about point zero two six one.
Keep in mind that a very high order Lagrange Polynomial is pretty accurate around some
points but becomes a really bad approximation towards points further away from it. Instead
of high degree Lagrange Polynomials try to use piecewise Lagrange or even Splines for
better results. Thanks for watching.