Finite Difference Coefficients Calculator


Finite difference equations enable you to take derivatives of any order at any point using any given sufficiently-large selection of points. By inputting the locations of your sampled points below, you will generate a finite difference equation which will approximate the derivative at any desired location.

To take a numerical derivative, you calculate the slope at a point using the values and relative locations of surrounding points. The locations of these sampled points are collectively called the finite difference stencil. This calculator accepts as input any finite difference stencil and desired derivative order and dynamically calculates the coefficients for the finite difference equation. You may be familiar with the backward difference derivative $$\frac{\partial f}{\partial x}=\frac{f(x)-f(x-h)}{h}$$ This is a special case of a finite difference equation (where \(f(x)-f(x-h)\) is the finite difference and \(h\) is the spacing between the points) and can be displayed below by entering the finite difference stencil {-1,0} for Locations of Sampled Points and 1 for Derivative Order

The finite difference coefficients calculator can be used generally for any finite difference stencil and any derivative order. Notable cases include the forward difference derivative, {0,1} and 1, the second-order central difference, {-1,0,1} and 2, and the fourth-order five-point stencil, {-2,-1,0,1,2} and 4. However, it is encouraged that you explore additional combinations of forward, backward, central, and otherwise offset stencils, as well as irregular and non-integer sampling stencils, such as {-7.1,-4.2,2.4,3.14,4.7} and 4.

Locations of Sampled Points


Derivative Order


Finite Difference Equation

$$\frac{\partial^{(1)}f}{\partial x^{(1)}}\approx$$

Python Code

	
	Code Here
	
	

I was taking Gilbert Strang's Computational Science and Engineering Class from Norbert Stoop, and Professor Stoop asked us to determine the coefficients for the difference equation $$\frac{\partial^4f}{\partial x^4} \approx Af(x-2h) + Bf(x-h) + Cf(x) + Df(x+h) + Ef(x+2h) $$ I asked him after class if he could recommend a strategy for solving the problem methodically, and he suggested I consider Taylor expansions. The formula for a Taylor expansion is $$f(b)=f(a)+\frac{f'(a)}{1!}(b-a)+\frac{f''(a)}{2!}(b-a)^2+\ldots$$ so, after some serious thought, I began by expanding the first term, \(Af(x-2h)\), with \(b=x-2h\) and \(a=x\), getting $$ A f(x) +A f'(x) (-2h) +A \frac{1}{2} f''(x) (-2h)^2 +A \frac{1}{6} f'''(x) (-2h)^3 +A \frac{1}{24} f''''(x) (-2h)^4 +A \frac{1}{120} f'''''(x) (-2h)^5 +\ldots $$ I then did the same for all of the other terms in the numerator $$ +B f(x) +B f'(x) (-h) +B \frac{1}{2} f''(x) (-h)^2 +B \frac{1}{6} f'''(x) (-h)^3 +B \frac{1}{24} f''''(x) (-h)^4 +B \frac{1}{120} f'''''(x) (-h)^5 +\ldots $$ $$ +C f(x) $$ $$ +D f(x) +D f'(x) (h) +D \frac{1}{2} f''(x) (h)^2 +D \frac{1}{6} f'''(x) (h)^3 +D \frac{1}{24} f''''(x) (h)^4 +D \frac{1}{120} f'''''(x) (h)^5 +\ldots $$ $$ +E f(x) +E f'(x) (2h) +E \frac{1}{2} f''(x) (2h)^2 +E \frac{1}{6} f'''(x) (2h)^3 +E \frac{1}{24} f''''(x) (2h)^4 +E \frac{1}{120} f'''''(x) (2h)^5 +\ldots $$ Now since I needed to choose \(A, B, C, D\) and \(E\) such that the summation of all of these terms results in the cancellation of all \(f(x)\), \(f'(x)\), \(f''(x)\), and \(f'''(x)\) terms and results in the \(f''''(x)\) coefficients summing to one, this requires us to fulfill the equations $$A+B+C+D+E=0 $$ $$-2A-B+D+2E=0 $$ $$4A+B+D+4E=0 $$ $$-8A-B+D+8E=0 $$ $$16A+B+D+16E=\frac{24}{h^4} $$ These can be rewritten in general form as $$(-2)^nA+(-1)^nB+C\delta(n)+1^nD+2^nE=\frac{4!}{h^4}\delta(n-4) \mbox{ for } 0\leq n\leq4 $$ or in matrix form as $$ \begin{bmatrix} 1 & 1 & 1 & 1 & 1 \\ -2 & -1 & 0 & 1 & 2 \\ 4 & 1 & 0 & 1 & 4 \\ -8 & -1 & 0 & 1 & 8 \\ 16 & 1 & 0 & 1 & 16 \\ \end{bmatrix} \begin{bmatrix} A \\ B \\ C \\ D \\ E \end{bmatrix} = \frac{1}{h^4} \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 24 \end{bmatrix} $$ Now this matrix is invertible, and the solution is $$ \begin{bmatrix} A \\ B \\ C \\ D \\ E \end{bmatrix} = \frac{1}{h^4} \begin{bmatrix} 1 & 1 & 1 & 1 & 1 \\ -2 & -1 & 0 & 1 & 2 \\ 4 & 1 & 0 & 1 & 4 \\ -8 & -1 & 0 & 1 & 8 \\ 16 & 1 & 0 & 1 & 16 \\ \end{bmatrix}^{-1} \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 24 \end{bmatrix} = \frac{1}{h^4} \begin{bmatrix} 1 \\ -4 \\ 6 \\ -4 \\ 1 \\ \end{bmatrix} $$ Substituting these values back into the original equation, we have the difference equation $$\frac{\partial^4f}{\partial x^4} \approx \frac{ f(x-2h) + -4f(x-h) + 6f(x) + -4f(x+h) + f(x+2h) } {h^4} $$ Carefully considering the general form above, it is noticed that this equation can be generalized to obtain the finite difference equation from any finite difference stencil given the desired derivative order (provided the desired finite difference equation exists for the given derivative order). Given a stencil \(s\) of length \(N\) and derivative order \(d< N\), the coefficients \(c\) are given by the finite difference coefficients equation $$s_1^nc_1 + \ldots + s_N^nc_N = \frac{d!}{h^d}\delta(n-d) \mbox{ for } 0 \leq n \leq N-1 $$ The solution to this equation can be written in matrix form as $$ \begin{bmatrix} c_1 \\ \vdots \\ c_N \end{bmatrix} = \frac{1}{h^d} \begin{bmatrix} s_1^0 & \ldots & s_N^0 \\ \vdots & \ddots & \vdots \\ s_1^{N-1} & \ldots & s_N^{N-1} \\ \end{bmatrix}^{-1} \begin{bmatrix} 0 \\ \vdots \\ d! \\ \vdots \\ 0 \end{bmatrix} $$ Each time \(s\) and \(d\) are altered in the calculator above, this matrix equation is solved and the resulting finite difference equation is displayed.


If you use the Finite Difference Coefficients Calculator for a publication, please cite it as

@misc{fdcc,
  title={Finite Difference Coefficients Calculator},
  author={Taylor, Cameron R.},
  year={2016},
  howpublished="\url{https://web.media.mit.edu/~crtaylor/calculator.html}"
}