# Derivative Approximation via Finite Difference Methods

Derivative Approximation via Finite Difference Methods

This post is part of a series of Finite Difference Method Articles. Other posts in the series concentrate on Solving The Heat/Diffusion Equation Explicitly, the Crank-Nicolson Implicit Method and the Tridiagonal Matrix Solver/Thomas Algorithm:

This is the first in a multi-part tutorial series on using Finite Difference Methods (FDM) to numerically solve parabolic partial differential equations. In particular, the heat equation will be solved. Given that the Black-Scholes pricing equation can be transformed into a heat equation, these methods can be applied to solve the equation numerically.

If we let $f \in C^2(\mathbb{R})$, i.e. let f be a continuous function of the real numbers which is twice differentiable, then the heat equation for $f$ is given by:

$\frac{\partial f}{\partial t} = \frac{\partial^2 f}{\partial x^2}$

### Discretising Derivatives

The Finite Difference Method provides a numerical solution to this equation via the discretisation of its derivatives. The derivatives will be approximated via a Taylor Series expansion. Recall that a Taylor Series provides a value for a function $f=f(x)$ when the dependent variable $x \in \mathbb{R}$ is translated by an amount $\Delta x$, in terms of its derivatives at that point. Thus, $f(x+\Delta x)$ can be calculated by:

$f(x+\Delta x) = f(x) + f'(x)\Delta x + \frac{f''(x)(\Delta x)^2}{2!} + \frac{f'''(x)(\Delta x)^3}{3!} + O(\Delta x)^4$

Note that $O(\Delta x)$ represents additional terms that are bounded by a constant term multipled by $\Delta x$.

In a similar manner, we can determine $f$ when the dependent variable $x$ is translated by an identical amount $\Delta x$ in the opposite direction:

$f(x-\Delta x) = f(x) - f'(x)\Delta x + \frac{f''(x)(\Delta x)^2}{2!} - \frac{f'''(x)(\Delta x)^3}{3!} + O(\Delta x)^4$

These two equations when rearranged allow a first order approximation for the derivative $f'(x)$:

\begin{aligned} f'(x) & = \frac{f(x+\Delta x)-f(x)}{\Delta x} + O(\Delta x) \\ & = \frac{f(x)-f(x-\Delta x)}{\Delta x} + O(\Delta x) \\ \end{aligned}

In addition, subtracting the backward Taylor expansion from the forward Taylor expansion provides a second order approximation for the derivative $f'(x)$:

$f'(x) = \frac{f(x+\Delta x)-f(x-\Delta x)}{2 \Delta x} + O(\Delta x)^2$

To obtain a second order approximation for the second derivative of $f$, $f''(x)$, we can add the forward and backward Taylor expansions to receive:

$f''(x) = \frac{f(x+\Delta x)-2f(x)+f(x-\Delta x)}{(\Delta x)^2} + O(\Delta x)^2$

### Domain Discretisation

Now that the derivatives have been approximated in a one-dimensional continuous setting, it is time to discretise the domain of the heat equation. The function $f$ is now a function of two real variables $x$ and $t$ which represent space and time, respectively. The function $f=f(x,t)$ can be thought of as the quantity of heat at any point along a one-dimensional bar.

The 1D continuous bar can be discretised into $I$ separate points $1,2,...,i,...,I$. Time can be discretised into $N$ separate points $1,2,...,n,...,N$, each separated by a time-spacing $\Delta t$. The approximation $f^n_i$ to $f$ at time $n$ and location $x=i\Delta x$ is given by:

$f^n_i \approx f(i\Delta x, n\Delta t)$

Note that the superscript $n$ does not refer to a power of $f$ or its $n$th derivative, instead it refers to its value at the time $n \Delta t$. Thus, the aforementioned Taylor approximations to the derivatives can be written as:

\begin{aligned} f'(i\Delta x, n\Delta t) & \approx \frac{f^n_{i+1} - f^n_i}{\Delta x} \\ & \approx \frac{f^n_i - f^n_{i-1}}{\Delta x} \\ & \approx \frac{f^n_{i+1} - f^n_{i-1}}{2\Delta x} \\ f''(i\Delta x, n\Delta t) & \approx \frac{f^n_{i+1} - 2 f^n_i + f^n_{i-1}}{(\Delta x)^2} \\ \end{aligned}

They are, respectively, the forward difference, backward difference, centered difference and centered second difference.

### Errors and Conclusion

Given that the differences are approximations it is natural to question how errors can arise in the overall solution. In fact, two types of errors can occur. They are the truncation error and the roundoff error. Truncation error is the error associated with the discretisation of the derivatives, i.e. those occuring due to the additional higher order derivative terms.

Roundoff error is that which occurs due to the precision of the solution being stored on a computer. Usually double precision floating point values are used which store 16 digits for each value. These errors can accumulate, particularly if single precision is used, which only stores 8 digits for floating point values.

In the next tutorial the heat equation will be solved numerically. It will be seen how truncation error can rapidly accumulate if certain conditions are not enforced.