Application Center - Maplesoft

App Preview:

Approximating general functions

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

L22-approxGeneral.mws

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 x = 0 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 proc (x) options operator, arrow; 0 end proc by dividing by powers of x .

> f := x -> sqrt(1 + x);

f := proc (x) options operator, arrow; sqrt(1+x) en...

> plot(f, -1..1);

[Maple Plot]

Since f(0) = 1 , our first approximation should be the constant function (0-th degree polynomial) 1:

> p0 := x -> 1;

p0 := 1

> limit(f(x) - p0(x), x=0);

0

As expected, our approximation is good enough that the difference goes to 0 as proc (x) options operator, arrow; 0 end proc , but how fast?

> limit((f(x) - p0(x))/x, x=0);

1/2

This limit shows that f(x)-p0(x) looks like x/2 when x is small, so our next approximation is p0(x)+x/2 :

> p1 := x -> 1 + (1/2)*x;

p1 := proc (x) options operator, arrow; 1+1/2*x end...

Before computing further, let's plot our original function f , together with the linear approximation p1 :

> plot({f,p1}, -1..1);

[Maple Plot]

Observe that the (graph of the) linear approximation is just the tangent line to the graph of f at x = 0 .

> limit((f(x) - p1(x))/x, x=0);

0

> limit((f(x) - p1(x))/x^2, x=0);

-1/8

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;

p2 := proc (x) options operator, arrow; 1+1/2*x-1/8...

> limit((f(x) - p2(x))/x^3, x=0);

1/16

> p3 := x -> p2(x) + (1/16)*x^3;

p3 := proc (x) options operator, arrow; p2(x)+1/16*...

> limit((f(x) - p3(x))/x^4, x=0);

-5/128

> p4 := x -> p3(x) - (5/128)*x^4;

p4 := proc (x) options operator, arrow; p3(x)-5/128...

> p4(x);

1+1/2*x-1/8*x^2+1/16*x^3-5/128*x^4

How well has the method worked?

> plot({f,p2,p4}, -1..1);

[Maple Plot]

There is nothing special about the function f 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);

g := proc (x) options operator, arrow; exp(-1/2*x^2...

> plot(g, -4..4, 0..2);

[Maple Plot]

When x = 0 , g(x) = 1 , so that will be our first approximation.

> q0 := x -> 1;

q0 := 1

> limit(g(x) - q0(x), x=0);

0

> limit((g(x) - q0(x))/x, x=0);

0

> limit((g(x) - q0(x))/x^2, x=0);

-1/2

These limits show that the constant 1 is also the linear approximation, as is clear from the graph of g . The quadratic approximation adds a new term.

> q2 := x -> q0(x) - (1/2)*x^2;

q2 := proc (x) options operator, arrow; q0(x)-1/2*x...

> q2(x);

1-1/2*x^2

In fact, the only powers of x which appear in approximations of g near x = 0 are even (why?). We will take advantage of this fact to avoid unnecessary computations below.

> limit((g(x) - q2(x))/x^4, x=0);

1/8

> q4 := x -> q2(x) + (1/8)*x^4;

q4 := proc (x) options operator, arrow; q2(x)+1/8*x...

> limit((g(x) - q4(x))/x^6, x=0);

-1/48

> q6 := x -> q4(x) - (1/48)*x^6; q6(x);

q6 := proc (x) options operator, arrow; q4(x)-1/48*...

1-1/2*x^2+1/8*x^4-1/48*x^6

> plot({g,q6}, -4..4, -2..2);

[Maple Plot]

Approximations near points other than 0

> restart;

As you can see from the examples above, our approximations are very good near x = 0 , but not near other points. Suppose we wanted to approximate sqrt(1+x) near x = 3 , for example. Of course, at x = 3 this expression is exactly equal to 2, so this should certainly be our first approximation.

> f := x -> sqrt(1 + x);

f := proc (x) options operator, arrow; sqrt(1+x) en...

> p0 := x -> 2;

p0 := 2

What should the next approximation be? We want to know how fast f(x)-2 goes to 0 as proc (x) options operator, arrow; 3 end proc , so we need to compare it with other, simpler, expressions which are going to 0. When we were approximating near x = 0 , we compared with powers of x . Now we are working near x = 3 so powers of x don't go to 0---but powers of ( x-3 ) do!

> limit(f(x) - p0(x), x=3);

0

> limit((f(x) - p0(x))/x, x=3);

0

> limit((f(x) - p0(x))/(x-3), x=3);

1/4

> p1 := x -> p0(x) + (1/4)*(x-3);

p1 := proc (x) options operator, arrow; p0(x)+1/4*x...

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 x-3 . However it is written, though, we should observe that the linear approximation is once again the tangent line:

> plot({f,p1}, -1..5);

[Maple Plot]

We can go on and construct the higher-order approximations in the usual way.

> limit((f(x) - p1(x))/(x-3)^2, x=3);

-1/64

> p2 := x -> p1(x) - (1/64)*(x-3)^2;

p2 := proc (x) options operator, arrow; p1(x)-1/64*...

> limit((f(x) - p2(x))/(x-3)^3, x=3);

1/512

> p3 := x -> p2(x) + (1/512)*(x-3)^3;

p3 := proc (x) options operator, arrow; p2(x)+1/512...

> limit((f(x) - p3(x))/(x-3)^4, x=3);

-5/16384

> p4 := x -> p3(x) - (5/16384)*(x-3)^4; p4(x);

p4 := proc (x) options operator, arrow; p3(x)-5/163...

5/4+1/4*x-1/64*(x-3)^2+1/512*(x-3)^3-5/16384*(x-3)^...

> plot({f,p4}, -1..5);

[Maple Plot]

Here are approximations for our other function near x = -2 . 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.)

> g := x -> exp(-x^2/2);

g := proc (x) options operator, arrow; exp(-1/2*x^2...

> q0 := x -> g(-2); q0(x);

q0 := proc (x) options operator, arrow; g(-2) end p...

exp(-2)

> limit(g(x) - q0(x), x=-2);

0

> l1 := limit((g(x) - q0(x))/(x + 2), x=-2);

l1 := 2*exp(-2)

Notice that the approximations will be in powers of ( x-(-2) ) = ( x+2 ) .

> q1 := x -> q0(x) + l1*(x+2); q1(x);

q1 := proc (x) options operator, arrow; q0(x)+l1*(x...

exp(-2)+2*exp(-2)*(x+2)

As always, the linear approximation gives us the tangent line:

> plot({g,q1}, -4..0);

[Maple Plot]

> limit((g(x) - q1(x))/(x+2), x=-2);

0

> l2 := limit((g(x) - q1(x))/(x+2)^2, x=-2);

l2 := 3/2*exp(-2)

> q2 := x -> q1(x) + l2*(x+2)^2; q2(x);

q2 := proc (x) options operator, arrow; q1(x)+l2*(x...

exp(-2)+2*exp(-2)*(x+2)+3/2*exp(-2)*(x+2)^2

> l3 := limit((g(x) - q2(x))/(x+2)^3, x=-2);

l3 := 1/3*exp(-2)

> q3 := x -> q2(x) + l3*(x+2)^3; q3(x);

q3 := proc (x) options operator, arrow; q2(x)+l3*(x...

exp(-2)+2*exp(-2)*(x+2)+3/2*exp(-2)*(x+2)^2+1/3*exp...

> l4 := limit((g(x) - q3(x))/(x+2)^4, x=-2);

l4 := -5/24*exp(-2)

> q4 := x -> q3(x) + l4*(x+2)^4; q4(x);

q4 := proc (x) options operator, arrow; q3(x)+l4*(x...

exp(-2)+2*exp(-2)*(x+2)+3/2*exp(-2)*(x+2)^2+1/3*exp...

> plot({g,q4}, -4..0, thickness=2);

[Maple Plot]

>

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.

> f := x -> sqrt(1 + x);

f := proc (x) options operator, arrow; sqrt(1+x) en...

> taylor(f(x), x=0, 6);

series(1+1/2*x-1/8*x^2+1/16*x^3-5/128*x^4+7/256*x^5...

> taylor(f(x), x=0, 12);

series(1+1/2*x-1/8*x^2+1/16*x^3-5/128*x^4+7/256*x^5...

> taylor(f(x), x=0);

series(1+1/2*x-1/8*x^2+1/16*x^3-5/128*x^4+7/256*x^5...

> taylor(f(x), x=3);

series(2+1/4*(x-3)-1/64*(x-3)^2+1/512*(x-3)^3-5/163...

> g := x -> exp(-x^2/2);

g := proc (x) options operator, arrow; exp(-1/2*x^2...

> taylor(g(x), x=0);

series(1-1/2*x^2+1/8*x^4+O(x^6),x,6)

> taylor(g(x), x=-2);

series(exp(-2)+2*exp(-2)*(x+2)+3/2*exp(-2)*(x+2)^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);

series(1*x-1/6*x^3+1/120*x^5-1/5040*x^7+1/362880*x^...

> taylor(cos(x), x=0, 10);

series(1-1/2*x^2+1/24*x^4-1/720*x^6+1/40320*x^8+O(x...

> taylor(exp(x), x=0, 10);

series(1+1*x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5+1/7...