Taylor polynomials in several variables Worksheet by Mike May, S.J.- maymk@slu.edu \302\2512006 restart; with(plots): with(Student[MultivariateCalculus]):
<Text-field style="Heading 1" layout="Heading 1">Review of Taylor polynomials in one variable</Text-field> Recall from calculus of one variable that we can approximate a nice function at a point x=a by Taylor polynomials. We get the nth degree part of the approximation near x=a by evaluating the nth derivative at that point and multiplying by NiMqJiksJiUieEciIiIlImFHISIiJSJuR0YnLSUqZmFjdG9yaWFsRzYjRipGKQ==. With the TaylorApproximation command from the Student[MultivariableCalculus] package we can compute the Taylor polynomial. It is instructive to note that the difference between the nth degree Taylor polynomial and the (n-1)st degree Taylor polynomial is the term we just described. TaylorDegree := 10; NthTermOfTaylor := TaylorApproximation(f(x),[x]=[a],TaylorDegree)- TaylorApproximation(f(x),[x]=[a],TaylorDegree-1); Consider an example: f := x -> sin(x) + cos(2*x); a:= Pi/3; We evaluate the first several Taylor polynomials for our function, expanded about our given point. Deg0 := TaylorApproximation(f(x),[x]=[a],0); Deg1 := TaylorApproximation(f(x),[x]=[a],1); Deg2 := TaylorApproximation(f(x),[x]=[a],2); Deg3 := TaylorApproximation(f(x),[x]=[a],3); Deg4 := TaylorApproximation(f(x),[x]=[a],4); Visually we notice that we get better and better approximations by adding in more and more terms. del := Pi/2; plot([f(x), Deg0, Deg1, Deg2, Deg3, Deg4], x=a-del..a+del, y=-5..5, axes=normal, legend = ["func", "Deg0", "Deg1", "Deg2", "Deg3", "Deg4"]); To check the closeness of an approximation we plot the difference of the function and the approximation. del := .6; plot([Deg1 - f(x), Deg2 - f(x), Deg3 - f(x), Deg4 - f(x)], x=a-del..a+del, y=-0.1..0.1, axes=normal, legend=["LinErr","QuadErr","CubeErr","QuadErr"]); If we want numbers rather than pictures, we can evaluate the Taylor polynomials with the eval command. We also use the evalf command to convert the answer to a decimal value. "f(1.2)" = f(1.2); "Deg 3 Approx" = eval(Deg3, x=1.2); "Deg 3 Approx" = evalf(eval(Deg3, x=1.2)); "Error in Approx" = f(1.2) - evalf(eval(Deg3, x=1.2)); We will be interested 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 we have of the function) or when we want a numerical value when a symbolic manipulation is hard or impossible for the given function but easy for a polynomial approximation. (For example, finding NiMtJSRpbnRHNiQtJSRzaW5HNiMqJCklInhHIiIlIiIiL0YrOyIiIUYt.)
<Text-field style="Heading 1" layout="Heading 1">Exercises:</Text-field> 1) For each Taylor polynomial approximation in the example above, give an interval where you think it is a good approximation to the original function. 2) For each of the Taylor approximations in the example above, use the error function to give an interval where the error is less than .02. 3) Find the values of f(x) and the 4th degree Taylor polynomial above at x=0.9. Find the value of the error in the approximation.
<Text-field style="Heading 1" layout="Heading 1">Generalizing Taylor Polynomials to functions of two variables</Text-field> 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, which simplifies the computations. That leads to the following procedures for computing the linear through quadratic terms. LinTerm2 := TaylorApproximation(g(x,y),[x,y]=[a,b],1)- TaylorApproximation(g(x,y),[x,y]=[a,b],0); QuadTerm2:= TaylorApproximation(g(x,y),[x,y]=[a,b],2)- TaylorApproximation(g(x,y),[x,y]=[a,b],1); CubeTerm2 := TaylorApproximation(g(x,y),[x,y]=[a,b],3)- TaylorApproximation(g(x,y),[x,y]=[a,b],2); (The rule for the factor added to each derivative is, if we take the partial derivative n times with respect to x and the partial derivative m times with respect to y then we multiply by NiMqKCksJiUieEciIiIlImFHISIiJSJuR0YnKSwmJSJ5R0YnJSJiR0YpJSJtR0YnKiYtJSpmYWN0b3JpYWxHNiNGKkYnLUYyNiNGL0YnRik=. Note that this naturally generalizes the formula for functions of one variable.) We start with a function and a point. g := (x,y) ->sin(x+3*y)+cos(5*x-y); a:= Pi/2; b:=Pi/2; Next we compute the first few Taylor polynomials Deg0TP := g(a,b); Deg1TP := TaylorApproximation(g(x,y),[x,y]=[a,b],1); Deg2TP := TaylorApproximation(g(x,y),[x,y]=[a,b],2); Deg3TP := TaylorApproximation(g(x,y),[x,y]=[a,b],3); del := Pi/6: plot3d({g(x,y),Deg1TP, Deg2TP, Deg3TP}, x=a-del..a+del, y=b-del..b+del, view=-5..5, style=patch); We can make the graph clearer by specifying colors for the graphs del := Pi/6: plot3d([g(x,y),Deg1TP,Deg2TP,Deg3TP], x=a-del..a+del, y=b-del..b+del,view=-5..5, color=[orange,red,green,blue], axes=boxed, style=patch); As above, we are interested in plotting an error term to see where the approximation is close enough and in evaluating the Taylor polynomials 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=normal); "Deg 3 Approx" = evalf(eval(Deg3TP, {x=1.7, y=1.5})); "computed value" = g(1.7,1.5); "Error" = g(1.7,1.5)-evalf(eval(Deg3TP, {x=1.7, y=1.5})); Since the homework in the text asks for evaluation of linear and quadratic Taylor polynomials, it seems worthwhile to put the code needed in one section so students can easily use Maple for those problems. g := (x,y) ->sin(x+3*y)+cos(5*x-y); a:= Pi/2; b:= Pi/2; Deg0TP := g(a,b); Deg1TP := TaylorApproximation(g(x,y),[x,y]=[a,b],1); Deg2TP := TaylorApproximation(g(x,y),[x,y]=[a,b],2); "Approx" = evalf(eval(Deg2TP, {x=a+0.1, y=b-0.1})); "Function Value" =g(a+0.1, b-0.1);
<Text-field style="Heading 1" layout="Heading 1">Exercises:</Text-field> 4) For what region is Deg3TP 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).
<Text-field style="Heading 1" layout="Heading 1">Generating Taylor Polynomials of arbitrary degree</Text-field>
<Text-field style="Heading 1" layout="Heading 1">Review of the one variable case</Text-field> As noted above, we can get Maple to compute Taylor polynomials of arbitrary degree using the TaylorApproximation command. This lets us compare the function at the start of the worksheet with its 15th degree Taylor polynomial. f := x -> sin(x) + cos(2*x); a:= Pi/2; del := 2*Pi; f(x); "TaylorDeg15" = TaylorApproximation(f(x),[x]=[a],15); plot([f(x), TaylorApproximation(f(x),[x]=[a],15)], x=a-del..a+del, y=-5..5, axes=normal, legend = ["f(x)", "Deg15TP"]); plot(f(x)-TaylorApproximation(f(x),[x]=[a],15), x=a-del..a+del, y=-0.1..0.1, axes=normal, legend = "Deg15Error"); Once again, we use the eval and evalf commands if we want to evaluate at a point. evalf(eval(TaylorApproximation(f(x),[x]=[a],15),x=0.3)); f(0.3);
<Text-field style="Heading 2" layout="Heading 2">Exercises: </Text-field> 6) What degree Taylor polynomial do we need to use for the graph of the function above and the graph of its Taylor polynomial to appear the same between -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 NiMsJi0lJHNpbkc2IyomIiIkIiIiJSJ4R0YpRiktJSRjb3NHNiMqJkYqRikiIiMhIiJGMA== and the graph of its Taylor polynomial to appear the same between 0 and 6? (The Taylor approximation should be centered at x=3.) 8) Evaluate the function NiMsJi0lJHNpbkc2IyomIiIkIiIiJSJ4R0YpRiktJSRjb3NHNiMqJkYqRikiIiMhIiJGMA== and its 20 degree Taylor polynomial approximation ax x=4.
<Text-field style="Heading 1" layout="Heading 1">Generalizing to two variables</Text-field> We can use the same approach for functions of 2 variables. Once again we use the TaylorApproximation command. Now we would like to explore the 5th degree Taylor polynomial approximation to the function g(x,y) that we used earlier. Once we have the polynomial we would like to compare the graphs of the g(x,y) with its Taylor polynomial. g := (x,y) ->sin(x+3*y)+cos(2*x-y): a:= Pi/2: b:=Pi/2: del := 1: ApproxPoly := TaylorApproximation(g(x,y),[x,y]=[a,b],5); plot3d([g(x,y),ApproxPoly], x=a-del..a+del, y=b-del..b+del, view=-3..3, color=[red, blue], axes=boxed, 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*x-y): a:= Pi/2: b:=Pi/2: TaylorDeg := 5: del := 1: maxerr := 0.1: ApproxPoly := TaylorApproximation(g(x,y),[x,y]=[a,b],TaylorDeg): plot3d(g(x,y) - ApproxPoly, x=a-del..a+del, y=b-del..b+del, view=-maxerr..maxerr, axes=boxed, style=patchcontour); 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 the degree of the approximation or the definition of acceptable error simply changes the size of the good patch.
<Text-field style="Heading 2" layout="Heading 2">Exercise:</Text-field> 9) What degree Taylor approximation do we need to use if we want to approximate the function LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYlLUkjbWlHRiQ2JlEhRicvJSdpdGFsaWNHUSV0cnVlRicvJStiYWNrZ3JvdW5kR1EoWzAsMCwwXUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1GIzYnRistRiM2JS1GLDYmUSRzaW5GJy9GMFEmZmFsc2VGJ0YyL0Y2USdub3JtYWxGJy1JI21vR0YkNjFRMCZBcHBseUZ1bmN0aW9uO0YnRjJGQS8lJmZlbmNlR0ZALyUqc2VwYXJhdG9yR0ZALyUpc3RyZXRjaHlHRkAvJSpzeW1tZXRyaWNHRkAvJShsYXJnZW9wR0ZALyUubW92YWJsZWxpbWl0c0dGQC8lJ2FjY2VudEdGQC8lJWZvcm1HUSZpbmZpeEYnLyUnbHNwYWNlR1EkMGVtRicvJSdyc3BhY2VHRlovJShtaW5zaXplR1EiMUYnLyUobWF4c2l6ZUdRKWluZmluaXR5RictSShtZmVuY2VkR0YkNiUtRiM2JUYrLUYjNiYtRiw2JlEieEYnRi9GMkY1LUZENjFRIitGJ0YyRkFGR0ZJRktGTUZPRlFGU0ZVL0ZZUTBtZWRpdW1tYXRoc3BhY2VGJy9GZm5GW3BGZ25Gam4tRiM2JS1JI21uR0YkNiVRIjNGJ0YyRkEtRkQ2MVExJkludmlzaWJsZVRpbWVzO0YnRjJGQUZHRklGS0ZNRk9GUUZTRlVGWEZlbkZnbkZqbi1GLDYmUSJ5RidGL0YyRjVGK0YrRjJGQUZnby1GIzYlLUYsNiZRJGNvc0YnRj9GMkZBRkMtRl5vNiUtRiM2JUYrLUYjNiYtRmBwNiVRIjVGJ0YyRkEtRiM2JEZjcEZkby1GRDYxUSgmbWludXM7RidGMkZBRkdGSUZLRk1GT0ZRRlNGVUZqb0ZccEZnbkZqbkZmcEYrRjJGQUYrRis= for all points within 1 unit of (NiMqJiUjUGlHIiIiIiIjISIi, NiMqJiUjUGlHIiIiIiIjISIi) with an maximum error of 0.1?
It seems worthwhile to see that the Taylor polynomials in two variables wrap down to the function in the same way that they do with functions of one variable. Since this often causes memory problems the animation is done in a separate worksheet.