Sec13.9Taylor.mws

Taylor polynomials in several variables

Worksheet by Mike May, S.J.- maymk@slu.edu

> restart; with(plots):

Warning, the name changecoords has been redefined

Review of Taylor polynomials in one variable

Recall from calculus of one variable that we can approximate a nice function at a point x=a by looking at Taylor polynomials of the function. We get the nth degree part of the approximation near x=a by evaluating the nth derivative at that point and multiplying by (x-a)^n/n! .

Consider an example:

> f := x -> sin(x) + cos(2*x); a:= Pi/3;

f := proc (x) options operator, arrow; sin(x)+cos(2...

a := 1/3*Pi

We want to write functions to take the derivatives, evaluate them at the given point, multiply by the correct power of (x-a), and divide by the correct constant..

> LinTerm1 := (func, a) -> limit(diff(func,x), x=a)*(x-a);
QuadTerm1 := (func, a) -> limit(diff(func,x, x), x=a)*(x-a)^2/2;
CubeTerm1 := (func, a) -> limit(diff(func,x, x, x), x=a)*(x-a)^3/6;
QuarTerm1 := (func, a) -> limit(diff(func,x, x, x, x), x=a)*(x-a)^4/24;

LinTerm1 := proc (func, a) options operator, arrow;...

QuadTerm1 := proc (func, a) options operator, arrow...

CubeTerm1 := proc (func, a) options operator, arrow...

QuarTerm1 := proc (func, a) options operator, arrow...

Now we evaluate for the given function and point.

> Deg0TP := f(a);
Deg1TP := Deg0TP + LinTerm1(f(x),a);
Deg2TP := Deg1TP + QuadTerm1(f(x),a);
Deg3TP := Deg2TP + CubeTerm1(f(x),a);
Deg4TP := Deg3TP + QuarTerm1(f(x),a);

Deg0TP := 1/2*sqrt(3)-1/2

Deg1TP := 1/2*sqrt(3)-1/2+(1/2-sqrt(3))*(x-1/3*Pi)

Deg2TP := 1/2*sqrt(3)-1/2+(1/2-sqrt(3))*(x-1/3*Pi)+...

Deg3TP := 1/2*sqrt(3)-1/2+(1/2-sqrt(3))*(x-1/3*Pi)+...

Deg4TP := 1/2*sqrt(3)-1/2+(1/2-sqrt(3))*(x-1/3*Pi)+...

Visually we notice that we get better and better appoximations by adding in more and more terms.

> del := Pi;
plot({f(x), Deg0TP, Deg1TP, Deg2TP, Deg3TP, Deg4TP},
x=a-del..a+del, y=-5..5, axes=normal);

del := Pi

[Maple Plot]

Exercise -

1) For each Taylor polynomial approximations above, give an interval where you think it is a good approximation to the original function.

>

To check the closeness of an approximation we look at the difference of the function and the approximation.

> del := .6;
plot({Deg1TP-f(x), Deg2TP-f(x), Deg3TP-f(x),Deg4TP-f(x)},
x=a-del..a+del, y=-0.1..0.1, axes=normal);

del := .6

[Maple Plot]

Exercise -

2) For each of the Taylor approximations above, give an interval where the error is less than .02.

>

If we want numbers rather than pictures, we can evaluate the Taylor polynomials with the subs command. We also use the evalf command to convert the answer to a decimal value.

> f(1.4);
subs(x=1.4, Deg3TP);
evalf(subs(x=1.4, Deg3TP));
f(1.4) - evalf(subs(x=1.4, Deg3TP));

.432273893e-1

1/2*sqrt(3)-1/2+(1/2-sqrt(3))*(1.4-1/3*Pi)+1/2*(-1/...

.4897465419e-1

-.574726489e-2

Exercise -

3) Find the values of f(x) and the 4th degree Taylor polynomial above at x=0.7. Find the value of the error in the approximation.

>

We will be interestered in using Taylor polynomials when we either have information about the function and its derivatives at a point but no formula (Hence the Taylor polynomial is the best description of the function we have) or when we want a numerical value where a symbolic manipulation is hard or impossible for the given function but easy for a polynomial approximation. (For example, finding int(sin(x^4),x = 0 .. 1) .)

>

Generalizing Taylor Polynomials to functions of two variables

To generalize to functions of two variables, we need to take into account all the partial derivatives. We have 2 first partials, 4 second partials, 8 third partials, and so on. We are helped out by the fact that for nice functions the mixed partials are equal, so we can simplify the computations. That leads to the following procedures for computing the linear through quadratic terms.

> LinTerm2 := (func,a,b) -> limit(limit(diff(func,x),x=a),y=b)*(x-a) +
limit(limit(diff(func,y),x=a),y=b)*(y-b);
QuadTerm2:= (func,a,b) ->
limit(limit(diff(func,x,x),x=a),y=b)*(x-a)^2/2 +
limit(limit(diff(func,x,y),x=a),y=b)*(x-a)*(y-b) +
limit(limit(diff(func,y,y),x=a),y=b)*(y-b)^2/2;
CubeTerm2 := (func,a,b) ->
limit(limit(diff(func,x,x,x),x=a),y=b)*(x-a)^3/6 +
limit(limit(diff(func,x,x,y),x=a),y=b)*(x-a)^2*(y-b)/2 +
limit(limit(diff(func,x,y,y),x=a),y=b)*(x-a)*(y-b)^2/2 +
limit(limit(diff(func,y,y,y),x=a),y=b)*(y-b)^3/6;

LinTerm2 := proc (func, a, b) options operator, arr...

QuadTerm2 := proc (func, a, b) options operator, ar...
QuadTerm2 := proc (func, a, b) options operator, ar...

CubeTerm2 := proc (func, a, b) options operator, ar...
CubeTerm2 := proc (func, a, b) options operator, ar...

(The rule for the factor added to each derivative is, if we take the partial n times with respect to x and the partial m times with respect to y then we multiply by (x-a)^n*(y-b)^m/(n!*m!) . Note that this becomes the familiar factor if we only have one variable.)

We start with a function and a point.

> g := (x,y) ->sin(x+3*y)+cos(2*x-y); a:= Pi/2; b:=Pi/2;

g := proc (x, y) options operator, arrow; sin(x+3*y...

a := 1/2*Pi

b := 1/2*Pi

Next we compute the parts of the polynomial expression

> Deg0TP := g(a,b);
Deg1TP := Deg0TP + LinTerm2(g(x,y),a,b);
Deg2TP := Deg1TP + QuadTerm2(g(x,y),a,b);
Deg3TP := Deg2TP + CubeTerm2(g(x,y),a,b);

Deg0TP := 0

Deg1TP := -x-3/2*Pi+4*y

Deg2TP := -x-3/2*Pi+4*y

Deg3TP := -x-3/2*Pi+4*y+7/6*(x-1/2*Pi)^3-7/2*(x-1/2...

> del := Pi/2:

> deg0plot := plot3d(g(x,y),
x=a-del..a+del, y=b-del..b+del, view=-5..5, style=patch, color=red):

> deg1plot := plot3d(Deg1TP,
x=a-del..a+del, y=b-del..b+del, view=-5..5, style=patch, color=blue):

> deg2plot := plot3d(Deg2TP,
x=a-del..a+del, y=b-del..b+del, view=-5..5, style=patch, color=green):

> deg3plot := plot3d(Deg3TP,
x=a-del..a+del, y=b-del..b+del, view=-5..5, style=patch, color=gold):

> display( deg0plot, deg1plot,deg2plot,deg3plot);

[Maple Plot]

We can make the graph clearer by specifying colors for the graphs

> del := .8:
surf3 := plot3d(Deg3TP,
x=a-del..a+del, y=b-del..b+del, color=blue):
surf2 := plot3d(Deg2TP,
x=a-del..a+del, y=b-del..b+del, color=green):
surf1 := plot3d(Deg1TP,
x=a-del..a+del, y=b-del..b+del, color=red):
surf := plot3d(g(x,y), x=a-del..a+del, y=b-del..b+del, color=orange):
display3d({surf, surf1, surf2, surf3}, axes=boxed,
view=-5..5,style=patch);

[Maple Plot]

As above, we are interested if plotting an error term to see where the approxiamtion is close enough and being able to evaluate at a specified point.

> plot3d(g(x,y)-Deg3TP, x=a-del..a+del, y=b-del..b+del,
view=-0.1..0.1, axes=boxed);
evalf(subs({x=1.7, y=1.4}, Deg3TP));
g(1.7,1.4);

[Maple Plot]

-.7898340528

-.7900235013

Exercise -

4) For what region is Deg2TP a good approximation of g(x,y)?

>

5) Find the error in the approximation Deg3TP gives of g(x,y) at (1.5, 1.6).

>

Since the homework in the book asks for evaluation of linear and quadratic Taylor polynomials, it seems worthwhile to put the code needed in one section so the students can easily use Maple for those problems.

> g := (x,y) ->sin(x+3*y)+cos(2*x-y); a:= Pi/2; b:= Pi/2;
Deg0TP := g(a,b);
Deg1TP := Deg0TP + LinTerm2(g(x,y),a,b);
Deg2TP := Deg1TP + QuadTerm2(g(x,y),a,b);
evalf(subs({x=a+0.1, y=b-0.1}, Deg2TP));
g(a+0.1, b-0.1);

g := proc (x, y) options operator, arrow; sin(x+3*y...

a := 1/2*Pi

b := 1/2*Pi

Deg0TP := 0

Deg1TP := -x-3/2*Pi+4*y

Deg2TP := -x-3/2*Pi+4*y

-.5

-.4941895375

>

Generating Taylor Polynomials of arbitrary degree

Review of the one variable case

With a bit of manipulation we can get Maple to compute Taylor polynomials of arbitrary degree. We want to be able to compute the nth degree part for arbitrary n, and then to add up all the part up to specified degree.

> degpart1 := (func,xval,deg) ->
limit(diff(func,x$deg),x=xval)*(x-xval)^deg/(deg!);
Taypoly1 := proc(func,xval,maxdeg)
local temp, deg:
temp:=subs(x=xval,func):
for deg from 1 to maxdeg do
temp := temp + degpart1(func,xval,deg):
od:
end:

degpart1 := proc (func, xval, deg) options operator...

This lets us compare the function at the beginning with the 15th degree Taylor polynomial.

> f := x -> sin(x) + cos(2*x); a:= 0;
del := 2*Pi;
f(x);
Taypoly1(f(x),a,15);
plot({f(x), Taypoly1(f(x),a,15)},
x=a-del..a+del, y=-5..5, axes=normal);
plot({f(x) - Taypoly1(f(x),a,15)},
x=a-del..a+del, y=-0.1..0.1, axes=normal);

f := proc (x) options operator, arrow; sin(x)+cos(2...

a := 0

del := 2*Pi

sin(x)+cos(2*x)

sin(0)+cos(0)+x-2*x^2-1/6*x^3+2/3*x^4+1/120*x^5-4/4...
sin(0)+cos(0)+x-2*x^2-1/6*x^3+2/3*x^4+1/120*x^5-4/4...

[Maple Plot]

[Maple Plot]

Once again, we use the subs and evalf commands if we want to evaluate at a point.

> evalf(subs(x=0.3,Taypoly1(f(x),a,15)));
f(0.1);

1.120855822

1.079899994

Exercises:

6) What degree Taylor polynomial do we need to use for the graph of the function above and the graph of its Taylor polynomial polynomial to appear the same betwee -5 and 5? What error do you allow in two graphs that you say look the same?

>

7) What degree Taylor polynomial do we need to use for the graph of the function sin(3*x)-cos(x/2) and the graph of its Taylor polynomial to appear the same betwee 0 and 6? (The Taylor approxiamtion should be centered at x=3.)

>

8) Evaluate the function sin(3*x)-cos(x/2) and its 20 degree Taylor polynomial approxiamtion ax x=4.

>

Generalizing to two variables

We can use the same approach for functions of 2 variables. We first create the functions to produce the Taylor approximations.

> degpart2 := proc(func,xval,yval,deg)
local temp, i:
temp := 0:
for i from 0 to deg do
temp := temp +
limit(limit(diff(func,x$i,y$(deg-i)),x=xval),y=yval)
*(x-xval)^i*(y-yval)^(deg-i)/((deg-i)!*i!):
od:
end:
Taypoly2 := proc(func,xval,yval,maxdeg)
local temp, deg:
temp := simplify(subs({x=xval, y=yval}, func)):
for deg from 1 to maxdeg do
temp := temp + degpart2(func,xval,yval,deg):
od:
end:

Now we would like to look at the 5th degree Taylor polynomail approximation to the function g(x,y) that we started with.

Once we have the polynomial we would like to compare the graphs of the original function with its Taylor polynomial.

> g := (x,y) ->sin(x+3*y)+cos(2*x-y): a:= Pi/2: b:=Pi/2:
del := 1:
Taypoly2(g(x,y),a,b,5);
surf1 := plot3d(Taypoly2(g(x,y),a,b,5),
x=a-del..a+del, y=b-del..b+del, color=red):
surf := plot3d(g(x,y), x=a-del..a+del, y=b-del..b+del, color=blue):
display3d({surf, surf1}, axes=boxed, view=-3..3,style=patch);

-x-3/2*Pi+4*y+7/6*(x-1/2*Pi)^3-7/2*(x-1/2*Pi)^2*(y-...
-x-3/2*Pi+4*y+7/6*(x-1/2*Pi)^3-7/2*(x-1/2*Pi)^2*(y-...

[Maple Plot]

It is worthwhile to look at the error, or difference between the function and its Taylor polynomial approximation. We are interested in the region where the error is no more than 0.1.

>

> g := (x,y) ->sin(x+3*y)+cos(2*x-y): a:= Pi/2: b:=Pi/2:
del := 1: maxerr := 0.1:
plot3d(g(x,y) - Taypoly2(g(x,y),a,b,5), x=a-del..a+del,
y=b-del..b+del, axes=boxed, view=-maxerr..maxerr, style=patch);

[Maple Plot]

The shape of the error is fairly typical. The error is close to 0 in a region around the point of contact, then the error blows up. Changing either ther degree of the approximation or the definition of acceptable error simply changes the size of the good patch.

Exercise:

9) What degree Taylor approximation do we need to use if we want to approxiamte the function sin(x+3*y)+cos(2*x-y) on a for all points within 1 of ( Pi/2 , Pi/2 ) with an accuracy of 0.1?

>

It seems worthwhile to see that the Taylor polynomials in two variables wrap down to the fucntion in the same way that they do with functions of one variable. Since this often causes memory problems the animation will be done as a separate worksheet.

Please close this worksheet and run the worksheet Sec13.9-3dTaylAnim.mws.

>