Lab 10: Taylor Polynomials

MAT 229, Spring 2025

In our last lab, I asked “How do we use series to approximate functions?” Actually infinite series provide exact answers when they converge; but we usually don’t have the patience to add up an infinite number of things, so we end up stopping with a partial sum, and live with the error.  

Our first goal in this lab is to create some infinite series to represent function, but then to truncate them to partial sums (which are polynomials). Thus we will accept some error. 

	1. One way of creating new series from old is composition -- so we can use the series for sin(x), say, to get the series for sin(x^2): just plug x^2 in for x in the series -- you still have polynomial terms.
	2. We can also add and subtract series (but we have to be careful that their intervals of convergence overlap!).
	3. Another way is via Taylor’s theorem: once you’ve chosen a center a, start differentiating! Then create the coefficients as (Subscript[c, n])=(((f^((n)))(a))/(n!)). Then f(x)=(Underoverscript[Σ, (n=0), ∞])(((f^((n)))(a))/(n!))(((x-a))^n).
	4. You can integrate and differentiate series: let’s see how to get the series for cosine from the series for sine. From last week:
	
		t11[x_]:=x-((x^3)/6)+((x^5)/120)-((x^7)/5040)+((x^9)/362880)-((x^11)/39916800)

Now for our second goal: once you have a series, it turns out that we can estimate the error we’re making pretty well. 

Our key commands this week include 
	Series, and 
	Normal, which eliminates the remainder from the command Series, and returns an expression which can be used to define a function
	Table
	TableForm (option TableHeadings)
	Evaluate

Taylor series review

If function f(x) has a power series representation centered at a, then that power series must be the Taylor series centered at a,
	f(x)=(Underoverscript[Σ, (n=0), ∞])(((f^((n)))(a))/(n!))(((x-a))^n)
	
But, for practical purposes, we’re going to replace that series with a polynomial: a Taylor polynomial:

Question

Let f(x)=sin(x).

What is the Taylor series for f(x) centered at π?

Series[Sin[x],{x,π,10}]

What is its interval of convergence?

We have already seen that the Taylor series for f(x) centered at 0 is sin(x)=(((Underoverscript[Σ, (k=0), ∞])(-1))^k)((x^(2k+1))/((2k+1)!)). Let’s create a composition, replacing x in this series centered at 0 with x-π. How do the results of this series compare with the Taylor series for f(x) centered at π?

Series[Sin[x],{x,0,10}]
taylorPoly[x_]=Normal[%]
taylorPoly[x-Pi]

There is a trigonometric identity that sin(x-π)=-sin(x). What does this mean for the above calculations?

Plot[{-Sin[x],Sin[x-Pi]},{x,-Pi,Pi},PlotStyle->{Blue,Dashed}]
Normal[Series[Sin[x],{x,Pi,9}]]
-taylorPoly[x-Pi]

Tangent line approximation

Question

Given a general function f(x) and value x=a, what is the tangent line approximation of f(x) at a?

Clear[g]
g[a]+g'[a](x-a)

Questions

Let f(x)=cos(x)+sin(2x).

What is an equation of the tangent line to y=f(x) at x=0?

f[x_]:=Cos[x]+Sin[2x]
f[0]+f'[0](x-0)

What is the sum of the linear terms of the series for  cos(x) and sin(2x) at 0?

Series[Cos[x]+Sin[2x],{x,0,1}]

Taylor polynomials

Definition

The (n^th) degree Taylor polynomial of f(x) centered at a is the partial sum of the Taylor series that goes up to and includes the (n^th) power of (x-a). If the Taylor series for f(x) centered at a converges to f(x) for a given value x, then the (n^th) Taylor polynomial of f(x) centered at a provides an  (n^th)-degree polynomial approximation to f(x).

Questions

Let f(x)=cos(x)+sin(2x).

f[x_]:=Cos[x]+Sin[2x]

What is the first degree Taylor polynomial of f(x) centered at 0?

Series[f[x],{x,0,1}]

What is the second degree Taylor polynomial of f(x) centered at 0?

Series[f[x],{x,0,2}]

What is the third degree Taylor polynomial of f(x) centered at 0?

Series[f[x],{x,0,3}]

Plot the graphs of three polynomials along with the graph y=f(x). You will see that all of these fit the function pretty well in the vicinity of x=0; however, each successive polynomial does its job for a little greater interval before breaking down catastrophically!

p1=Plot[f[x],{x,-2,2},PlotStyle->Black];
p2=Plot[Evaluate[Table[Normal[Series[f[x],{x,0,n}]],{n,3}]],{x,-Pi,Pi}];
Show[p1,p2]

Using the third degree Taylor polynomial of f(x) centered at 0, approximate f(0.5).

Series[f[x],{x,0,3}]
(* 
 In terms of the error estimate, you might be tempted,but don't be fooled -- 
this series's coefficients don't satisfy decreasing coefficients in order for one to use the Alternating Series Test! *)
g[x_]=Normal[Series[f[x],{x,0,3}]]
exact=f[.5]
approx=g[.5]
error=Abs[approx-exact]
TableForm[{{approx},{exact},{error}},TableHeadings->{{"g(.5)","f(.5)","|g(.5)-f(.5)|"},{"f(x) = Cos[x] + Sin[2  x]"}}]

Taylor remainder

If you approximate a quantity, you need some way to analyze how good the approximation is. Consider the error=|approximation-exact|. Here the exact is a given function g(x) and the approximation is the (n^th) degree Taylor polynomial of g(x) centered at a.

Definition

The (n^th) Taylor remainder of g(x) centered at a is

(Subscript[R, n])(x)=g(x)-(Underoverscript[Σ, (k=0), n])(((g^((k)))(a))/(k!))(((x-a))^k)

In other words the absolute error in using the (n^th) degree Taylor polynomial to approximate the function is |(Subscript[R, n])(x)|.

Analyzing error: The Taylor series error estimate

If we can bound the (((n+1))^th) derivative |(f^((n+1)))(x)|≤M for all values of x of interest, then

|(Subscript[R, n])(x)|≤(M/((n+1)!))|x-a((|)^(n+1)).

You can think of this roughly this way: the error on the interval is smaller than the largest “first neglected Taylor term” evaluated anywhere on the interval.

Questions

Let f(x)=(e^(-(x^2))).

What is the Taylor series centered at zero for (e^x)?

f[x_]:=E^(x)
Series[f[x],{x,0,6}]

Using the Taylor series for (e^x) what is the Taylor series for f(x)?

g[x_]=Series[f[x],{x,0,6}]
g[-x^2]
g[x_]=Normal[Series[f[x],{x,0,6}]]
g[-x^2]

What is the fourth degree Taylor polynomial for f(x) centered at 0? Plot it against the given function.

nterms=4 (* Why does nothing change if you replace 4 with 5? *)
f[x_]:=E^(-x^2)
Series[f[x],{x,0,nterms}]
sn[x_]=Normal[Series[f[x],{x,0,nterms}]];
Plot[{f[x],sn[x]},{x,-2,2},PlotRange->All]

From the plot of  |(f^((5)))(x)| shown below, 


use the Taylor series error estimate to bound the error in approximating f(x) with the (4^th) degree Taylor polynomial on the interval [-2,2].

f[x_]:=(E^(-(x^2)));
nterms=4;
bound=35;
fp[x_]=D[f[x],{x,nterms+1}];
Plot[Abs[fp[x]],{x,-2,2},PlotRange->All,GridLines->Automatic]
g[x_]=Normal[Series[f[x],{x,0,nterms}]];
Plot[{f[x],g[x]},{x,-2,2},PlotLabels->{"f(x)","Taylor Approx"}]
Plot[{Abs[f[x]-g[x]],bound/Factorial[nterms+1]Abs[x-0]^(nterms+1)},{x,-2,2},PlotLabels->{"actual error","bound"}]

What is the (20^th) degree Taylor polynomial for f(x) centered at 0?

f[x_]:=E^(-x^2)
nterms=20; 
Normal[Series[f[x],{x,0,nterms}]]

From the plot of  |(f^((5)))(x)| shown below,
 
 use the Taylor series error estimate to bound the error in approximating f(x) with the (20^th) degree Taylor polynomial on the interval [-2,2].

f[x_]:=E^(-x^2)
nterms=20; 
bound=4.2*10^(12);
fp[x_]=D[f[x],{x,nterms+1}];
Plot[Abs[fp[x]],{x,-2,2},PlotRange->All,GridLines->Automatic]
g[x_]=Normal[Series[f[x],{x,0,nterms}]];
Plot[{f[x],g[x]},{x,-2,2},PlotRange->All]
Plot[{Abs[f[x]-g[x]],bound/Factorial[nterms+1]Abs[x-0]^(nterms+1)},{x,-2,2},PlotLabels->{"actual error","bound"},PlotRange->All]

From the plot of  |(f^((5)))(x)| shown below, 


use the Taylor series error estimate to bound the error in approximating f(x) with the (45^th) degree Taylor polynomial on the interval [-2,2].

f[x_]:=(E^(-(x^2)));
nterms=45;
bound=2.2*10^(35);
fp[x_]=D[f[x],{x,nterms+1}];
Plot[Abs[fp[x]],{x,-2,2},PlotRange->All,GridLines->Automatic]
g[x_]=Normal[Series[f[x],{x,0,nterms}]];
Plot[{f[x],g[x]},{x,-2,2},PlotLabels->{"f(x)","Taylor Approx"}]
Plot[{Abs[f[x]-g[x]],bound/Factorial[nterms+1]Abs[x-0]^(nterms+1)},{x,-2,2},PlotLabels->{"actual error","bound"},PlotRange->All]