Numerical Methods/Numerical Differentiation

Often in Physics or Engineering it is necessary to use a calculus operation known as differentiation. Unlike textbook mathematics, the differentiated functions are data generated by an experiment or a computer code.

Begin with the Taylor series as seen in Equation 1.

${\displaystyle f(x+h)=f(x)+f^{'}(x)h+{\frac {f^{(2)}(x)}{2!}}h^{2}+{\frac {f^{(3)}(x)}{3!}}h^{3}+\cdots \quad (1)}$

Next by cutting off the Taylor series after the fourth term and evaluating it at h and -h yields Equations (2) and (3).

${\displaystyle f(x+h)=f(x)+f^{'}(x)h+{\frac {f^{(2)}(x)}{2!}}h^{2}+{\frac {f^{(3)}(c_{1})}{3!}}h^{3}\quad (2)}$

${\displaystyle f(x-h)=f(x)-f^{'}(x)h+{\frac {f^{(2)}(x)}{2!}}h^{2}-{\frac {f^{(3)}(c_{2})}{3!}}h^{3}\quad (3)}$

Then by subtracting Equation (2) by Equation (3) yields.

${\displaystyle f(x+h)-f(x-h)=2f^{'}(x)h+{\frac {f^{(3)}(c_{1})}{3!}}h^{3}+{\frac {f^{(3)}(c_{2})}{3!}}h^{3}}$

Central Difference

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

Forward Difference

${\displaystyle f^{'}(x)={\frac {f(x+h)-f(x)}{h}}+O(h)}$

Backward Difference

${\displaystyle f^{'}(x)={\frac {f(x)-f(x-h)}{h}}+O(h)}$

Second Derivative

The second order derivatives can be obtained by adding equations (2) and (3) (if properly expanded to include the fourth-derivative-term):

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