Notes on Taylor and Maclaurin series



A Maclaurin series is a power series - a polynomial with carefully selected coefficients and an infinite number of terms - used to approximate arbitrary functions with some conditions (e.g. differentiability). The Maclaurin series does this for input values close to 0, and is a special case of the Taylor series which can be used to find a polynomial approximation around any value.

Intuition

Let's say we have a function f(x) and we want to approximate it with some other - polynomial - function p(x). To make sure that p(x) is as close as possible to f(x), we'll create a function that has similar derivatives to f(x).

  • We start with a constant polynomial, such that p(0)=f(0). This approximation is perfect at 0 itself, but not as much elsewhere.
  • We want p(x) to behave similarly to f(x) around 0, so we'll set the derivative of our approximation to be the same as the derivative of f(x) at 0; in other words p'(0)=f'(0). This approximation will be decent very close to 0 (at least in the direction of the slope), but will become progressively worse as we get farther away from 0.
  • We continue this process, by setting the second derivative to be p''(0)=f''(0), the third derivative to be p'''(0)=f'''(0) and so on, for as many terms as we need to achieve a good approximation in our desired range. Intuitively, if many derivatives of p(x) are identical to the corresponding derivatives of f(x) at some point, the two functions will have very similar behaviors around that point [1].

The full Maclaurin series that accomplishes this approximation is:

\[p(x) = f(0)+\frac{f'(0)}{1!}x+\frac{f''(0)}{2!}x^2+\frac{f'''(0)}{3!}x^3+\cdots=\sum_{n=0}^{\infty} \frac{f^{(n)}(0)}{n!}x^n\]

We'll get to how this equation is found in a moment, but first an example that demonstrates its approximation capabilities. Suppose we want to find a polynomial approximation for f(x)=cos(x). Following the definition of the Maclaurin series, it's easy to calculate:

\[p_{cos}(x)=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\frac{x^8}{8!}-\cdots\]

(try it as an exercise).

Successive approximation of cos(x) with Maclaurin series

The dark blue line is the cosine function f(x)=cos(x). The light blue lines are successive approximations, with k terms of the power series p_{cos}(x) included:

  • With k=1, p_{cos}(x)=1 since that's just the value of cos(x) at 0.
  • With k=2, p_{cos}(x)=1-\frac{x^2}{2}, and indeed the line looks parabolic
  • With k=3 we get a 4th degree polynomial which tracks the function better, and so on

With more terms in the power series, the approximation resembles cos(x) more and more, at least close to 0. The farther away we get from 0, the more terms we need for a good approximation [2].

How the Maclaurin series works

This section shows how one arrives at the formula for the Maclaurin series, and connects it to the intuition of equating derivatives.

We'll start by observing that the Maclaurin series is developed around 0 for a good reason. The generalized form of a power series is:

\[p(x)=a_0+a_1 x+a_2 x^2 + a_3 x^3 + a_4 x^4 + \cdots\]

To properly approximate a function, we need this series to converge; therefore, it would be desirable for its terms to decrease. An x value close to zero guarantees that x^n becomes smaller and smaller with each successive term. There's a whole section on convergence further down with more details.

Recall from the Intuition section that we're looking for a polynomial that passes through the same point as f(x) at 0, and that has derivatives equal to those of f(x) at that point.

Let's calculate a few of the first derivatives of p(x); the function itself can be considered as the 0-th derivative:

\[\begin{align*} p(x)&=a_0+a_1 x+a_2 x^2 + a_3 x^3+ a_4 x^4+\cdots\\ p'(x)&= a_1 +2 a_2 x + 3 a_3 x^2+4 a_4 x^3+\cdots\\ p''(x)&= 2 a_2 + 3 \cdot 2 a_3 x+ 4 \cdot 3 x^2+\cdots\\ p'''(x)&= 3\cdot 2 a_3 + 4\cdot 3 \cdot 2 x+\cdots \\ \cdots \end{align*}\]

Now, equate these to corresponding derivatives of f(x) at x=0. All the non-constant terms drop out, and we're left with:

\[\begin{align*} f(0)&=p(0)=a_0\\ f'(0)&=p'(0)= a_1 \\ f''(0)&=p''(0)= 2 a_2 \\ f'''(0)&=p'''(0)= 3\cdot 2 a_3 \\ \cdots\\ f^{(n)}(0)&=p^{(0)}(0)=n!a_n\\ \cdots\\ \end{align*}\]

So we can set the coefficients of the power series, generalizing the denominators using factorials:

\[\begin{align*} a_0 &= f(0)\\ a_1 &= \frac{f'(0)}{1!}\\ a_2 &= \frac{f''(0)}{2!}\\ a_3 &= \frac{f'''(0)}{3!}\\ \cdots \\ a_n &= \frac{f^{(n)}(0)}{n!} \end{align*}\]

Which gives us the definition of the Maclaurin series:

\[p(x) = f(0)+\frac{f'(0)}{1!}x+\frac{f''(0)}{2!}x^2+\frac{f'''(0)}{3!}x^3+\cdots=\sum_{n=0}^{\infty} \frac{f^{(n)}(0)}{n!}x^n\]

Taylor series

The Maclaurin series is suitable for finding approximations for functions around 0; what if we want to approximate a function around a different value? First, let's see why we would even want that. A couple of major reasons come to mind:

  1. We have a non-cyclic function and we're really interested in approximating it around some specific value of x; if we use Maclaurin series, we get a good approximation around 0, but its quality is diminishing the farther away we get. We may be able to use much fewer terms for a good approximation if we start it around our target value.
  2. The function we're approximating is not well behaved around 0.

It's the second reason which is most common, at least in calculus. By "not well behaved" I mean a function that's not finite at 0 (or close to it), or that isn't differentiable at that point, or whose derivatives aren't finite.

There's a very simple and common example of such a function - the natural logarithm ln(x). This function is undefined at 0 (it approaches -\infty). Moreover, its derivatives are:

\[\begin{align*} ln'(x)&= \frac{1}{x}\\ ln''(x)&= -\frac{1}{x^2}\\ ln'''(x)&= \frac{2}{x^3}\\ ln^{(4)}(x)&= -\frac{6}{x^4}\\ ln^{(5)}(x)&= \frac{24}{x^5}\\ \cdots \end{align*}\]

None of these is defined at 0 either! The Maclaurin series won't work here, and we'll have to turn to its generalization - the Taylor series:

\[p(x) = f(a)+\frac{f'(a)}{1!}(x-a)+\frac{f''(a)}{2!}(x-a)^2+\frac{f'''(a)}{3!}(x-a)^3+\cdots=\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!}(x-a)^n\]

This is a power series that provides an approximation for f(x) around any point a where f(x) is finite and differentiable. It's easy to use exactly the same technique to develop this series as we did for Maclaurin.

Let's use this to approximate ln(x) around x=1, where this function is well behaved. ln(1)=0 and substituting x=1 into its derivatives (as listed above) at this point, we get:

\[f'(1)=1\quad f''(1)=-1\quad f'''(1)=2\quad f^{(4)}(1)=-6\quad f^{(5)}(1)=24\]

There's a pattern here: generally, the n-th derivative at 1 is (n-1)! with an alternating sign. Substituting into the Taylor series equation from above we get:

\[p_{ln}(x)=(x-1)-\frac{1}{2}(x-1)^2+\frac{1}{3}(x-1)^3-\frac{1}{4}(x-1)^4+\cdots\]

Here's a plot of approximations with the first k terms (the function itself is dark blue, as before):

Successive approximation of ln(x) with Taylor series around a=1

While the approximation looks good in the vicinity of 1, it seems like all approximations diverge dramatically at some point. The next section helps understand what's going on.

Convergence of power series and the ratio test

When approximating a function with power series (e.g. with Maclaurin or Taylor series), a natural question to ask is: does the series actually converge to the function it's approximating, and what are the conditions on this convergence?

Now it's time to treat these questions a bit more rigorously. We'll be using the ratio test to check for convergence. Generally, for a series:

\[\sum_{n=1}^\infty a_n\]

We'll administer this test:

\[L = \lim_{n\to\infty}\left|\frac{a_{n+1}}{a_n}\right|\]

And check the conditions for which L < 1, meaning that our series converges absolutely.

Let's start with our Maclaurin series for cos(x):

\[p_{cos}(x)=1-\frac{x^2}{2!}+\frac{x^4}{4!}-\frac{x^6}{6!}+\frac{x^8}{8!}-\cdots=1+\sum_{n=1}^{\infty} \frac{(-1)^n x^{2n}}{(2n)!}\]

Ignoring the constant term, we'll write out the ratio limit. Note that because of the absolute value, we can ignore the power-of-minus-one term too:

\[\begin{align*} L &= \lim_{n\to\infty}\left|\frac{a_{n+1}}{a_n}\right|\\ &= \lim_{n\to\infty}\left| \frac{x^{2n+2} (2n)!}{(2n+2)! x^{2n}}\right|\\ &= \lim_{n\to\infty}\left| \frac{x^2}{(2n+1)(2n+2)}\right| \end{align*}\]

Since the limit contents are independent of x, it's obvious that that L=0 for any x. This means that the series converges to cos(x) at any x, given an infinite number of terms. This matches our intuition for this function, which is well-behaved (smooth everywhere).

Now on to ln(x) with its Taylor series around x=1. The series is:

\[p_{ln}(x)=(x-1)-\frac{1}{2}(x-1)^2+\frac{1}{3}(x-1)^3-\frac{1}{4}(x-1)^4+\cdots=\sum_{n=1}^{\infty} \frac{(-1)^{n+1} (x-1)^n}{n}\]

Once again, writing out the ratio limit:

\[\begin{align*} L &= \lim_{n\to\infty}\left|\frac{a_{n+1}}{a_n}\right|\\ &= \lim_{n\to\infty}\left| \frac{(x-1)^{n+1} n}{(n+1) (x-1)^n}\right|\\ &= \lim_{n\to\infty}\left| \frac{n(x-1)}{(n+1)}\right|\\ &= \left|x-1\right| \lim_{n\to\infty}\left| \frac{n}{(n+1)}\right|=\left| x-1\right| \end{align*}\]

To converge, we require:

\[L=\left| x-1\right|<1\]

The solution of this inequality is 0 < x < 2. Therefore, the series converges to ln(x) only in this range of x. This is also what we observe in the latest plot. Another way to say it: the radius of convergence of the series around x=1 is 1.


[1]If this explanation and the plot of cos(x) following it don't convince you, consider watching this video by 3Blue1Brown - it includes more visualizations as well as a compelling alternative intuition using integrals and area.
[2]

Note that since cos(x) is cyclic, all we really need is good approximations in the range [-\pi, \pi). Our plot only shows the positive x axis; it looks like a mirror image on the negative side, so we see that a pretty good approximation is achieved by the time we reach k=5.

This is also a good place to note that while Maclaurin series are important in Calculus, it's not the best approximation for numerical analysis purposes; there are better approximations that converge faster.


Recent posts

2024.07.09: Asking an LLM to build a simple web tool
2024.07.04: Locally patching dependencies in Go
2024.06.30: Summary of reading: April - June 2024
2024.06.26: Projections and Projection Matrices
2024.05.31: Reading Google Sheets from a Go program
2024.04.25: Tokens for LLMs: Byte Pair Encoding in Go
2024.04.16: Method of differences and Newton polynomials
2024.03.31: Summary of reading: January - March 2024
2024.03.06: The life of an Ollama prompt
2024.02.22: Gemma, Ollama and LangChainGo

See Archives for a full list.