Forward, Backward and Central scheme for numerical differentiation
In previous article I’ve describe, how to numerically calculate simplest derivative. But now we can extend this techniques and try to use left and right nearby points.
Forward, Backward and Central scheme
Forward
So, when, for calculation of the derivative, we use information about out point xi and information about next point xi+1 with final formula f’(xi) = (f(xi+1)-f(xi))/dx we call this scheme of numerical calculation – forward scheme.
Backward
When we use information about function value in the point of interest and in the previous point – this is backward derivative. To calculate it we use following numerical equation: f’(xi) = (f(xi)-f(xi-1))/dx
Central
If we use information about function in previous and next points, then we are using central scheme for numerical differentiation. The equation for central scheme will be f’(xi) = (f(xi+1)-f(xi-1))/2dx
Python code
All these schemes can be easily coded with python.
def derivative_1(res, f, x):
'''
Forward numerical scheme
'''
res[x['l']:x['r']] = (f[x['l']+1:x['r']+1] - f[x['l']:x['r']])/x['d']
return res
def derivative_2(res, f, x):
'''
Backward numerical scheme
'''
res[x['l']:x['r']] = (f[x['l']:x['r']] - f[x['l']-1:x['r']-1])/x['d']
return res
def derivative_3(res, f, x):
'''
Central numerical scheme
'''
res[x['l']:x['r']] = (f[x['l']+1:x['r']+1] - f[x['l']-1:x['r']-1])/(2*x['d'])
return res
Also it is necessary to have some king of function to select between these functions for calculations. For the time of development, we can use something very simple.
def derivative(res, f, x, type):
if type == 1:
return derivative_1(res, f, x)
if type == 2:
return derivative_2(res, f, x)
if type == 3:
return derivative_3(res, f, x)
To see short explanation about these schemes of numerica differentiation go to check video: Forward, Backward and Central scheme for numerical differentiation
Published: 2022-07-26 22:56:57
Updated: 2022-07-26 23:30:52