ChemEng stuff followers

Brief background for numerical derivatives

Deriving a function numerically is not usual even if it is a very large analytical expression. But if this is reasonably short you would prefer to derive it analytically and then evaluate the resulting expression for every point where you need the derivative.


There are other cases related to simulation of a phenomena through differential equations which are usually treated with specific packcages. For simple cases Google Sheets can do the job.


Here, simple cases shall be considered. For this a numerical derivative is to be estimated through a formula obtained by an approximation. This is good news because the work is reduced to evaluate a formula (every engieer's dream).

However, smart people (physicists, mathematicians) developed several formulas for the same thing!


For short, there is a number of formulas for the first, second, etc, derivatives of a function to choose from. The main discrimination rule one usually use to choose is error and availability of data. A good summary of these formulas can be found in the book of Numerical Methods for Engineers by SC Chapra and RP Canale.


For the purposes of this post we will use the forward finite difference formula for the first derivative with error of the size O(h). Do not be scared. All derivatives are usually appproximated by a finite difference and O(h) means that the numerical error to be expected would be as large of the step used h. This formula is as follows,

$f'(x_i)=\frac{f\left(x_{i+1}\right)-f(x_i)}{h}$          Eq. (1)

which can also be written only in terms of $x_i$'s as follows,

$f'(x_i)=\frac{f\left(x_{i+1}\right)-f(x_i)}{|x_i-x_{i+1}|}$          Eq. (2)


For the not initiated the $i$ is used to indicated the beginning and the end of a segment (over which you are estimating the first derivative) of the function $f(x)$. This can be schematically relpresented as follows:


Form the picture above it is clear that the step $h$ may take any value. But there are some restrictions.


Note: $h$ should be as small as possible since the formula presented here, and in the recommended book, was obtained under the assumption that $h$ was very small $h<<1$. In other words, if you take $h=0.1$ it could be already to large and consequently your derivatives will carry an error. 


Usaje of Eq. (1) or E. (2) is straightforward. All you need to do is to substitute into your function $f(x)$ the two values of $x$, which are $x_i$ and $x_{i+1}$, in order to find the derivative $f'(x_i)$ at $x_i$. If you need to find the value of the first drivative at several points $x_i$, $x_{i+1}$, $x_{i+2}$, etc. a software like Google Sheets or Excel is recommended.


You should notice that Eq. (1) or E. (2) can be used for analytical functions or discrete functions (usually given in the form of experimental data). One last restriction for these equations is that the function shoul be smooth as the one shown in the picture above. If your function $f(x)$ has sudden changes between a pair of $x_i$'s the best you can do is to reduce $h$.


Any questtion? Write in the comments and I shall try to help.

===========

Ildebrando.

No comments:

Post a Comment

Most popular posts