Basic_Taylor_demo.mw

 

Taylor Polynomials Demo. 

 

The following worksheet demonstrates some of Maple's abilities to work with Taylor polynomials.  

 

To begin we need to include the following Maple packages... 

 

> restart;
with(Student[Calculus1]):
with(Optimization):
 

 

Let's work with a "Bell Curve" like function...  

 

> f := x -> exp(-x^2):
'f(x)'=f(x);
 

f(x) = exp(`+`(`-`(`*`(`^`(x, 2))))) (1)
 

 

The "Student[Calculus1]" package has a function called "TaylorApproximation" which will do pretty much everything we want. 

 

Here is the (fifth order) Taylor polynomial P[5](x) for f(x) = exp(`+`(`-`(`*`(`^`(x, 2))))) centered at x[0]=1... 

 

> TaylorApproximation(f(x),x=1,order=5);
 

`+`(`*`(`/`(73, 30), `*`(exp(-1))), `*`(`/`(5, 3), `*`(exp(-1), `*`(x))), `-`(`*`(`/`(20, 3), `*`(exp(-1), `*`(`^`(x, 2))))), `*`(`/`(14, 3), `*`(exp(-1), `*`(`^`(x, 3)))), `-`(`*`(`/`(7, 6), `*`(exp(... (2)
 

 

This function will also happily churn out a whole list of Taylor polynomials.  

For example here are and  P[3](x)... 

 

> TaylorApproximation(f(x),x=1,order=1..3);
 

`+`(`-`(`*`(2, `*`(exp(-1), `*`(x)))), `*`(3, `*`(exp(-1)))), `+`(`*`(exp(-1), `*`(`^`(x, 2))), `-`(`*`(4, `*`(exp(-1), `*`(x)))), `*`(4, `*`(exp(-1)))), `+`(`*`(`/`(10, 3), `*`(exp(-1))), `-`(`*`(2, ... (3)
 

 

"TaylorApproximation" will also produce plots. Here is a plot of the first 3 Taylor polynomials (in blue) 

along with the original function (in red). I have set the view window to `and`(`<=`(-2, x), `<=`(x, 4)) and `and`(`<=`(-.5, y), `<=`(y, 1.5))... 

 

> TaylorApproximation(f(x),x=1,order=1..3,output=plot,view=[-2..4,-0.5..1.5]);
 

Plot_2d
 

 

Another nice feature is that this function will output animations (a series of plots shown over time). 

Here are plots of the first 25 Taylor polynomials.  

 

Note: To play this animation, click on the plot and then click on the play button appearing in the toolbar at the top of this window. 

 

> TaylorApproximation(f(x),x=1,order=1..25,output=animation,view=[-2..4,-0.5..1.5]);
 

Plot_2d
 

 

Let's compute the actual maximum error for the 9th order Taylor polynomial. 

 

> P9 := TaylorApproximation(f(x),x=1,order=9);
 

`+`(`*`(`/`(12295, 4536), `*`(exp(-1))), `*`(`/`(103, 1260), `*`(exp(-1), `*`(x))), `-`(`*`(`/`(979, 315), `*`(exp(-1), `*`(`^`(x, 2))))), `*`(`/`(149, 135), `*`(exp(-1), `*`(`^`(x, 3)))), `-`(`*`(`/`... (4)
 

 

Next, we use the "Maximize" function again to find the max error... 

 

> ActualMaxError := Maximize(abs(f(x)-P9),x=0..2)[1];
 

HFloat(0.002848824773145137) (5)
 

 

So if we stay with `&+-`(1) of the base point x = 1, our Taylor polynomial never gives an answer more than `&+-`(0.245e-2) off of our actual function. 

 

 

A second example: Let's consider f(x) = sin(`*`(`^`(x, 2))) and base at x = 0 this time. 

 

> f := x -> sin(x^2)+sin(x):
'f(x)'=f(x);
 

f(x) = `+`(sin(`*`(`^`(x, 2))), sin(x)) (6)
 

 

The Student[Calculus1] package has a function called "TaylorApproximation" which will do pretty much 

everything we want. 

 

Here is the (fifth order) Taylor polynomial P[5](x) for  f(x) centered at  x[0]=0... 

 

> TaylorApproximation(f(x),x=0,order=5);
 

`+`(x, `*`(`^`(x, 2)), `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5)))) (7)
 

 

Here are   and  P[3](x) ... 

 

P[3](x) (8)
 

> TaylorApproximation(f(x),x=0,order=1..3);
 

x, `+`(`*`(`^`(x, 2)), x), `+`(x, `*`(`^`(x, 2)), `-`(`*`(`/`(1, 6), `*`(`^`(x, 3))))) (9)
 

 

Here is a plot of the first 3 Taylor polynomials (in blue) 

along with the original function (in red). I have set the view window to `and`(`<=`(-4, x), `<=`(x, 4)) and `and`(`<=`(-4, y), `<=`(y, 4)). 

 

> TaylorApproximation(f(x),x=0,order=1..3,output=plot,view=[-4..4,-4..4]);
 

Plot_2d
 

 

Here are plots of the first 25 Taylor polynomials.  

 

> TaylorApproximation(f(x),x=0,order=1..25,output=animation,view=[-4..4,-4..4]);
 

Plot_2d
 

 

Let's compute the actual maximum error for the 9th order Taylor polynomial. 

 

> P9 := TaylorApproximation(f(x),x=0,order=9);
 

`+`(x, `*`(`^`(x, 2)), `-`(`*`(`/`(1, 6), `*`(`^`(x, 3)))), `*`(`/`(1, 120), `*`(`^`(x, 5))), `-`(`*`(`/`(1, 6), `*`(`^`(x, 6)))), `-`(`*`(`/`(1, 5040), `*`(`^`(x, 7)))), `*`(`/`(1, 362880), `*`(`^`(x... (10)
 

 

Next, we use the "Maximize" function again to find the max error... 

 

> ActualMaxError := Maximize(abs(f(x)-P9),x=-1..1)[1];
 

HFloat(0.008137675405957993) (11)
 

 

So if we stay with `&+-`(1) of the base point x = 0, our Taylor polynomial never gives an answer more than `&+-`(0.814e-2) off of our actual function.