This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

# Expressing Functions with Taylor Series¶

A sequence is an ordered set of numbers denoted by the list of numbers inside parentheses. For example, $$s = (s_1, s_2, s_3, \cdots)$$ means $$s$$ is the sequence $$s_1, s_2, s_3, \cdots$$ and so on. In this context, “ordered” means that $$s_1$$ comes before $$s_2$$, not that $$s_1 < s_2$$. Many sequences have a more complicated structure. For example, $$s = (n^2, n\in N)$$ is the sequence 0, 1, 4, 9, $$\cdots$$. A series is the sum of a sequence up to a certain element. An infinite sequence is a sequence with an infinite number of terms, and an infinite series is the sum of an infinite sequence.

A Taylor series expansion is a representation of a function by an infinite series of polynomials around a point. Mathematically, the Taylor series of a function, $$f(x)$$, is defined as:

$f(x) = \sum_{n = 0}^{\infty} \frac{f^{(n)}(a)(x-a)^n}{n!},$

where $$f^{(n)}$$ is the $$n^\mathrm{th}$$ derivative of $$f$$ and $$f^{(0)}$$ is the function $$f$$

TRY IT! Compute the Taylor series expansion for $$f(x) = 5x^2 + 3x + 5$$ around $$a = 0$$, and $$a = 1$$. Verify that $$f$$ and its Taylor series expansions are identical.

First compute derivatives analytically:

$\begin{eqnarray*} f(x) &=& 5x^2 + 3x + 5\\ f^{\prime}(x) &=& 10x + 3\\ f''(x) &=& 10 \end{eqnarray*}$

Around a = 0:

$f(x) = \frac{5x^0}{0!} + \frac{3x^1}{1!} + \frac{10x^2}{2!} + 0 + 0 + \cdots = 5x^2 + 3x + 5$

Around a = 1:

$\begin{eqnarray*} f(x) &=& \frac{13(x-1)^0}{0!} + \frac{13(x-1)^1}{1!} + \frac{10(x-1)^2}{2!} + 0 + \cdots\\ &=& 13 + 13x - 13 + 5x^2 - 10x + 5 = 5x^2 + 3x + 5 \end{eqnarray*}$

Note: The Taylor series expansion of any polynomial has finite terms because the $$n^\mathrm{th}$$ derivative of any polynomial is 0 for $$n$$ large enough.

TRY IT! Write the Taylor series for $$\sin(x)$$ around the point $$a = 0$$.

Let $$f(x) = \sin(x)$$. Then according to the Taylor series expansion, $$$f(x) = \frac{\sin(0)}{0!}x^0 + \frac{\cos(0)}{1!}x^1 + \frac{-\sin(0)}{2!}x^2 + \frac{-\cos(0)}{3!}x^3 + \frac{\sin(0)}{4!}x^4 + \frac{\cos(0)}{5!}x^5 + \cdots.$$$

The expansion can be written compactly by the formula $$$f(x) = \sum_{n = 0}^{\infty} \frac{(-1)^n x^{2n+1}}{(2n+1)!},$$$$which ignores the terms that contain$$\sin(0)$$(i.e., the even terms). However, because these terms are ignored, the terms in this series and the proper Taylor series expansion are off by a factor of$$2n+1$$; for example the$$n = 0$$term in formula is the$$n = 1$$term in the Taylor series, and the$$n = 1$$term in the formula is the$$n = 3$ term in the Taylor series.mm