You are on page 1of 11

ECE 1010 ECE Problem Solving I

Numerical Differentiation
The derivative of a function f ( x ) is defined as f' ( x ) = df ( x ) = lim f ( x + x ) f ( x ) -----------------------------------------------dx x 0 ( x + x ) x if the limit exists Physical examples of the derivative in action are: Given x ( t ) is the position in meters of an object at time t, the first derivative with respect to t, x' ( t ) , is the velocity in meters/second (note: The integral of velocity is position to within a constant) Given x ( t ) is the velocity in meters/second of an object at time t, the first derivative with respect to t, x' ( t ) , is the acceleration in meters/second squared In an electrical circuit with time varying voltages and currents, the current flowing through a capacitor is given by dv c ( t ) i c ( t ) = C -------------dt In an electrical circuit with time varying voltages and currents, the voltage across an inductor is given by di l ( t ) v l ( t ) = L -----------dt

Chapter 7: Numerical Differentiation

716

ECE 1010 ECE Problem Solving I

di l ( t ) v l ( t ) = L -----------dt + il ( t ) L

vc ( t ) + C dv c ( t ) i c ( t ) = C -------------dt

The Derivative and the Slope The derivative of f ( x ) at a is the slope of the line tangent to f ( x ) at a Critical Points f(a) Slope of line = f ( a )

f( x)

x a Points where the derivative of f ( x ) is equal to zero are known as critical points The function may be horizontal in this region or may have reached a so-called extrema point, a point where f ( x ) is at a local (global) maximum or local (global) minimum In calculus you learn that the second derivative can be used to determine if an extrema is a maximum or minimum If the second derivative of f ( x ) is positive the extrema is a minimum

Chapter 7: Numerical Differentiation

717

ECE 1010 ECE Problem Solving I

If the second derivative of f ( x ) is negative the extrema is a maximum Global Maximum Local Maximum f( x) Local Minimum

Derivative Approximations using Differences Numerical algorithms for computing the derivative of a function require the estimate of the slope of the function for some particular range of x values Three common approaches are the backward difference, forward difference, and the central difference Backward Difference f ( xk ) f ( xk 1 ) f' ( x k ) ----------------------------------xk xk 1 f ( xk 1 ) f ( xk ) f ( xk + 1 )

xk 1 xk xk + 1

Chapter 7: Numerical Differentiation

718

ECE 1010 ECE Problem Solving I

Forward Difference f ( xk + 1 ) f ( xk ) f' ( x k ) -----------------------------------xk + 1 xk Central Difference f ( xk + 1 ) f ( xk 1 ) f' ( x k ) ------------------------------------------xk + 1 xk 1 f ( xk 1 ) f ( xk ) f ( xk 1 ) f ( xk )

f ( xk + 1 )

xk 1 xk xk + 1 f ( xk + 1 )

xk 1 xk xk + 1

To estimate the second derivative we simple apply one of the above algorithms a second time, that is using the backward difference f' ( x k ) f' ( x k 1 ) f'' ( x ) -------------------------------------xk xk 1 The MATLAB diff Function To make computing the numerical derivative a bit easier, MATLAB has the function diff(x) which computes the differences between adjacent values of the vector x The vector (matrix) that diff(x) returns contains one less element (row) than x

Chapter 7: Numerical Differentiation

719

ECE 1010 ECE Problem Solving I

If samples of f ( x ) are contained in vector y and the corresponding x values in vector x, the derivative can be estimated using
deriv_y = diff(y)./diff(x);

The corresponding x values are obtained from the original x vector by trimming either the first or last value Trimming the last value results in a forward difference estimate Trimming the first value results in a backward difference estimate Example: Find the derivative of a function containing polynomial terms and a trig function f ( x ) = 5 cos ( 10x ) + x 2x 6x + 10
x = 0:.01:4; y = 5*cos(10*x) + x.^3 - 2*x.^2 - 6*x +10; plot(x,y) deriv_y = diff(y)./diff(x); xd = x(2:length(x)-1); figure xd = x(2:length(x)); % Backward difference x values plot(xd,deriv_y)

Direct analysis also yields f' ( x ) = 50 sin ( 10x ) + 3x 4x 6


2

Chapter 7: Numerical Differentiation

720

ECE 1010 ECE Problem Solving I

Polynomial-Trig Function
20

15

10

f(x)

-5

-10

0.5

1.5

2.5

3.5

Polynomial-Trig Function Derivative


80

60

40

20

f2 (x)
0 -20 -40 -60 0

0.5

1.5

2.5

3.5

Chapter 7: Numerical Differentiation

721

ECE 1010 ECE Problem Solving I

Note: Here we have used a backward difference calculation To locate the critical points we can find the zero crossings of the derivative function
delay_mult = deriv_y(1:length(deriv_y)-1) .*deriv_y(2:length(deriv_y)); critical = xd(find(delay_mult < 0)); critical = Columns 1 through 5 0.3300 0.6100 0.9600 Columns 6 through 10 1.8800 2.2000 2.5200 Columns 11 through 12 3.4200 3.8200

1.2400 2.8100

1.5800 3.1600

We can plot these points over the original function to see if they are maxima or minima
plot(x,y) hold Current plot held plot(xd(find(delay_mult < 0)),y(find(delay_mult < 0)),'s'); title('Polynomial-Trig Function','fontsize',18) ylabel('f\''(x)','fontsize',14); xlabel('x','fontsize',14) grid

Chapter 7: Numerical Differentiation

722

ECE 1010 ECE Problem Solving I

Polynomial-Trig Function
20

15

10

-5

-10

0.5

1.5

2.5

3.5

To classify the extrema as local maxima or minima we can compute the second derivative and classify the samples of the second derivative at the extrema points
dderiv_y = diff(deriv_y)./diff(xd); xdd = xd(2:length(xd)); subplot(311) plot(x,y) grid ylabel('f(x)','fontsize',14) title('Poly-Trig Function and Two Derivatives',... 'fontsize',18) subplot(312) plot(xd,deriv_y)
Chapter 7: Numerical Differentiation 723

ECE 1010 ECE Problem Solving I

grid ylabel('f\prime(x)','fontsize',14) subplot(313) plot(xdd,dderiv_y) grid xlabel('x','fontsize',14) ylabel('f\prime\prime(x)','fontsize',14)

Poly-Trig Function and Two Derivatives


20 10

f(x)
0 -10 100 0

0.5

1.5

2.5

3.5

f(x)

50 0 -50 -100 0 0.5 1 1.5 2 2.5 3 3.5 4

1000

f(x)

500 0 -500 -1000 0 0.5 1 1.5 2 2.5 3 3.5 4

The extrema stored in critical are those indices of x where f' ( x ) has zero crossings

Chapter 7: Numerical Differentiation

724

ECE 1010 ECE Problem Solving I

We can apply the same criteria, i.e., find(delay_mult < 0), to determine the sign of the corresponding values critical values of the second derivative
% Since the x values of dderiv_y are shifted to the % left by one we use the critical values indices % reduced by one. % Place the critical values of dderiv_y and xdd in % special arrays ddy_crit = dderiv_y(find(delay_mult < 0)-1)

ddy_crit = Columns 1 through 5 496.6516 -480.0852 499.8707 -478.5845 504.9876 Columns 6 through 10 -486.7868 506.6496 -488.2557 493.7020 -482.9189 Columns 11 through 12 464.6619 -441.1139 ddx_crit = xdd(find(delay_mult < 0)-1);

We see that the second derivative at the critical values is either large negative or large positive, hence in this case we can easily decide if the extrema are maxima or minima
% Now find the x values that correspond to maxima max_xval = ddx_crit(find(ddy_crit < 0)) max_xval = 0.6100 3.8200

1.2400

1.8800

2.5200

3.1600

Chapter 7: Numerical Differentiation

725

ECE 1010 ECE Problem Solving I

% Now find the x values that correspond to minima min_xval = ddx_crit(find(ddy_crit > 0)) min_xval = 0.3300 3.4200

0.9600

1.5800

2.2000

2.8100

Chapter 7: Numerical Differentiation

726

You might also like