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
.
Consider an example:
>
f := x > sin(x) + cos(2*x); a:= Pi/3;
We want to write functions to take the derivatives, evaluate them at the given point, multiply by the correct power of (xa), and divide by the correct constant..
>
LinTerm1 := (func, a) > limit(diff(func,x), x=a)*(xa);
QuadTerm1 := (func, a) > limit(diff(func,x, x), x=a)*(xa)^2/2;
CubeTerm1 := (func, a) > limit(diff(func,x, x, x), x=a)*(xa)^3/6;
QuarTerm1 := (func, a) > limit(diff(func,x, x, x, x), x=a)*(xa)^4/24;
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);
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=adel..a+del, y=5..5, axes=normal);
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({Deg1TPf(x), Deg2TPf(x), Deg3TPf(x),Deg4TPf(x)},
x=adel..a+del, y=0.1..0.1, axes=normal);
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));
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
.)
>
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)*(xa) +
limit(limit(diff(func,y),x=a),y=b)*(yb);
QuadTerm2:= (func,a,b) >
limit(limit(diff(func,x,x),x=a),y=b)*(xa)^2/2 +
limit(limit(diff(func,x,y),x=a),y=b)*(xa)*(yb) +
limit(limit(diff(func,y,y),x=a),y=b)*(yb)^2/2;
CubeTerm2 := (func,a,b) >
limit(limit(diff(func,x,x,x),x=a),y=b)*(xa)^3/6 +
limit(limit(diff(func,x,x,y),x=a),y=b)*(xa)^2*(yb)/2 +
limit(limit(diff(func,x,y,y),x=a),y=b)*(xa)*(yb)^2/2 +
limit(limit(diff(func,y,y,y),x=a),y=b)*(yb)^3/6;
(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
. 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*xy); a:= Pi/2; b:=Pi/2;
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);
>
del := Pi/2:
>
deg0plot := plot3d(g(x,y),
x=adel..a+del, y=bdel..b+del, view=5..5, style=patch, color=red):
>
deg1plot := plot3d(Deg1TP,
x=adel..a+del, y=bdel..b+del, view=5..5, style=patch, color=blue):
>
deg2plot := plot3d(Deg2TP,
x=adel..a+del, y=bdel..b+del, view=5..5, style=patch, color=green):
>
deg3plot := plot3d(Deg3TP,
x=adel..a+del, y=bdel..b+del, view=5..5, style=patch, color=gold):
>
display( deg0plot, deg1plot,deg2plot,deg3plot);
We can make the graph clearer by specifying colors for the graphs
>
del := .8:
surf3 := plot3d(Deg3TP,
x=adel..a+del, y=bdel..b+del, color=blue):
surf2 := plot3d(Deg2TP,
x=adel..a+del, y=bdel..b+del, color=green):
surf1 := plot3d(Deg1TP,
x=adel..a+del, y=bdel..b+del, color=red):
surf := plot3d(g(x,y), x=adel..a+del, y=bdel..b+del, color=orange):
display3d({surf, surf1, surf2, surf3}, axes=boxed,
view=5..5,style=patch);
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=adel..a+del, y=bdel..b+del,
view=0.1..0.1, axes=boxed);
evalf(subs({x=1.7, y=1.4}, Deg3TP));
g(1.7,1.4);
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*xy); 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=b0.1}, Deg2TP));
g(a+0.1, b0.1);
>
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)*(xxval)^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:
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=adel..a+del, y=5..5, axes=normal);
plot({f(x)  Taypoly1(f(x),a,15)},
x=adel..a+del, y=0.1..0.1, axes=normal);
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);
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
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
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$(degi)),x=xval),y=yval)
*(xxval)^i*(yyval)^(degi)/((degi)!*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*xy): 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=adel..a+del, y=bdel..b+del, color=red):
surf := plot3d(g(x,y), x=adel..a+del, y=bdel..b+del, color=blue):
display3d({surf, surf1}, axes=boxed, view=3..3,style=patch);
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*xy): a:= Pi/2: b:=Pi/2:
del := 1: maxerr := 0.1:
plot3d(g(x,y)  Taypoly2(g(x,y),a,b,5), x=adel..a+del,
y=bdel..b+del, axes=boxed, view=maxerr..maxerr, style=patch);
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
on a for all points within 1 of (
,
) 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.93dTaylAnim.mws.
>