Numerical Analysis: Part Five
Chapter 21: Numerical Differentiation
Prof. Sung Jin Yoo
School of Electrical and Electronics Engineering
Chung-Ang University
Numerical analysis Chapter 21
21. Numerical Differentiation – Differentiation
Differentiation
• The mathematical definition of a derivative begins with a difference
y f xi x f xi
approximation:
x x
and as x is allowed to approach zero, the difference becomes a
derivative:
dy f xi x f xi
lim
dx x0 x
Numerical analysis Chapter 21
21. Numerical Differentiation – Differentiation
High-Accuracy Differentiation Formulas
• Taylor series expansion can be used to generate high-accuracy
formulas for derivatives by using linear algebra to combine the
expansion around several points.
• To reduce the errors, we can include additional terms from Taylor series
expansion.
• For example,
f '' ( xi ) 2 f ( xi 1 ) f ( xi ) f '' ( xi )
f ( xi 1 ) f ( xi ) f ( xi )h
'
h f ( xi )
'
h O(h 2 )
2! h 2!
Forward-difference appro. of the second derivative
f ( xi 2 ) 2 f ( xi 1 ) f ( xi )
f '' ( xi ) 2
O ( h)
h
f ( xi 1 ) f ( xi ) f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
f ' ( xi ) O(h) f ' ( xi ) O(h 2 )
h 2h
• Three categories for the formula include forward finite-difference,
backward finite-difference, and centered finite-difference.
Numerical analysis Chapter 21
21. Numerical Differentiation – Forward Finite-Difference
Numerical analysis Chapter 21
21. Numerical Differentiation – Backward Finite-Difference
Numerical analysis Chapter 21
21. Numerical Differentiation – Centered Finite-Difference
Numerical analysis Chapter 21
21. Numerical Differentiation – Centered Finite-Difference
High-Accuracy Differentiation Formulas (cont.)
ex) This is the results to estimate the derivative of this function
f (x ) = -0.1x 4 - 0.15x 3 - 0.5x 2 - 0.25x + 1.2
at x=0.5 using finite–differences and a step size of h=0.25. True value:
Repeat this computation, but employ the high-accuracy formulas.
Sol.) The data needed for this example
a) Forward difference of accuracy O(h2)
b) backward difference of accuracy O(h2)
More accurate !! c) Centered difference of accuracy O(h2)
Centered diff. : four points are used
So, the error is zero !!
Numerical analysis Chapter 21
21. Numerical Differentiation – Unequally Spaced Data
Unequally Spaced Data
• One way to calculate derivatives of unequally spaced data is to determine
a polynomial fit and take its derivative at a point.
• As an example, using a second-order Lagrange polynomial to fit three
points and taking its derivative yields:
2x x1 x2 2x x0 x2 2x x0 x1
f x f x0 f x1 f x2
x0 x1 x0 x2 x1 x0 x1 x2 x2 x0 x2 x1
Ex 21.3) Use numerical differentiation to evaluate the gradient at z=0.
That is, compute (k=0.5)
Sol.) Given three data in the figure,
Numerical analysis Chapter 21
21. Numerical Differentiation
– Derivatives and Integrals for Data with Errors
Derivatives and Integrals for Data with Errors
• A shortcoming of numerical differentiation is that it tends to amplify
errors in data, but integration tends to smooth data errors.
• One approach for taking derivatives of data with errors is to fit a smooth,
differentiable function to the data and take the derivative of the function.
Numerical analysis Chapter 21
21. Numerical Differentiation
– Numerical Differentiation with MATLAB
Numerical Differentiation with MATLAB
• MATLAB has two built-in functions to help take derivatives, diff and
gradient:
• diff(x)
– Returns the difference between adjacent elements in x
• diff(y)./diff(x)
– Returns the difference between adjacent values in y divided by the
corresponding difference in adjacent values of x
Ex) Use diff to differentiate the func. f (x ) = 0.2 + 25x - 200x 2 + 675x 3 - 900x 4 + 400x 5
from x=0 to 0.8. Compare the exact derivative
f ' (x ) = 25 - 400x + 2025x 2 - 3600x 3 + 2000x 4
>> f=@(x) 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
>> x=0:0.1:0.8;
>> y=f(x);
>> diff(x)
>> d = diff(y)./diff(x)
Numerical analysis Chapter 21
21. Numerical Differentiation
– Numerical Differentiation with MATLAB
Numerical Differentiation with MATLAB
• fx = gradient(f, h) ~ returns the differences
where f is a one-dimensional function value vector of length n, and fx is a
vector of length n containing differences based on f. h is the spacing
between points ; if omitted, h=1.
Determines the derivative of the data in f at each of the points. The
program uses forward difference for the first point, backward difference for
the last point, and centered difference for the interior points.
Ex) Consider the same function as the previous example.
f (x ) = 0.2 + 25x - 200x 2 + 675x 3 - 900x 4 + 400x 5
>> f=@(x) 0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
>> x=0:0.1:0.8;
>> y=f(x);
>> dy=gradient(y,0.1)
Numerical analysis Chapter 21