Mathematics Basics - Multivariate Calculus (Taylor Series)

Taylor Series

We discussed in Jacobian chapter some challenges with applying Jacobian in reality. One of the challenges is when there is no clearly defined analytical function or that function is too complicated to be differentiated. To tackle this, we have to resort to numerical methods and come up with an approximation to the actual function. Taylor series is one such method. It is used to approximate a complex function by a series of simpler functions. In the univariate case, Taylor series usually takes a polynomial form like below.

g0(x)=ag1(x)=a+bxg2(x)=a+bx+cx2g3(x)=a+bx+cx2+dx3 \begin{aligned} g_0(x)&=a\\ g_1(x)&=a+bx\\ g_2(x)&=a+bx+cx^2\\ g_3(x)&=a+bx+cx^2+dx^3\\ \vdots \end{aligned}

We call g0(x)g_0(x) the zeroth order approximation, g1(x)g_1(x) the first order approximation, g2(x)g_2(x) the second order approximation, g3(x)g_3(x) the third order approximation and so on. The order is dictated by the highest power xx term in the polynomial expression. So Taylor series is just a series of polynomial expressions with increasing power of xx terms. You will soon see that as the power increases, the resultant polynomial expression gets more and more accurate at approximating the original function.

Next, we will use an example to demonstrate how to find the coefficients aa, bb, cc, dd etc. used in the Taylor series function. We take a function f(x)=exf(x)=e^x and build a series of polynomial functions to approximate it.

Mathematics Basics - Multivariate Calculus (Taylor Series)

Before we start, let’s first note an interesting property of the function f(x)=exf(x)=e^x. No matter how many times we differentiate f(x)f(x), we will always get the same result exe^x. Therefore,

f(x)=f(1)(x)=f(2)(x)==f(n)(x)=ex f(x)=f^{(1)}(x)=f^{(2)}(x)=\cdots=f^{(n)}(x)=e^x​

We use superscript (n)(n) to denote differentiating the function nn times with respect to xx.

In Taylor Series we can only approximate function ff at one particular point. Here we choose our point of interest at x=0x=0. Substitute this into the derivatives, we get

f(0)=f(1)(0)=f(2)(0)==f(n)(0)=e0=1 f(0)=f^{(1)}(0)=f^{(2)}(0)=\cdots=f^{(n)}(0)=e^0=1

With all those preparation steps in place, we can start with the zeroth order approximation g0(x)=ag_0(x)=a. Since zeroth order approximation is just a constant, the best approximation we can obtain at x=0x=0 is a=f(0)a=f(0). So

g0(x)=f(0)=1 g_0(x)=f(0)=1
Mathematics Basics - Multivariate Calculus (Taylor Series)

This does not look like a very good approximation. Let’s try the first order approximation.

For first order approximation, we are trying to find a line g1(x)=a+bxg_1(x)=a+bx that satisfies the following equations.

g1(x)=f(x)g1(1)(x)=f(1)(x) \begin{aligned} g_1(x)&=f(x)\\ g_1^{(1)}(x)&=f^{(1)}(x) \end{aligned}

We know the first order derivative of g1(x)g_1(x) is

g1(1)(x)=ddx(a+bx)=b g_1^{(1)}(x)=\frac{d}{dx}(a+bx)=b

Substitute the expression for g1(x)g_1(x) and g1(1)(x)g_1^{(1)}(x) into the simultaneous equation,

a+bx=f(x)b=f(1)(x) \begin{aligned} a+bx&=f(x)\\ b&=f^{(1)}(x)\\ \end{aligned}

At point x = 0,

a+b(0)=f(0)b=f(1)(0) \begin{aligned} a+b(0)&=f(0)\\ b&=f^{(1)}(0) \end{aligned}

Therefore, we obtain the solution for aa and bb in first order approximation function as,

g1(x)=f(0)+f(1)(0)x=e0+e0x=1+x \begin{aligned} g_1(x)&=f(0)+f^{(1)}(0)x\\ &=e^0+e^0x\\ &=1+x \end{aligned}

We can plot this line together with original function ff.
Mathematics Basics - Multivariate Calculus (Taylor Series)

Our approximation has improved a little, but this is still not good enough. Let’s move on to the second order approximation.

In second order approximation, we are solving a quadratic function g2(x)=a+bx+cx2g_2(x)=a+bx+cx^2. This function g2(x)g_2(x) must satisfy the following equations

g2(x)=f(x)g2(1)(x)=f(1)(x)g2(2)(x)=f(2)(x) \begin{aligned} g_2(x)&=f(x)\\ g_2^{(1)}(x)&=f^{(1)}(x)\\ g_2^{(2)}(x)&=f^{(2)}(x) \end{aligned}

We can calculate the first and second order derivatives of g2(x)g_2(x) as,

g2(1)(x)=ddx(a+bx+cx2)=b+2cxg2(2)(x)=ddx(b+2cx)=2c \begin{aligned} g_2^{(1)}(x)&=\frac{d}{dx}(a+bx+cx^2)=b+2cx\\ g_2^{(2)}(x)&=\frac{d}{dx}(b+2cx)=2c\\ \end{aligned}

Substitute these back to our previous simultaneous equation,

a+bx+cx2=f(x)b+2cx=f(1)(x)2c=f(2)(x) \begin{aligned} a+bx+cx^2&=f(x)\\ b+2cx&=f^{(1)}(x)\\ 2c&=f^{(2)}(x) \end{aligned}

At point x=0x=0,

a+b(0)+c(0)2=f(0)b+2c(0)=f(1)(0)2c=f(2)(0) \begin{aligned} a+b(0)+c(0)^2&=f(0)\\ b+2c(0)&=f^{(1)}(0)\\ 2c&=f^{(2)}(0) \end{aligned}

The solution for our coefficients aa, bb and cc is simply

a=f(0)b=f(1)(0)c=12f(2)(0) \begin{aligned} a&=f(0)\\ b&=f^{(1)}(0)\\ c&=\frac{1}{2}f^{(2)}(0) \end{aligned}

So our second order approximation can be evaluated as,

g2(x)=f(0)+f(1)(0)x+12f(2)(0)x2=e0+e0x+12e0x2=1+x+12x2 \begin{aligned} g_2(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2\\ &=e^0+e^0x+\frac{1}{2}e^0x^2\\ &=1+x+\frac{1}{2}x^2 \end{aligned}

We plot this second order approximation function together with the original function ff.
Mathematics Basics - Multivariate Calculus (Taylor Series)

This looks better now. Can we be more accurate? Let’s solve the third order approximation which is a cubic function g3(x)=a+bx+cx2+dx3g_3(x)=a+bx+cx^2+dx^3. Again, it must satisfy the following equations.

g3(x)=f(x)g3(1)(x)=f(1)xg3(2)(x)=f(2)xg3(3)(x)=f(3)x \begin{aligned} g_3(x)&=f(x)\\ g_3^{(1)}(x)&=f^{(1)}x\\ g_3^{(2)}(x)&=f^{(2)}x\\ g_3^{(3)}(x)&=f^{(3)}x\\ \end{aligned}

The derivatives of g3(x)g_3(x) are given by

g3(1)(x)=ddx(a+bx+cx2+dx3)=b+2cx+3dx2g3(2)(x)=ddx(b+2cx+3dx2)=2c+6dxg3(3)(x)=ddx(2c+6dx)=6d \begin{aligned} g_3^{(1)}(x)&=\frac{d}{dx}(a+bx+cx^2+dx^3)=b+2cx+3dx^2\\ g_3^{(2)}(x)&=\frac{d}{dx}(b+2cx+3dx^2)=2c+6dx\\ g_3^{(3)}(x)&=\frac{d}{dx}(2c+6dx)=6d \end{aligned}

Substitute these back and evaluate at point x=0x=0.

a+b(0)+c(0)2+d(0)3=f(0)b+2c(0)+3d(0)2=f(1)(0)2c+6d(0)=f(2)(0)6d=f(3)(0) \begin{aligned} a+b(0)+c(0)^2+d(0)^3&=f(0)\\ b+2c(0)+3d(0)^2&=f^{(1)}(0)\\ 2c+6d(0)&=f^{(2)}(0)\\ 6d&=f^{(3)}(0) \end{aligned}

The coefficients for our approximation function g3(x)g_3(x) are

a=f(0)b=f(1)(0)c=12f(2)(0)d=16f(3)(0) \begin{aligned} a&=f(0)\\ b&=f^{(1)}(0)\\ c&=\frac{1}{2}f^{(2)}(0)\\ d&=\frac{1}{6}f^{(3)}(0) \end{aligned}

Therefore, the third order approximation function is

g3(x)=f(0)+f(1)(0)x+12f(2)(0)x2+16f(3)(0)x2=e0+e0x+12e0x2+16e0x3=1+x+12x2+16x3 \begin{aligned} g_3(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2+\frac{1}{6}f^{(3)}(0)x^2\\ &=e^0+e^0x+\frac{1}{2}e^0x^2+\frac{1}{6}e^0x^3\\ &=1+x+\frac{1}{2}x^2+\frac{1}{6}x^3 \end{aligned}

Let’s plot the third order approximation function in the chart.
Mathematics Basics - Multivariate Calculus (Taylor Series)
It looks like we are getting better and better at approximating the function f(x)=exf(x)=e^x. Before we get carried over with higher order approximations, let’s pause for a second and look at the approximation functions we have obtained so far.

g0(x)=f(0)g1(x)=f(0)+f(1)(0)xg2(x)=f(0)+f(1)(0)x+12f(2)(0)x2g3(x)=f(0)+f(1)(0)x+12f(2)(0)x2+16f(3)(0)x3 \begin{aligned} g_0(x)&=f(0)\\ g_1(x)&=f(0)+f^{(1)}(0)x\\ g_2(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2\\ g_3(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2+\frac{1}{6}f^{(3)}(0)x^3\\ \end{aligned}

A nice pattern is revealed. Instead of explicitly writing out coefficients for higher order approximation functions, we can derive a a general expression for the nthn^{th} order approximation for function ff at point x=0x=0 as

gn(x)=n=0nf(n)(0)n!xn g_n(x)=\sum_{n=0}^{n}\frac{f^{(n)}(0)}{n!}x^n

Note that when nn goes to infinity, the approximation function g(x)g(x) becomes exactly the same as the original function f(x)f(x).

f(x)=n=0f(n)(0)n!xn f(x)=\sum_{n=0}^{\infty}\frac{f^{(n)}(0)}{n!}x^n

With this general expression, we can easily tell the fourth order approximation is

g4(x)=f(0)+f(1)(0)x+12f(2)(0)x2+16f(3)(0)x3+124f(4)(0)x4=e0+e0x+12e0x2+16e0x3+124e0x4=1+x+12x2+16x3+124x4 \begin{aligned} g_4(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2+\frac{1}{6}f^{(3)}(0)x^3+\frac{1}{24}f^{(4)}(0)x^4\\ &=e^0+e^0x+\frac{1}{2}e^0x^2+\frac{1}{6}e^0x^3+\frac{1}{24}e^0x^4\\ &=1+x+\frac{1}{2}x^2+\frac{1}{6}x^3+\frac{1}{24}x^4 \end{aligned}

And the fifth order approximation is

g5(x)=f(0)+f(1)(0)x+12f(2)(0)x2+16f(3)(0)x3+124f(4)(0)x4+1120f(5)(0)x5=e0+e0x+12e0x2+16e0x3+124e0x4+1120e0x5=1+x+12x2+16x3+124x4+1120x5 \begin{aligned} g_5(x)&=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2+\frac{1}{6}f^{(3)}(0)x^3+\frac{1}{24}f^{(4)}(0)x^4+\frac{1}{120}f^{(5)}(0)x^5\\ &=e^0+e^0x+\frac{1}{2}e^0x^2+\frac{1}{6}e^0x^3+\frac{1}{24}e^0x^4+\frac{1}{120}e^0x^5\\ &=1+x+\frac{1}{2}x^2+\frac{1}{6}x^3+\frac{1}{24}x^4+\frac{1}{120}x^5 \end{aligned}

This is a very powerful formula. It means as long as we can keep on differentiating a function at point x=0x=0, we can use these derivative terms to reconstruct the function everywhere else. Moreover, the polynomial function we derive from this formula can approximate the actual function at an arbitrary degree of accuracy. This formula for approximating a function at point x=0x=0 was first discovered by mathematician Colin Maclaurin. Therefore, it is also called Maclaurin series.

You might be wondering what is so special about the point x=0x=0 that would allow us to find an approximation function easily. It turns out there is nothing special. Maclaurin series is just one special case of the more general Taylor series which can be evaluated at any point x=px=p. The general form of Taylor series is given by

f(x)=n=0f(n)(p)n!(xp)n f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(p)}{n!}(x-p)^n

According to the Taylor series formula, we can rewrite our zeroth to fifth order approximation function as

g0(x)=f(p)g1(x)=f(p)+f(1)(p)(xp)g2(x)=f(p)+f(1)(p)(xp)+12f(2)(p)(xp)2g3(x)=f(p)+f(1)(p)(xp)+12f(2)(p)(xp)2+16f(3)(p)(xp)3g4(x)=f(p)+f(1)(p)(xp)+12f(2)(p)(xp)2+16f(3)(p)(xp)3+124f(4)(p)(xp)4g5(x)=f(p)+f(1)(p)(xp)+12f(2)(p)(xp)2+16f(3)(p)(xp)3+124f(4)(p)(xp)4+1120f(5)(p)(xp)5 \begin{aligned} g_0(x)&=f(p)\\ g_1(x)&=f(p)+f^{(1)}(p)(x-p)\\ g_2(x)&=f(p)+f^{(1)}(p)(x-p)+\frac{1}{2}f^{(2)}(p)(x-p)^2\\ g_3(x)&=f(p)+f^{(1)}(p)(x-p)+\frac{1}{2}f^{(2)}(p)(x-p)^2+\frac{1}{6}f^{(3)}(p)(x-p)^3\\ g_4(x)&=f(p)+f^{(1)}(p)(x-p)+\frac{1}{2}f^{(2)}(p)(x-p)^2+\frac{1}{6}f^{(3)}(p)(x-p)^3+\frac{1}{24}f^{(4)}(p)(x-p)^4\\ g_5(x)&=f(p)+f^{(1)}(p)(x-p)+\frac{1}{2}f^{(2)}(p)(x-p)^2+\frac{1}{6}f^{(3)}(p)(x-p)^3+\frac{1}{24}f^{(4)}(p)(x-p)^4+\frac{1}{120}f^{(5)}(p)(x-p)^5 \end{aligned}

Compared to the Maclaurin series, Taylor series can be applied to any point x=px=p as long as function ff is differentiable at this point. We change from differentiating at x=0x=0 to differentiating at x=px=p. Consequently, the xx term is changed from xnx^n to (xp)n(x-p)^n. It is easy to see that Maclairin series is just Taylor series when p=0p=0.

Of course, we are not building the Taylor series to approximate a simple function like f(x)=exf(x)=e^x. Taylor series is most useful when we have a complex function that can’t be differentiated easily. For such complex functions, we can find its gradient at one particular point and derive a polynomial expression to re-express it. This is much simpler than carrying out the standard differentiation process for every point of the function.

In addition, Taylor series approximation is most accurate around the point it is approximating. The accuracy drops as we move away from this point. Luckily, in real word applications we seldom need to obtain the derivatives of a function at every point. Instead, we only need to focus on a specific range of input values and understand how they influence the final output. This is exactly what Taylor series can be used for.

More Taylor Series Examples

Let’s look at another commonly used function f(x)=cos(x)f(x)=\cos(x) and find its approximations.
Mathematics Basics - Multivariate Calculus (Taylor Series)

We first observe that there is a cyclic pattern in the derivatives of f(x)f(x). The derivatives cycle between cosine and sine and between positive and negative. Moreover, the same terms appear after every 4th differentiation.

f(x)=cos(x)f(1)(x)=sin(x)f(2)(x)=cos(x)f(3)(x)=sin(x)f(4)(x)=cos(x) \begin{aligned} f(x)&=\cos(x)\\ f^{(1)}(x)&=-\sin(x)\\ f^{(2)}(x)&=-\cos(x)\\ f^{(3)}(x)&=\sin(x)\\ f^{(4)}(x)&=\cos(x)\\ \vdots \end{aligned}

We can substitute the value x=0x=0 and see what these derivatives evaluate to.

f(0)=cos(0)=1f(1)(0)=sin(0)=0f(2)(0)=cos(0)=1f(3)(0)=sin(0)=0f(4)(0)=cos(0)=1 \begin{aligned} f(0)&=\cos(0)=1\\ f^{(1)}(0)&=-\sin(0)=0\\ f^{(2)}(0)&=-\cos(0)=-1\\ f^{(3)}(0)&=\sin(0)=0\\ f^{(4)}(0)&=\cos(0)=1\\ \vdots \end{aligned}

In this case, the derivatives are 0 whenever we differentiate the function odd number of times (f(1)(0),f(0)(x),,f(2n+1)(0)f^{(1)}(0),f^{(0)}(x),\cdots,f^{(2n+1)}(0)). One the other hand, the derivatives are flipping between 11 and 1-1 whenever we differentiate the function even number of times (f(2)(0),f(4)(0),,f(2n)(0)f^{(2)}(0),f^{(4)}(0),\cdots,f^{(2n)}(0)). Based on this observation, we can derive the Maclaurin series for approximating function f(x)=cos(x)f(x)=\cos(x) at point x=0x=0 as,

f(x)=n=0f(n)(0)n!xn=f(0)+f(1)(0)x+12f(2)(0)x2+16f(3)(0)x3+124f(4)(0)x4+=1+(0)x+12(1)x2+16(0)x3+124(1)x4+=n=0(1)n(2n)!x2n \begin{aligned} f(x)&=\sum_{n=0}^{\infty}\frac{f^{(n)}(0)}{n!}x^n\\ &=f(0)+f^{(1)}(0)x+\frac{1}{2}f^{(2)}(0)x^2+\frac{1}{6}f^{(3)}(0)x^3+\frac{1}{24}f^{(4)}(0)x^4+\cdots\\ &=1+(0)x+\frac{1}{2}(-1)x^2+\frac{1}{6}(0)x^3+\frac{1}{24}(1)x^4+\cdots\\ &=\sum_{n=0}^\infty\frac{(-1)^n}{(2n)!}x^{2n} \end{aligned}

Let’s plot out the fourth order approximation function. It shows a very closed match to the original function around the region x=0x=0.

g4(x)=1x22+x424 g_4(x)=1-\frac{x^2}{2}+\frac{x^4}{24}
Mathematics Basics - Multivariate Calculus (Taylor Series)

We call the function f(x)=cos(x)f(x)=\cos(x) a well-behaved function because it has two properties.

  1. It is continuous everywhere. You can traverse along the curve of this function and not find any sudden break.
  2. It can be differentiated infinitely. The same cyclic pattern will repeat over and over as you differentiate the function in higher orders.

These two properties are very important prerequisites for us to successfully derive a Maclaurin or Taylor series in general. We can use another function f(x)=1xf(x)=\frac{1}{x} to demonstrate why these two properties matter so much.
Mathematics Basics - Multivariate Calculus (Taylor Series)

Based on the plot, we can see that f(x)=1xf(x)=\frac{1}{x} is not a continuous function. The curve breaks at x=0x=0. Therefore, even we can infinitely differentiate this function as follows, it is not a well-behaved function.

f(x)=1xf(1)(x)=1x2f(2)(x)=2x3f(3)(x)=6x4f(4)(x)=24x5 \begin{aligned} f(x)&=\frac{1}{x}\\ f^{(1)}(x)&=-\frac{1}{x^2}\\ f^{(2)}(x)&=\frac{2}{x^3}\\ f^{(3)}(x)&=-\frac{6}{x^4}\\ f^{(4)}(x)&=\frac{24}{x^5}\\ \vdots \end{aligned}

It is quite clear that we cannot use Maclaurin series for approximation because all the derivatives of function ff are undefined at x=0x=0. Nevertheless, we can use Taylor series to approximate this function at a different point. For example, at x=1x=1, we can evaluate the derivatives of f(x)f(x) as

f(1)=11=1f(1)(1)=112=1f(2)(1)=213=2f(3)(1)=614=6f(4)(1)=2415=24 \begin{aligned} f(1)&=\frac{1}{1}=1\\ f^{(1)}(1)&=-\frac{1}{1^2}=-1\\ f^{(2)}(1)&=\frac{2}{1^3}=2\\ f^{(3)}(1)&=-\frac{6}{1^4}=-6\\ f^{(4)}(1)&=\frac{24}{1^5}=24\\ \vdots \end{aligned}

Notice that the derivatives are evaluated to be a positive number whenever we differentiate function ff even number of times (f(2)(1),f(4)(1),,f(2n)(1)f^{(2)}(1),f^{(4)}(1),\cdots,f^{(2n)}(1)). Conversely, the derivatives are evaluated to be a negative number whenever we differentiate function ff odd number of times (f(1)(1),f(3)(1),,f(2n+1)(1)f^{(1)}(1),f^{(3)}(1),\cdots,f^{(2n+1)}(1)).

Substituting these values into our Taylor series formula, we can obtain the following approximation function for f=1xf=\frac{1}{x}.

f(x)=n=0f(n)(1)n!(x1)n=f(1)+f(1)(1)(x1)+12f(2)(1)(x1)2+16f(3)(1)(x1)3+124f(4)(1)(x1)4+=1+(1)(x1)+12(2)(x1)2+16(6)(x1)3+124(24)(x1)4+=1(x1)+(x1)2(x1)3+(x1)4+=n=0(1)n(x1)n \begin{aligned} f(x)&=\sum_{n=0}^\infty\frac{f^{(n)}(1)}{n!}(x-1)^n\\ &=f(1)+f^{(1)}(1)(x-1)+\frac{1}{2}f^{(2)}(1)(x-1)^2+\frac{1}{6}f^{(3)}(1)(x-1)^3+\frac{1}{24}f^{(4)}(1)(x-1)^4+\cdots\\ &=1+(-1)(x-1)+\frac{1}{2}(2)(x-1)^2+\frac{1}{6}(-6)(x-1)^3+\frac{1}{24}(24)(x-1)^4+\cdots\\ &=1-(x-1)+(x-1)^2-(x-1)^3+(x-1)^4+\cdots\\ &=\sum_{n=0}^\infty(-1)^n(x-1)^n \end{aligned}

How good are we with the approximation this time? Let’s plot the fourth order approximation function below.

g4(x)=1(x1)+(x1)2(x1)3+(x1)4 g_4(x)=1-(x-1)+(x-1)^2-(x-1)^3+(x-1)^4
Mathematics Basics - Multivariate Calculus (Taylor Series)

We have some degree of closeness of the two curves, especially around the region x=1x=1. There are also some interesting features we can observe here. Firstly, the region of the original function where xx is less than 0 is not covered at all by this approximation function. Secondly, the approximation function ignores the asymptote at x=0x=0 and goes straight across the y-axis. Lastly, the approximation function did a bad job for points with larger values of x. This does not get improved much with higher order approximation. The “tail” is wildly flailing up and down with higher power terms (verify this by yourself).

Therefore, we can see that Taylor series does not approximate well if a function is not well-behaved, i.e. continuous everywhere and indefinitely differentiable. Luckily, most of the functions we see in real world can fulfill these two requirements. Even for functions that are discontinuous like f(x)=1xf(x)=\frac{1}{x}, we can try to approximate it by deriving piece-wise Taylor series expressions. For example, we can have one approximation at x=1x=1 and the other one at x=1x=-1 to cover both the positive and negative xx values of f(x)=1xf(x)=\frac{1}{x}. As for functions that can only be differentiated finite number of times, we are just approximating it with a certain level of accuracy determined by the highest order differentiation term. We will have to ensure this approximation accuracy is acceptable for the input range we are interested in.

Linearization

We have learned that Taylor series can be expanded into infinite number of terms (as long as the function can be differentiated). However, in practice we often just evaluate the first few terms, and ignore the other higher order terms. This is called the truncated form of Taylor series. We will see where this trucked form is used.

Recall that a Taylor series is given by

f(x)=n=0f(n)(p)n!(xp)n f(x)=\sum_{n=0}^\infty\frac{f^{(n)}(p)}{n!}(x-p)^n

This is evaluated at point x=px=p. At some distance Δp\Delta p away from this point, the same Taylor series will give us the value of the function as

f(p+Δp)=n=0f(n)(p)n!(Δp)n f(p+\Delta p)=\sum_{n=0}^\infty\frac{f^{(n)}(p)}{n!}(\Delta p)^n

By convention, we will rewrite all the pp's as xx's because they are in fact the same. So the function becomes

f(x+Δx)=n=0f(n)(x)n!(Δx)n=f(x)+f(1)(x)(Δx)+12f(2)(x)(Δx)2+16f(3)(x)(Δx)3+124f(4)(x)(Δx)4+ \begin{aligned} f(x+\Delta x)&=\sum_{n=0}^\infty\frac{f^{(n)}(x)}{n!}(\Delta x)^n\\&=f(x)+f^{(1)}(x)(\Delta x)+\frac{1}{2}f^{(2)}(x)(\Delta x)^2+\frac{1}{6}f^{(3)}(x)(\Delta x)^3+\frac{1}{24}f^{(4)}(x)(\Delta x)^4+\cdots \end{aligned}

Let’s rearrange this expression to find the first order derivative of function ff.

f(1)(x)=f(x+Δx)f(x)Δx12f(2)(x)(Δx)16f(3)(x)(Δx)2124f(4)(x)(Δx)3 f^{(1)}(x)=\frac{f(x+\Delta x)-f(x)}{\Delta x}-\frac{1}{2}f^{(2)}(x)(\Delta x)-\frac{1}{6}f^{(3)}(x)(\Delta x)^2-\frac{1}{24}f^{(4)}(x)(\Delta x)^3-\cdots

Check the first term of this expression, f(x+Δx)f(x)Δx\frac{f(x+\Delta x)-f(x)}{\Delta x}. This looks very similar to the rise-over-run equation we derived at the beginning to estimate gradient of a function at a point. Except now we have some additional terms to tell us how good we are at estimating the gradient. We can lump these additional terms up as an error term and denote it by O(Δx)O(\Delta x).

O(Δx)=12f(2)(x)(Δx)16f(3)(x)(Δx)2124f(4)(x)(Δx)3 O(\Delta x)=-\frac{1}{2}f^{(2)}(x)(\Delta x)-\frac{1}{6}f^{(3)}(x)(\Delta x)^2-\frac{1}{24}f^{(4)}(x)(\Delta x)^3-\cdots

If we were careful with choosing Δx\Delta x when estimating the gradient, this Δx\Delta x would be evaluated to a very small number. Then (Δx)2(\Delta x)^2, (Δx)3(\Delta x)^3 and all the higher order terms would be even smaller. Therefore, the first term we omitted in the Taylor series expansion, 12f(2)(x)(Δx)\frac{1}{2}f^{(2)}(x)(\Delta x) is a good indicator of the size of error incurred with the estimation. This estimation is said to be first order accurate since its first omitted term is Δx\Delta x. Gradient of a function can then be expressed as

f(1)(x)=f(x+Δx)f(x)Δx+O(Δx) f^{(1)}(x)=\frac{f(x+\Delta x)-f(x)}{\Delta x}+O(\Delta x)

As a result, we know a linear approximation of function ff at some distance Δx\Delta x away from the approximation point and we know how to measure the approximation error by Δx\Delta x.

f(x+Δx)f(x)+f(1)(x)(Δx) f(x+\Delta x)\approx f(x)+f^{(1)}(x)(\Delta x)

This process of taking a function and ignoring all the higher order terms above Δx\Delta x is called linearization and is often used by computers to solve derivative of a function numerically rather than analytically.

One practical use of linearization is when we know the value of a function at one point and we want to estimate the value at another point. A simple example is estimating 4.01\sqrt{4.01} without the help of a calculator.

We can first define our square-root function f(x)f(x) as

f(x)=x f(x)=\sqrt{x}

The linearized approximation of f(x)f(x) would be

f(x+Δx)f(x)+ddxx(Δx)x+12x(Δx) \begin{aligned} f(x+\Delta x)&\approx f(x)+\frac{d}{dx}\sqrt{x}(\Delta x)\\ &\approx \sqrt{x}+\frac{1}{2\sqrt{x}}(\Delta x) \end{aligned}

We already know that 4=2\sqrt{4}=2. So we can approximate function f(x)f(x) at x=4x=4 first and calculate the value at distance Δx=0.01\Delta x=0.01 away from this approximation point.

4.01=f(4+0.01)4+124(0.01)2+14×0.012.0025 \begin{aligned} \sqrt{4.01}&=f(4+0.01)\\ &\approx \sqrt{4}+\frac{1}{2\sqrt{4}}(0.01)\\ &\approx 2+\frac{1}{4}\times0.01\\ &\approx 2.0025 \end{aligned}

This result is very closed to the actual value of 4.01=2.002498\sqrt{4.01}=2.002498. It shows our linearization method can generate a good enough approximation if we have no better way to solve the equation analytically.

In reality, there are some functions which are computationally too expensive to evaluate at every single point. Therefore, if we know its value at one point, we can make use of the linearization method to approximate its value at another point close by. It is much easier to perform such operations by a computer.

We have gone through quite a few heavy topics regarding Taylor series and its applications. Taylor series is an important tool for us to solve complex functions by close approximations. It enables computers to efficiently evaluate functions at different points. We will see it being used a lot in advanced machine learning algorithms.


(Inspired by Mathematics for Machine Learning lecture series from Imperial College London)