CoCalc Public Files12 - Taylor Series Assignment / Taylor Series Notes.sagewsOpen with one click!
Author: Aaron Tresham
Views : 42
License: Other -- explicitly state in your code
Compute Environment: Ubuntu 20.04 (Default)
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.


  • Intro to Sage
  • Series

Taylor Series

Definition: A power series centered at x=ax=a is a series of the form n=0[cn(xa)n]=c0+c1(xa)+c2(xa)2+\sum_{n=0}^{\infty}\left[c_n\cdot(x-a)^n\right]=c_0+c_1\cdot(x-a)+c_2\cdot(x-a)^2+\cdots

where aa and all the cic_i are constants.

Notice that a power series is a function of xx. The domain is all values of xx for which the series converges. There is always at least one number in the domain, namely aa.

You can think of a power series as a polynomial with (possibly) infinite degree.

One of the nice things about power series is that it's easy to differentiate and integrate them. Just like regular polynomials, you can take the derivative or integral of each term first, and then add up the results.



Example 1

A geometric series with xx as the common ratio is a simple example of a power series (a=0a=0 and cn=cc_n=c for all nn):

n=0cxn\sum_{n=0}^{\infty}c\cdot x^n

We know when this series converges and what it coverges to:

n=0cxn=c1x\sum_{n=0}^{\infty}c\cdot x^n=\frac{c}{1-x}

provided that 1<x<1-1 < x < 1.

So n=0cxn\displaystyle\sum_{n=0}^{\infty}c\cdot x^n is a function with domain (1,1)(-1,1). On this domain, this power series function happens to be equal to the function f(x)=c1x\displaystyle f(x)=\frac{c}{1-x}.

Taylor Series

This raises the question of whether other functions are equal to the sum of a power series (at least for part of their domains).

In other words, given a function f(x)f(x), can we find cic_i so that f(x)=n=0[cn(xa)n]f(x)=\sum_{n=0}^{\infty}\left[c_n\cdot(x-a)^n\right]

for some interval of x-values?

This process of representing a function by a power series is called "expanding" the function into a series. The power series you get is called a Taylor series expansion of f(x)f(x), after mathematician Brook Taylor (1685-1731).

Expanding functions into Taylor series and differentiating and integrating the series had a number of applications back then. For example, you can use Taylor Series to approximate the values of numbers like π\pi and ee. Or consider the logarithmic and trigonometric functions. These are often difficult to calculate, but if you expand these into Taylor series, then you can approximate values of these functions using only polynomials (and polynomials only require arithmetic to calculate).

Fortunately, finding the right power series to represent a function is fairly straightforward, as long as the function is repeatedly differentiable. The secret is to find derivatives of every order and evaluate them at x=ax=a.

Suppose f(x)=n=0[cn(xa)n]\displaystyle f(x)=\sum_{n=0}^{\infty}\left[c_n\cdot(x-a)^n\right], take the mthm^{th} derivative, and plug in x=ax=a:


For all n<mn < m, dmdxm[cn(xa)n]=0\displaystyle \frac{d^m}{dx^m}\left[c_n\cdot(x-a)^n\right]=0, so when you plug in x=ax=a it's still 0.

For all n>mn > m, dmdxm[cn(xa)n]=n!(nm)!cn(xa)nm\displaystyle \frac{d^m}{dx^m}\left[c_n\cdot(x-a)^n\right]=\frac{n!}{(n-m)!}\cdot c_n\cdot(x-a)^{n-m}, with nm>0n-m > 0. So when you plug in x=ax=a, you get n!(nm)!cn(aa)nm=0\displaystyle \frac{n!}{(n-m)!}\cdot c_n\cdot(a-a)^{n-m}=0.

Finally, when n=mn=m, dmdxm[cn(xa)n]=n!cn(xa)0=n!cn=m!cm\displaystyle \frac{d^m}{dx^m}\left[c_n\cdot(x-a)^n\right]=n!\cdot c_n\cdot (x-a)^0=n!\cdot c_n=m!\cdot c_m.

Putting this all together, we see that f(m)(a)=m!cm\displaystyle f^{(m)}(a)=m!\cdot c_m.

Therefore, cm=f(m)(a)m!\displaystyle c_m=\frac{f^{(m)}(a)}{m!}.

Conclusion: If a function f(x)f(x) with derivatives of every order may be represented by a power series centered at x=ax=a on some interval II, then that power series is f(x)=n=0f(n)(a)n!(xa)n,f(x)=\sum_{n=0}^{\infty}\frac{f^{(n)}(a)}{n!}\cdot(x-a)^n,

where the series converges on the interval II.

Notice the "if" in the last sentence. There are functions that are not equal to the sum of their Taylor series, even if the series converges. We are not going to deal with such functions in this lab.

Example 2

Find the Taylor series of f(x)=exf(x)=e^x centered at x=0x=0 (Taylor series centered at 0 are also called Maclaurin series).

We know f(n)(x)=f(x)=exf^{(n)}(x)=f(x)=e^x in this case. Since e0=1e^0=1, we have f(n)(0)=1f^{(n)}(0)=1 for all nn.

Thus, cn=1n!\displaystyle c_n=\frac{1}{n!}.

If exe^x equals the sum of its Taylor series, then ex=n=0xnn!\displaystyle e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}. For this particular function, the Taylor series converges for all xx, and exe^x does equal the sum of the series (take my word for it).

Taylor Polynomials

The partial sums of a Taylor series are actual polynomials, called Taylor polynomials. In other words, the Taylor polynomial of degree mm is n=0mf(n)(a)n!(xa)n\displaystyle\sum_{n=0}^m\frac{f^{(n)}(a)}{n!}(x-a)^n.

We can approximate a Taylor series to whatever level of accuracy we want by using a Taylor polynomial of high enough degree.

Note: If f(m)(a)=0f^{(m)}(a)=0, then this polynomial will actually have degree less than mm.

Notice that the Taylor polynomial of degree 1 is n=01f(n)(a)n!(xa)n=f(0)(a)0!(xa)0+f(1)(a)1!(xa)1=f(a)+f(a)(xa)\displaystyle\sum_{n=0}^1\frac{f^{(n)}(a)}{n!}(x-a)^n=\frac{f^{(0)}(a)}{0!}(x-a)^0+\frac{f^{(1)}(a)}{1!}\cdot(x-a)^1=f(a)+f'(a)(x-a).

Does this look familiar? It should! This is an equation for the tangent line to ff at aa. In other words, Taylor polynomials are generalizations of the tangent line to higher degree polynomials.

Also note that the Taylor polynomial of degree 2 is n=02f(n)(a)n!(xa)n=\displaystyle\sum_{n=0}^2\frac{f^{(n)}(a)}{n!}(x-a)^n= f(0)(a)0!(xa)0+f(1)(a)1!(xa)1+f(2)(a)2!(xa)2=\frac{f^{(0)}(a)}{0!}(x-a)^0+\frac{f^{(1)}(a)}{1!}\cdot(x-a)^1+\frac{f^{(2)}(a)}{2!}\cdot(x-a)^2=


This is the formula we used for Quadratic Approximation in Calc 1.

Example 3

We will use Taylor polynomials to approximate the value of ee. We saw above that ex=n=0xnn!\displaystyle e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}.

The Taylor polynomial of degree mm is n=0mxnn!=1+x+x22+x36++xmm!\displaystyle\sum_{n=0}^{m}\frac{x^n}{n!}=1+x+\frac{x^2}{2}+\frac{x^3}{6}+\cdots+\frac{x^m}{m!}.

So ex1+x+x22+x36++xmm!\displaystyle e^x\approx 1+x+\frac{x^2}{2}+\frac{x^3}{6}+\cdots+\frac{x^m}{m!}. The approximation improves as mm increases.

If we plug in x=1x=1, we get e1+1+12+16++1m!\displaystyle e\approx 1+1+\frac{1}{2}+\frac{1}{6}+\cdots+\frac{1}{m!}.

Let's approximate the value of ee.

%auto %var n @interact def _(m=input_box(1,label='Degree of Taylor polynomial',width=20)): print "Approximation =",N(sum(1/factorial(n),n,0,m),digits=30) print "Actual value =",N(e,digits=30)
Interact: please open in CoCalc

How big must mm be so that our approximation is correct for all the decimal places shown?

Let's look at some graphs.

f(x)=exf(x)=e^x is plotted in black, along with three Taylor polynomials (blue = degree 2, green = degree 3, red = degree 4)

%var n plot(sum(x^n/factorial(n),n,0,2),xmin=-1, xmax=1,color='blue',legend_label='degree 2')+plot(sum(x^n/factorial(n),n,0,3),xmin=-1, xmax=1,color='green',legend_label='degree 3')+plot(sum(x^n/factorial(n),n,0,4),xmin=-1, xmax=1,color='red',legend_label='degree 4')+plot(e^x,xmin=-1, xmax=1,ymax=e,color='black',legend_label='$e^x$')

Notice that the higher the degree the better the approximation.

Now try xmin=-10; xmax=10; ymax=1000

%var n plot(sum(x^n/factorial(n),n,0,2),xmin=-10,xmax=10,color='blue',legend_label='degree 2')+plot(sum(x^n/factorial(n),n,0,3),xmin=-10, xmax=10,color='green',legend_label='degree 3')+plot(sum(x^n/factorial(n),n,0,4),xmin=-10, xmax=10,color='red',legend_label='degree 4')+plot(e^x,xmin=-10, xmax=10,ymax=1000,color='black',legend_label='$e^x$')

Notice that the Taylor polynomials are better approximations the closer you get to the center of the expansion (in this case x=0x=0), just like the tangent line is a better approximation the closer you get to the point of tangency.

In the animation below, we see taylor polynomials of increasing degree (in blue) plotted with f(x)=exf(x)=e^x (in black).

%auto s=[] taylorpoly=1 p=plot(e^x,-5,15,ymin=-100,ymax=1500,color='black') for n in [1..10]: taylorpoly+=x^n/factorial(n) s+=[p+plot(taylorpoly,-5,15)] a=animate(s) show(a,delay=50)

We can use Sage to calculate Taylor polynomials using the "taylor" command. This command takes four arguments: the expression or function to expand, the variable of expansion, the center of the expansion, and the degree of polynomial you want.

Example 4

Use Sage to find the 10th-degree Taylor polynomial centered at x=0x=0 for f(x)=exf(x)=e^x.

13628800x10+1362880x9+140320x8+15040x7+1720x6+1120x5+124x4+16x3+12x2+x+1\displaystyle \frac{1}{3628800} \, x^{10} + \frac{1}{362880} \, x^{9} + \frac{1}{40320} \, x^{8} + \frac{1}{5040} \, x^{7} + \frac{1}{720} \, x^{6} + \frac{1}{120} \, x^{5} + \frac{1}{24} \, x^{4} + \frac{1}{6} \, x^{3} + \frac{1}{2} \, x^{2} + x + 1

Example 5

Find the 15th-degree Taylor polynomials of sin(x)\sin(x) centered at x=0x=0, x=πx=-\pi, and x=πx=\pi.

11307674368000x15+16227020800x13139916800x11+1362880x915040x7+1120x516x3+x\displaystyle -\frac{1}{1307674368000} \, x^{15} + \frac{1}{6227020800} \, x^{13} - \frac{1}{39916800} \, x^{11} + \frac{1}{362880} \, x^{9} - \frac{1}{5040} \, x^{7} + \frac{1}{120} \, x^{5} - \frac{1}{6} \, x^{3} + x
π+11307674368000(π+x)1516227020800(π+x)13+139916800(π+x)111362880(π+x)9+15040(π+x)71120(π+x)5+16(π+x)3x\displaystyle -\pi + \frac{1}{1307674368000} \, {\left(\pi + x\right)}^{15} - \frac{1}{6227020800} \, {\left(\pi + x\right)}^{13} + \frac{1}{39916800} \, {\left(\pi + x\right)}^{11} - \frac{1}{362880} \, {\left(\pi + x\right)}^{9} + \frac{1}{5040} \, {\left(\pi + x\right)}^{7} - \frac{1}{120} \, {\left(\pi + x\right)}^{5} + \frac{1}{6} \, {\left(\pi + x\right)}^{3} - x
π11307674368000(πx)15+16227020800(πx)13139916800(πx)11+1362880(πx)915040(πx)7+1120(πx)516(πx)3x\displaystyle \pi - \frac{1}{1307674368000} \, {\left(\pi - x\right)}^{15} + \frac{1}{6227020800} \, {\left(\pi - x\right)}^{13} - \frac{1}{39916800} \, {\left(\pi - x\right)}^{11} + \frac{1}{362880} \, {\left(\pi - x\right)}^{9} - \frac{1}{5040} \, {\left(\pi - x\right)}^{7} + \frac{1}{120} \, {\left(\pi - x\right)}^{5} - \frac{1}{6} \, {\left(\pi - x\right)}^{3} - x

Notice that some of the coefficients (the cic_i) are 0 in this example.

Example 6

Find the Taylor polynomials of cos(x)\cos(x) centered at x=0x=0 of degrees 5, 10, and 15.

124x412x2+1\frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1
13628800x10+140320x81720x6+124x412x2+1-\frac{1}{3628800} \, x^{10} + \frac{1}{40320} \, x^{8} - \frac{1}{720} \, x^{6} + \frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1
187178291200x14+1479001600x1213628800x10+140320x81720x6+124x412x2+1-\frac{1}{87178291200} \, x^{14} + \frac{1}{479001600} \, x^{12} - \frac{1}{3628800} \, x^{10} + \frac{1}{40320} \, x^{8} - \frac{1}{720} \, x^{6} + \frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1

Example 7

Find the 10th-degree Taylor polynomial centered at x=0x=0 of f(x)=c1x\displaystyle f(x)=\frac{c}{1-x}.

%var c show(taylor(c/(1-x),x,0,10))
cx10+cx9+cx8+cx7+cx6+cx5+cx4+cx3+cx2+cx+c\displaystyle c x^{10} + c x^{9} + c x^{8} + c x^{7} + c x^{6} + c x^{5} + c x^{4} + c x^{3} + c x^{2} + c x + c

Here's a graph of this one for c=1c=1.