Shared15 - Taylor Series Assignment / Taylor Series Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

### Prerequisites:

• Intro to Sage
• Series

# Taylor Series

Definition: A power series centered at $x=a$ is a series of the form $\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 $a$ and all the $c_i$ are constants.

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

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.

$\frac{d}{dx}\sum_{n=0}^{\infty}\left[c_n\cdot(x-a)^n\right]=\sum_{n=0}^{\infty}\frac{d}{dx}\left[c_n\cdot(x-a)^n\right]$

$\int\sum_{n=0}^{\infty}\left[c_n\cdot(x-a)^n\right]\,dx=\sum_{n=0}^{\infty}\int\left[c_n\cdot(x-a)^n\right]\,dx$

## Example 1

A geometric series with $x$ as the common ratio is a simple example of a power series ($a=0$ and $c_i=c$ for all $i$):

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

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

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

provided that $-1 < x < 1$.

So $\displaystyle\sum_{n=0}^{\infty}c\cdot x^n$ is a function with domain $(-1,1)$. On this domain, this power series function happens to be equal to the function $\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)$, can we find $c_i$ so that $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)$, 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 $e$. 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=a$.

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

$f^{(m)}(a)=\sum_{n=0}^{\infty}\left.\frac{d^m}{dx^m}\left[c_n\cdot(x-a)^n\right]\right|_{x=a}$

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

For all $n > m$, $\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 $n-m > 0$. So when you plug in $x=a$, you get $\displaystyle \frac{n!}{(n-m)!}\cdot c_n\cdot(a-a)^{n-m}=0$.

Finally, when $n=m$, $\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 $\displaystyle f^{(m)}(a)=m!\cdot c_m$.

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

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

where the series converges on the interval $I$.

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)=e^x$ centered at $x=0$ (Taylor series centered at 0 are also called Maclaurin series).

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

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

If $e^x$ equals the sum of its Taylor series, then $\displaystyle e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}$. For this particular function, the Taylor series converges for all $x$, and $e^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 $m$ is $\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)=0$, then this polynomial will actually have degree less than $m$.

Notice that the Taylor polynomial of degree 1 is $\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 $f$ at $a$. 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 $\displaystyle\sum_{n=0}^2\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+\frac{f^{(2)}(a)}{2!}\cdot(x-a)^2=$

$f(a)+f'(a)(x-a)+\frac{f''(a)}{2}(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 $e$. We saw above that $\displaystyle e^x=\sum_{n=0}^{\infty}\frac{x^n}{n!}$.

The Taylor polynomial of degree $m$ is $\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 $\displaystyle e^x\approx 1+x+\frac{x^2}{2}+\frac{x^3}{6}+\cdots+\frac{x^m}{m!}$. The approximation improves as $m$ increases.

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

Let's approximate the value of $e$.

%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)


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

Let's look at some graphs.

$f(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=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)=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=0$ for $f(x)=e^x$.

show(taylor(e^x,x,0,10))

$\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)$ centered at $x=0$, $x=-\pi$, and $x=\pi$.

show(taylor(sin(x),x,0,15))

$\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$
show(taylor(sin(x),x,-pi,15))

$\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$
show(taylor(sin(x),x,pi,15))

$\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 $c_i$) are 0 in this example.

## Example 6

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

show(taylor(cos(x),x,0,5))

$\frac{1}{24} \, x^{4} - \frac{1}{2} \, x^{2} + 1$
show(taylor(cos(x),x,0,10))

$-\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$
show(taylor(cos(x),x,0,15))

$-\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=0$ of $\displaystyle f(x)=\frac{c}{1-x}$.

%var c
show(taylor(c/(1-x),x,0,10))

$\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=1$.

plot(taylor(1/(1-x),x,0,10))+plot(1/(1-x),color='black',ymax=6,ymin=0)