Calculus II
Lesson 22: Approximating General Functions with Taylor Series
In the previous lesson, we saw how to approximate the sine and cosine functions arbitrarily accurately near by using polynomials. In this worksheet, we will see that the same approach can be used to approximate other functions, and that it can be used to get approximations near other points.
Approximations near 0
> restart;
Recall how the method works: we find an approximation to our function near 0, then we subtract that approximation from the function, and see how quickly the difference goes to 0 as by dividing by powers of .
> f := x -> sqrt(1 + x);
> plot(f, -1..1);
Since , our first approximation should be the constant function (0-th degree polynomial) 1:
> p0 := x -> 1;
> limit(f(x) - p0(x), x=0);
As expected, our approximation is good enough that the difference goes to 0 as , but how fast?
> limit((f(x) - p0(x))/x, x=0);
This limit shows that looks like when is small, so our next approximation is :
> p1 := x -> 1 + (1/2)*x;
Before computing further, let's plot our original function , together with the linear approximation :
> plot({f,p1}, -1..1);
Observe that the (graph of the) linear approximation is just the tangent line to the graph of at .
> limit((f(x) - p1(x))/x, x=0);
> limit((f(x) - p1(x))/x^2, x=0);
These limits show that we have improved the approximation, and tell us what the next term should be. We can repeat the process as many times as we like. Here are a few more steps.
> p2 := x -> 1 + (1/2)*x - (1/8)*x^2;
> limit((f(x) - p2(x))/x^3, x=0);
> p3 := x -> p2(x) + (1/16)*x^3;
> limit((f(x) - p3(x))/x^4, x=0);
> p4 := x -> p3(x) - (5/128)*x^4;
> p4(x);
How well has the method worked?
> plot({f,p2,p4}, -1..1);
There is nothing special about the function we chose: the method will work with any reasonably nice function. Here is one that is important in statistics (it is used to describe the so-called normal distribution ).
> g := x -> exp(-x^2/2);
> plot(g, -4..4, 0..2);
When , , so that will be our first approximation.
> q0 := x -> 1;
> limit(g(x) - q0(x), x=0);
> limit((g(x) - q0(x))/x, x=0);
> limit((g(x) - q0(x))/x^2, x=0);
These limits show that the constant 1 is also the linear approximation, as is clear from the graph of . The quadratic approximation adds a new term.
> q2 := x -> q0(x) - (1/2)*x^2;
> q2(x);
In fact, the only powers of which appear in approximations of near are even (why?). We will take advantage of this fact to avoid unnecessary computations below.
> limit((g(x) - q2(x))/x^4, x=0);
> q4 := x -> q2(x) + (1/8)*x^4;
> limit((g(x) - q4(x))/x^6, x=0);
> q6 := x -> q4(x) - (1/48)*x^6; q6(x);
> plot({g,q6}, -4..4, -2..2);
Approximations near points other than 0
As you can see from the examples above, our approximations are very good near , but not near other points. Suppose we wanted to approximate near , for example. Of course, at this expression is exactly equal to 2, so this should certainly be our first approximation.
> p0 := x -> 2;
What should the next approximation be? We want to know how fast goes to 0 as , so we need to compare it with other, simpler, expressions which are going to 0. When we were approximating near , we compared with powers of . Now we are working near so powers of don't go to 0---but powers of ( ) do!
> limit(f(x) - p0(x), x=3);
> limit((f(x) - p0(x))/x, x=3);
> limit((f(x) - p0(x))/(x-3), x=3);
> p1 := x -> p0(x) + (1/4)*(x-3);
Unfortunately, Maple insists on simplifying some of the polynomial approximations; we will see later how we can force it to write them in powers of . However it is written, though, we should observe that the linear approximation is once again the tangent line:
> plot({f,p1}, -1..5);
We can go on and construct the higher-order approximations in the usual way.
> limit((f(x) - p1(x))/(x-3)^2, x=3);
> p2 := x -> p1(x) - (1/64)*(x-3)^2;
> limit((f(x) - p2(x))/(x-3)^3, x=3);
> p3 := x -> p2(x) + (1/512)*(x-3)^3;
> limit((f(x) - p3(x))/(x-3)^4, x=3);
> p4 := x -> p3(x) - (5/16384)*(x-3)^4; p4(x);
> plot({f,p4}, -1..5);
Here are approximations for our other function near . The coefficients are not very nice, but you can see from the plots that the method is still working. (If you need decimal approximations, you can of course use evalf , but you should be sure to compute the "exact approximations" first and only use evalf at the very end of the procedure.)
> q0 := x -> g(-2); q0(x);
> limit(g(x) - q0(x), x=-2);
> l1 := limit((g(x) - q0(x))/(x + 2), x=-2);
Notice that the approximations will be in powers of ( ) = ( ) .
> q1 := x -> q0(x) + l1*(x+2); q1(x);
As always, the linear approximation gives us the tangent line:
> plot({g,q1}, -4..0);
> limit((g(x) - q1(x))/(x+2), x=-2);
> l2 := limit((g(x) - q1(x))/(x+2)^2, x=-2);
> q2 := x -> q1(x) + l2*(x+2)^2; q2(x);
> l3 := limit((g(x) - q2(x))/(x+2)^3, x=-2);
> q3 := x -> q2(x) + l3*(x+2)^3; q3(x);
> l4 := limit((g(x) - q3(x))/(x+2)^4, x=-2);
> q4 := x -> q3(x) + l4*(x+2)^4; q4(x);
> plot({g,q4}, -4..0, thickness=2);
>
The Taylor command
Now that we have seen how to produce the approximating polynomials for any function at any point, it is time to admit that Maple has a built-in command to do the computation. The polynomials we are producing are called Taylor polynomials, and the command taylor will produce them. To use this command, you have to give an expression to be expanded, the point near which you want the expansion, and the order of expansion required. ( Maple has rules about how many terms it gives in the expansion, and they are not always what you might expect, but if you ask for more terms you should get more terms.) You can omit the last parameter; Maple will then use its default Order variable to determine the number of terms.
> taylor(f(x), x=0, 6);
> taylor(f(x), x=0, 12);
> taylor(f(x), x=0);
> taylor(f(x), x=3);
> taylor(g(x), x=0);
> taylor(g(x), x=-2);
Notice that these are the same series we produced earlier in the worksheet. Here are a few more that should already be familiar to you.
> taylor(sin(x), x=0, 10);
> taylor(cos(x), x=0, 10);
> taylor(exp(x), x=0, 10);