\documentstyle[12pt]<<313>>article<<313>>
\begin<<314>>document<<314>>
\section<<315>>Numerical Differentiation<<315>>
In this section we will discuss a few basic ideas in the construction of numerical
approximations for derivatives. We focus on using the Taylor series method, since
with it one can reduce the problem of generating approximations to derivatives to
algebraic manipulations. Further, with the Taylor series method one automatically gets
an indication of the order of accuracy of the particular approximation.
In no sense do we intend on presenting a complete treatment. What we hope the reader
comes away with is an appreciation of the origin of some commmon approximations and an
ability to generate such approximations for him/herself in the future, as well as being
able to invent other approximations which are needed by some future particular context.
\subsection<<316>>Forward and Backward First Derivatives<<316>>
Suppose that we are given an array of values $f_i$, where $i=0,1,2,...,N$. We assume that
there exists a continuous function underlying the $N+1$ values. Further, we assume that
the underlying function has as many derivatives defined as is necessary for our purposes
below. We also assume that there are $N+1$ values of a variable x, $x_i$. For simplicity
we will take the $x_i$ to be uniformly distributed in values with $x_0 ;SPMlt; x_1 ;SPMlt; ... ;SPMlt; x_N$
and the separation of $x_i$ and $x_<<317>>i+1<<317>>$ independent of $i$, so $dx = x_<<318>>i+1<<318>> - x_i$ for all
$i$. Having the $f_i$ non-uniformly distributed would complicate the considerations which
follow.
The problem is then to find approximate expressions for the first derivative using only
the provided data $(x_i,f_i)$.
Given our assumption that there is an underlying continuous function with as many derivatives
as needed, we make use of the familiar Taylor series expansion formula, which expresses the
function $f$ as an infinite series expansion about a given point of interest:
\begin<<319>>equation<<319>>
f\left(x\right) = f\left(x_0\right) + \left(x - x_0\right) f^<<320>>'<<320>><<321>>\mid<<321>>_<<322>>x_0<<322>> + <<323>>1 \over 2<<323>> \left(x - x_0\right)^2 f^<<324>>''<<324>><<325>>\mid<<325>>_<<326>>x_0<<326>>
+ <<491>>1 \over <<327>>3 !<<327>><<491>>\left(x - x_0\right)^3 f^<<328>>'''<<328>><<329>>\mid<<329>>_<<330>>x_0<<330>> + \ \ldots
\end<<331>>equation<<331>>
Let's evaluate $f$ at $x = x_0 \pm dx$ using the Taylor series. Simplifying slightly we find:
\begin<<332>>equation<<332>>
f\left(x_0 \pm dx \right) = f\left(x_0\right) \pm dx f^<<333>>'<<333>><<334>>\mid<<334>>_<<335>>x_0<<335>> + <<336>>1 \over 2<<336>> dx^2 f^<<337>>''<<337>><<338>>\mid<<338>>_<<339>>x_0<<339>>
\pm <<492>>1 \over <<340>>3 !<<340>><<492>> dx^3 f^<<341>>'''<<341>><<342>>\mid<<342>>_<<343>>x_0<<343>> + \ \ldots
\end<<344>>equation<<344>>
Now if we let $x_0$ be $x_i$, then $x_0 \pm dx$ corresponds to $x_<<345>>i \pm 1<<345>>$ and the Taylor series
expansion becomes that for $f_<<346>>i \pm 1<<346>>$. Note that if we subtract $f_i$ from Eq.(2) for the $i+1$ case, we have
\begin<<347>>equation<<347>>
f_<<348>>i+1<<348>> - f_<<349>>i<<349>> = dx f^<<350>>'<<350>><<351>>\mid<<351>>_<<352>>x_i<<352>> + <<353>>1 \over 2<<353>> dx^2 f^<<354>>''<<354>><<355>>\mid<<355>>_<<356>>x_i<<356>> + <<493>>1 \over <<357>>3 !<<357>><<493>>dx^3 f^<<358>>'''<<358>><<359>>\mid<<359>>_<<360>>x_i<<360>>
+ \ldots
\end<<361>>equation<<361>>
Dividing by $dx$ and solving for the first derivative at $x_i$ we obtain the
<<494>>\bf<<362>>forward difference approximation<<362>><<494>> for the first derivative:
\begin<<363>>equation<<363>>
f^<<364>>'<<364>><<365>>\mid<<365>>_<<366>>x_i<<366>> = <<503>><<495>>f_<<367>>i+1<<367>> - f_<<368>>i<<368>><<495>> \over <<369>>dx<<369>><<503>> - <<370>>1 \over 2<<370>> dx f^<<371>>''<<371>><<372>>\mid<<372>>_<<373>>x_i<<373>> + \ldots
\end<<374>>equation<<374>>
Note that the error in this approximation is of order $(dx)^1$ in the separation between points. Such an dependence
is said to indicate that the forward difference approximation is only ;SPMquot;first order accurate;SPMquot;. As we will see below,
one can do better than this.
Next, we give the result of performing the calculation as above except using $f_<<375>>i-1<<375>>$ and $f_i$. Then, taking
the difference between $f_<<376>>i-1<<376>>$ and $f<<377>>i<<377>>$ from the Taylor series expression and simplifying a bit, we find
the <<496>>\bf<<378>>backward difference approximation<<378>><<496>> for the first derivative:
\begin<<379>>equation<<379>>
f^<<380>>'<<380>><<381>>\mid<<381>>_<<382>>x_i<<382>> = <<504>><<497>>f_<<383>>i<<383>> - f_<<384>>i-1<<384>><<497>> \over <<385>>dx<<385>><<504>> + <<386>>1 \over 2<<386>> dx f^<<387>>''<<387>><<388>>\mid<<388>>_<<389>>x_i<<389>> + \ldots
\end<<390>>equation<<390>>
As is clear from inspecting this result, the backward difference approximation is also only first order
accurate in $dx$.
Before we proceed to find more accurate approximations, let's pause to make a comment on these two formulae.
From the perspective of order of accuracy, the forward and backward difference formulae represent the simplest
and least accurate approximations that one can imagine. In practice these formulae are not used except in situations
where a quick estimate of the derivative is needed. In most applications in computational physics, the resulting
low accuracy that these formulae present make them just not good enough for serious simulation. While the two
formulae are not widely used and thus will not get much attention from us in future discussions, here is a
good place to point out another aspect of them which may need attention when we get some higher order accurate
approximations: their one-sidedness. To illustrate the point, we can simply note that if we used the forward
difference formula we would run into trouble when we try to use it on the last point $i=N$. There is no point
and function value at $i=N+1$. So we can not compute the derivative at the rightmost point. However, we could
get a first order accurate estimate there if we used the backward difference approximation just for that last point,
since it uses values of the function at $i$ and $i-1$. A similar remark can be made about the leftmost point $x_0$.
At $i=0$ we can not use the backward formula but could use the forward formula. Hence, to the same order of
accuracy in $dx$ if we utilize both formulae we can find the derivative at all points $i=0, \ldots ,N$.
In addition, let's note that the forward and backward difference approximations would be exact if the underlying
function $f(x)$ happened to be a linear function $f(x) = a + b x$. However, in applications one does not know
the underlying function and thus can not determine, in advance, whether these forward and backward formulae
are the only ones needed. In most situation, they certainly do not suffice to capture enough about the rate
of change of the data to be very useful.
\subsection<<391>>Centered First Derivatives<<391>>
If we take the difference between $f_<<392>>i+1<<392>>$ and $f_<<393>>i-1<<393>>$, we find that all of the even order derivative
terms in the Taylor expansion cancel out, leaving the following result:
\begin<<394>>equation<<394>>
f_<<395>>i+1<<395>> - f_<<396>>i-1<<396>> = 2 dx f^<<397>>'<<397>><<398>>\mid<<398>>_<<399>>x_i<<399>> + <<498>> 2 \over <<400>>3!<<400>> <<498>> dx^3 f^<<401>>'''<<401>><<402>>\mid<<402>>_<<403>>x_i<<403>> + \ldots
\end<<404>>equation<<404>>
Solving for the first derivative then yields the result:
\begin<<405>>equation<<405>>
f^<<406>>'<<406>><<407>>\mid<<407>>_<<408>>x_i<<408>> = <<505>><<499>>f_<<409>>i+1<<409>> - f_<<410>>i-1<<410>><<499>> \over <<411>>2 dx<<411>><<505>> - <<500>> 2 \over <<412>>3!<<412>> <<500>> dx^2 f^<<413>>'''<<413>><<414>>\mid<<414>>_<<415>>x_i<<415>> + \ldots
\end<<416>>equation<<416>>
Note that this approximation is second order accurate in $dx$. It uses one point to the right and one point
to the left of the point where the derivative is evaluated. Only two values of $f$ are needed, as with the
forward and backward formulae. So, we can get better accuracy without using more data. Since the data used
are symmetrically placed relative to where the derivative is computed, this formula is called a centered
difference approximation. In practice, this formula is quite widely used. It is a workhorse of computational
physics.
As with the forward and backward formulae, we note that the centered difference formula would be exact if
the underlying function had a vanishing third derivative. This implies that the function would necessarily
have to be a quadratic function $F = a + b x + c x^2$. While we do not know that the function has this
property, our result above for this centered difference approximation indicates that our approximating an unknown
function with a quadratic is a better approximation by a whole order of accuracy. This in not very surprising.
Surely, the more terms in the Taylor series we include the better we should be able to capture the behavior of
the function.
Our remarks above about the problem with the forward and backward formulae at the points $i=0$ and $i=N$
also apply to this centered difference approximation: At $i=0$ one needs $f_<<417>>-1<<417>>$ while at $i=N$ one needs
$f_<<418>>N+1<<418>>$, both of which do not exist as part of the original data. What to do? Well, we can develop one-sided
second order accurate formulae without too much trouble.
For example, let's construct a second order expression for the first derivative at $x_0$ using only data
with $i ;SPMgt; 0$. For this purpose we record the Taylor expansion at $i=1,2$ with base point at $i=0$. We
have
\begin<<419>>equation<<419>>
f_1 = f_0 + dx f^<<420>>'<<420>><<421>>\mid<<421>>_<<422>>x_0<<422>> + <<423>>1 \over 2<<423>> dx^2 f^<<424>>''<<424>><<425>>\mid<<425>>_<<426>>x_0<<426>>
+ <<427>>1 \over 6<<427>> dx^3 f^<<428>>'''<<428>><<429>>\mid<<429>>_<<430>>x_0<<430>> + \ \ldots
\end<<431>>equation<<431>>
\begin<<432>>equation<<432>>
f_2 = f_0 + 2 dx f^<<433>>'<<433>><<434>>\mid<<434>>_<<435>>x_0<<435>> + 2 dx^2 f^<<436>>''<<436>><<437>>\mid<<437>>_<<438>>x_0<<438>>
+ <<439>>8 \over 6<<439>> dx^3 f^<<440>>'''<<440>><<441>>\mid<<441>>_<<442>>x_0<<442>> + \ \ldots
\end<<443>>equation<<443>>
Then, by inspecting these two expansions we easily see that the following combination will
result in the terms which have second derivatives cancelling out.
\begin<<444>>equation<<444>>
4 f_1 - f_2 = 3 f_0 + 2 dx f^<<445>>'<<445>><<446>>\mid<<446>>_<<447>>x_0<<447>> - <<448>>2 \over 3<<448>> dx^3 f^<<449>>'''<<449>><<450>>\mid<<450>>_<<451>>x_0<<451>> + \ldots
\end<<452>>equation<<452>>
Solving for the first derivative we find
\begin<<453>>equation<<453>>
f^<<454>>'<<454>><<455>>\mid<<455>>_<<456>>x_0<<456>> = <<501>><<457>>4 f_1 -3 f_0 - f_2<<457>> \over <<458>> 2 dx <<458>><<501>> - <<459>>1 \over 3<<459>> dx^2 f^<<460>>'''<<460>><<461>>\mid<<461>>_<<462>>x_0<<462>> + \ldots
\end<<463>>equation<<463>>
Thus we have constructed a second order accurate derivative approximation which is defined at the leftmost
end point of the defined data. The same method could be used to generate a second order derivative defined
at the rightmost end of the given data.
\subsection<<464>>A Centered Second Derivative<<464>>
In our algebraic manipulations thus far of the Taylor series we have endeavored to obtain the first
derivative and did what we had to do to eliminate the second derivative terms when we wanted a final
result of second order. Here we simply notice that if we take the sum rather than the difference between
$f_<<465>>i+1<<465>>$ and $f_<<466>>i-1<<466>>$ we can solve for the second derivative. We have
\begin<<467>>equation<<467>>
f_<<468>>i+1<<468>> + f_<<469>>i-1<<469>> = 2 f_i + dx^2 f^<<470>>''<<470>><<471>>\mid<<471>>_<<472>>x_i<<472>> + <<473>>1 \over 12<<473>> dx^4 f^<<474>>''''<<474>><<475>>\mid<<475>>_<<476>>x_i<<476>>
\end<<477>>equation<<477>>
Finally, solving for the second derivative we find
\begin<<478>>equation<<478>>
f^<<479>>''<<479>><<480>>\mid<<480>>_<<481>>x_i<<481>> = <<506>><<502>>f_<<482>>i+1<<482>> -2 f_i + f_<<483>>i-1<<483>> <<502>> \over <<484>> dx^2 <<484>><<506>> - <<485>>1 \over 12<<485>> dx^2 f^<<486>>''''<<486>><<487>>\mid<<487>>_<<488>>x_i<<488>> + \ldots
\end<<489>>equation<<489>>
This result is another of those which are widely used in computational physics simulations. Note that one needs
three data values to obtain this second derivative. The centered character of the approximation has the
benefit that it does not build in any directional bias. Of course, should one need a second derivative at one
of the end points one would have to find a set of interior values which would also have the third derivative
terms cancelling. We leave such a construction as an exercise for the reader.
\end<<490>>document<<490>>