Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Stephen Roberts
Michaelmas Term
Estimating Derivatives
1
Derivatives- motivation
Engineers often need to calculate derivatives approximately, either from data or
from functions for which simple analytic forms of the derivatives don’t exist.
Examples:
• Motion simulation, such as in flight simulators solving &x& = Forces
equations..
• estimation of rates of change of measured signals.
• control systems, where e.g. velocity signals are wanted from position
encoder data.
• Medical signal & image processing – analysis and visualisation
Most methods derive from the basic derivation of differentiation of a function f(t):
df f (t + δt ) − f (t )
f′= = lim .
dt δt →0 δt
Forward difference
If a function (or data) is sampled at discrete points at intervals of length h,
so that
f n +1 − f n f (nh + h ) − f (nh )
=
h h
h2
f (nh ) + hf ′(nh ) + f ′′(nh ) + L − f (nh )
=
2
h
h
≈ f ′(nh ) + f ′′(nh ) = f ′(nh ) + O (h )
2
Engineering Computation ECL6-4
2
Clearly the order of approximation is O(h). Lets check this out with
the derivative of f (t ) = e t at the point t = 0.
Clearly, the exact answer is f ′(0) = 1.00000 . Using a spreadheet, we get:
Forward difference formula
h f' error
Note that the error does behave as O(h) until very small h, when rounding errors
cause problems.
Of course with data (say accurate to .± 1.0% ) this could cause problems much
earlier.
Much of the error comes from the asymmetry of the approximation, where the
points used are on one side of the point where the derivative is sought.
Engineering Computation ECL6-5
Backward difference
This follows a similar line of argument but we step backwards from f n = f (nh ) rather
than forward. Thus the backward difference formula is
f n − f n −1
f n′ ≈
h
The error behaves as before. Why is this more useful than the forward difference in
practice?
3
Central differences
4
N-point Formulae
f n+1 − f n−1
f ' ( nh) =
2h
5
Differentiation and Noise
The numerical differentiation process amplifies any noise in the data. Consider
using the central difference formula with h = 0.1 to find the derivative of sinx
with little added noise, using a MATLAB m-file:
% diff1.m
%plots the differential coefficient of noisy data.
h=0.1; %set h
x = [0:h:5]'; %data range.
n=length(x);
x2 = x(2:n-1); %trim x
for i=2:n-1
df1(i-1)=(fn(i+1)-fn(i-1))/(2*h); %non-noisy differential
df2(i-1)=(fn2(i+1)-fn2(i-1))/(2*h); %noisy differential
end
2 .5
1 .5
0 .5
-0 .5
-1
0 0.5 1 1.5 2 2 .5 3 3.5 4 4.5 5
Note that even though the noise on the functions is small, the noise on the
derivative is horrendous.
6
3 .5
2 .5
1 .5
0 .5
-0 . 5
0 0.5 1 1 .5 2 2 .5 3 3 .5 4 4.5 5
Solution: fit a polynomial to the to the function, and differentiate that. fitting a
4th order poly to the noisy data gets a smooth differential coefficient, even if it is
off at the ends.
Estimating Integrals
7
Numerical Integration
You’ve done this in Prelims, so sit back and revise (or look at
Kreyszig):
where the a’s are weights over n+1 intervals. This process is
sometimes called numerical quadrature.
h 3
2.5
f 2
Expanding the first term as a Taylor series, 1.5
and remembering that f ( x ) = y ′( x ) gives the 1
0.5
area between two adjacent points 0
0 1 2 3 4
( )
y ((n + 1)h ) − y (nh ) = hf (nh ) + O h 2
x
However, in a given length L, there are L/h elements, a number which increases
as h decreases.
Thus the local, one step error of O(h2) becomes a Global error O(h) , which is
not very good.
8
The Trapezium rule is better. The formula Trapesium rule
for one step is
4.5
h 4
y ((n + 1)h ) − y (nh ) =
( f (nh ) + f (n + 1)) + ε 3.5
2
3
where ε is the error at each step. 2.5
f
2
By expanding in Taylor series, 1.5
h2
y n +1 = y n + hy n′ +
2
( )
y n′′ + O h 3 and
1
0.5
0
h2
y ′n′′ + O (h 3 )
0 1 2 3 4
f n +1 = y ′n+1 = y n′ + hy n′′ +
2 x
( )
You can show that the local error ε = O h 3 , and hence the global error is
O(h2), which is much better.
Simpson’s rule
Fit quadratics (i.e. parabolas) to the middle and end points of an interval
nh ≤ x ≤ (n + 2)h .
The formula is derived in Kreyszig (p. 960, 7th Ed.) :
h
y (2nh ) − y (0) =
{ f (0) + 4 f (h ) + 2 f (2h ) + 4 f (3h ) + 2 f (4h ) + LL + f (2nh )}
3
Note the alternating 2f and 4f terms.