Lec 23 Derivatives(3)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

Lecture Slides

on
Modeling and Simulation

Lecture: Numerical Derivatives

Dr. Rubina Nasir


Email: rubinanm@yahoo.com
Numerical Differentiation and Integration

• We known from Calculus the mathematical concepts of


differentiation and integration:

∆y f ( xi + ∆x) − f ( xi )
=
∆x ∆x
dy f ( xi + ∆x) − f ( xi )
= ∆x lim 0
dx ∆x
b
I = ∫ f ( x)dx
a

2
Numerical Differentiation
We known from Calculus the derivative forms a slope at a point.
Let us see that:

f(xi+∆x)

∆y

f(xi)

x
∆x
Numerical Differentiation

f(xi+∆x)

∆y

f(xi)

x
∆x
Numerical Differentiation

f(xi+∆x)
∆y

f(xi)

x
∆x
Numerical Differentiation

f(xi+∆x)
f(xi) ∆y

x
∆x
Numerical Differentiation

f(xi+∆x)
∆y
f(xi)

x
∆x
Numerical Differentiation
∆y f ( xi + ∆x) − f ( xi )
Finally, we can =
∆x ∆x
see that the
dy f ( xi + ∆x) − f ( xi )
derivative forms = ∆x lim 0
a slope at a point. dx ∆x

f(xi)

xi x
Methods for Differentiation and Integration

• The function to be differentiated or integrated will typically be


in one of the following three forms:
– A simple continuous function such as polynomial, an
exponential, or a trigonometric function.
– A complicated continuous function that is difficult or
impossible to differentiate or integrate directly.
– A tabulated function where values of x and f(x) are given at
a number of discrete points, as is often the case with
experimental or field data.
Derivatives

• We generally use Taylor series to evaluate a function in the


neighborhood of xo when the derivatives of all order are known.
• It can also be used to evaluate approximately the derivatives of f(x)
if the values of function are known at a number of discrete points, {
xi } where, i = 1, 2, . . . N.
• Let us assume that we know the values of f(xo) and f(xo + h), where
h is a small value.
• Then the Taylor series expansion of f(xo + h) can be written as
following:
h2
f ( x o + h ) = f ( x o ) + hf ′( x o ) + f ′′( x o ) + 
2
Forward Difference as First Derivative

• When we truncate Taylor’s expansion after the first derivative and


solve for first derivative we obtain an approximation:
1
f ′( x o ) = [ f (x o + h ) − f (x o )]
h
• Thus the first derivative at x = xo is approximately estimated by f(xo+
h) and f(xo). By defining the forward differences on the right side is
expressed by
∆f ( x o ) = f ( x o + h ) − f ( x o )

• It is termed as forward difference formula for the


derivative.
1
[ f ( x + h ) − f ( x )] = ∆f ( x )
o o
o

h ∆x o
Backward Difference Formula
• Similarly, the backward difference formula is obtained by
starting with function :
1
[ f ( x o ) − f ( x o + h )] = ∇f ( x o )
h ∇x o

• where, ∇ is used to denote the backward difference.


• The central difference may be defined by :
δf ( x o ) = f ( x o + h / 2 ) − f ( x o − h / 2 )
• By applying the Taylor series expression to both terms on the
right hand side, we obtain the following finite difference:
3
h 1 h 
δf ( x o ) = 2  f ′( x o ) +   f ′′′( x o ) + 
2 3!  2  

• Truncating after the second order term, the following central


difference formula is obtained: f ( x + h / 2 ) − f ( x − h / 2 ) δf ( x )
o o o
=
h δx o
Errors

• We see that approximating the first derivatives by


forward or backward difference processes error and the
error is approximately equal to the (h/2)f’’ .
• The error is known as truncation error.
• Another type of error that can be introduced in numerical
calculations is round-off error.
• It is error appearing in computing the difference terms
and may be denoted by ε.
Errors

• The truncation error decreases


with h, however the round-off
error increases.
• It has been seen that truncation
error is dominant when h is
large and absolute error has
dominating role of round-off
error when h is very small.
• Both errors have been
compared in Figure.
• For large step size, h, the error
in the first derivative is
dominated by the truncation
error and as h is allowed to
reduce, major contribution to
the total error becomes due to
round-off error.
Example 1: derivative
Given the following data from an experiment:

x (cm) 0.1 0.2 0.3 0.4

f(x) (gm) 2.1 4.3 5.8 7.2

Estimate first and second derivatives at x = 0.2 and 0.3 using central
difference form.

Solution: The interval between two x values is h = 0.1. Using the


central difference form, let us first calculate the derivative at x = 0.2:
Example 1: derivative
Let us first calculate the derivative at x = 0.2,

df
=
1
[ f ( x = 0.3) − f ( x = 0.1)]
dx x = 0.2 2h

df (5.8 − 2.1)
= = 18.5 gm/cm
dx x = 0.2 2(0.1)

•Similarly at x = 0.3,

df
=
1
[ f ( x = 0.4) − f ( x = 0.2)]
dx x = 0.3 2h

df (7.2 − 4.3)
= = 14.5gm/cm
dx x = 0.3 2(0.1)
Example 1: Derivative
and the second derivative at x = 0.2 is given as:

d2 f 1
= [ f ( x = 0.1) − 2 f ( x = 0.2 ) + f ( x = 0.3)]
dx 2 x = 0.2
h 2

d2 f
=
1
[2.1 − 2.0 × 4.3 + 5.8] = −70.
dx 2 x = 0.2
(0.1) 2

Then the second derivative at x= 0.3 is approximately equal to:

d2 f
=
1
[ f ( x = 0.2) − 2 f ( x = 0.3) + f ( x = 0.4)] = −10.
dx 2 x = 0.3
h 2

These estimates can also be found using forward and


backward difference forms.
Example 2: Derivative
Another Method to compute first derivative is following:
Example 3: derivative Using MATLAB

Find derivative of polynomial using MATLAB for the


function:
f ( x ) = 2 x 5 − 2 x 4 − 5x 3 + 10 x + 1

The derivative function is given as following:

f ′( x) = 10 x 4 − 8 x 3 − 15 x 2 + 10

Use MATLAB to find the derivative function.


Example 3: derivative Using MATLAB
• Solution:
• The script file for the MATLAB code is give below where
we first find the solution by polyder function and the
employ diff to find the derivative.
• The coefficients of polynomial are identified and then
these are used to find the coefficients of derivative
function..
• Then using polyval command this polynomial is
generated.
• The diff command approximates the derivative as a ratio
of two differences as
( y i +1 − y i ) /( x i +1 − x i ) ∀i = 1 ,2 ,
Example 3: derivative Using MATLAB
• The accuracy can be improved when the spacing of xi is reduced.
Here we have computed for 200 points in the range [0, 2] and Fig.
compares the results from both techniques.
Example 3: derivative Using MATLAB
MATLAB Program
% Program Name: derivative_1.m
% A MATLAB script file to compute derivatives
% f(x) = 2*x^5 -2*x^4 -5*x^3 + 10*x + 1
power_f = [2 -2 -5 0 10 1] % coefficients
pq = polyder (power_f); % polynomial derivative
xk = linspace(0, 2, 200); % 0-2 is divided into 200 points
f_dash = polyval (pq, xk) % evaluation for each xk

yk = 2*xk.^5 - 2*xk.^4 - 5*xk.^3 + 10*xk + 1.;


yk_dash = diff(yk)./diff(xk); % using diff
clf % clear any previous figure

subplot (1, 2, 1), plot(xk(1:200), f_dash,'-') % from polyder


xlabel('x'), ylabel('derivative of f(x) using polyder')
hold on
subplot (1, 2, 2), plot(xk(1:199), yk_dash,'+') % from diff
xlabel('x'), ylabel('derivative of f(x) using diff')
hold off
% ------------------------------------------------
Physical Concept of an Integral

b
I = ∫ f ( x)dx
a

Similarly, the above


integral of a function f(x) is
the Area Under the curve
for range of x = a to x = b
as shown in Figure.
Numerical Integration

• Numerical integration methods become necessary


when the values of f(x) are known on the discrete
points {xi} and the exact form of f(x) is not known;
• or when the formula of f(x) is known but the analytical
integration is difficult.
• Numerical integration formulas are obtained by
integrating the interpolating polynomials described
earlier.
Numerical Integration
• Integration is a summing process as opposed to find
rates of changes in differentiation.
• It is generally area under the curve within the given
range.
• When we want to find the change in the quantity g(x)
whose rate of change with respect to x is known function
f(x), then integral is the net change in the quantity g from
a point a to b
• i.e., for a known f(x) = dg(x)/dx, the integral is

b
g b − g a = ∫ f ( x )dx
a
Numerical Integration
Area under the curve

f(x)
• When many
sufficiently small
strips are added
we can generate
the area under the
curve from x = a
a b x
to x = b.

b
Strips = 1
g b − g a = ∫ f ( x )dx
a
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 2
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 3
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 4
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 5
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 7
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 9
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 10
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 11
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 12
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 13
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 15
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 16
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

Strips = 17
Numerical Integration
• When many
sufficiently small
strips are added
f(x)
we can generate
the area under the
curve from x = a
to x = b.

b
g b − g a = ∫ f ( x )dx a b x
a

With 18 Strips of different heights and same Strips = 18


Width we get the area under the curve.

You might also like