Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

Finite Difference Approach by MATLAB for the First and Second Derivatives

The following MATLAB program determines the first and second derivatives of the data given in the problem applying the finite difference schemes and developing a custom user defined function firstsecondderivatives(x,y).

The finite difference schemes used are the following:

For first derivative:
  • At first point (three point forward)
  • At last point (three point backward)
  • At all other points (two point central)

For second derivative:
  • At first point (four point forward)
  • At last point (four point backward)
  • At all other points (three point central)

DATA SET
 x -1 -0.5 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5
 f(x) -3.632 -0.3935 1 0.6487 -1.282 -4.518 -8.611 -12.82 -15.91 -15.88 -9.402 9.017


function [dy, ddy] = firstsecondderivatives(x,y)

% The function calculates the first & second derivative of a function that is given by a set
% of points. The first derivatives at the first and last points are calculated by
% the 3 point forward and 3 point backward finite difference scheme respectively.
% The first derivatives at all the other points are calculated by the 2 point
% central approach.

% The second derivatives at the first and last points are calculated by
% the 4 point forward and 4 point backward finite difference scheme respectively.
% The second derivatives at all the other points are calculated by the 3 point
% central approach.

n = length (x);
dy = zeros;
ddy = zeros;

% Input variables:
% x: vector with the x the data points.
% y: vector with the f(x) data points.

% Output variable:
% dy: Vector with first derivative at each point.
% ddy: Vector with second derivative at each point.

dy(1) = (-3*y(1) + 4*y(2) - y(3)) / 2*(x(2) - x(1)); % First derivative
ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative

for i = 2:n-1
   
dy(i) = (y(i+1) - y(i-1)) / 2*(x(i+1) - x(i-1));
ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;

end

dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / 2*(x(n) - x(n-1));
ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;

figure(1)
subplot (3,1,1)
plot (x, y, 'linewidth', 2, 'color', 'k'), grid
title('x VS f(x)')
ylabel ('f(x)')

subplot (3,1,2)
plot (x, dy, 'linewidth', 2, 'color', 'b'), grid
title('x VS dy')
ylabel ('dy: First Derivative')

subplot (3,1,3)
plot (x, ddy, 'linewidth', 2, 'color', 'r'), grid
title('x VS dy')
ylabel ('ddy: Second Derivative'), xlabel('x')

figure(2)
plot (x, y, 'linewidth', 2, 'color', 'k');
hold on;
plot (x, dy, 'linewidth', 2, 'color', 'b');
hold on;
plot (x, ddy, 'linewidth', 2, 'color', 'r'), legend
ylabel ('y, dy, ddy'), xlabel('x')
end


The following operations are done in MATLAB command window to run the above function.

Program Outputs:

>> x = -1:0.5:4.5;
>> y=[-3.632 -0.3935 1 0.6487 -1.282 -4.518 -8.611 -12.82 -15.91 -15.88 -9.402 9.017];
>> firstsecondderivatives(x,y)

ans =

  Columns 1 through 11

    2.0805    2.3160    0.5211   -1.1410   -2.5833   -3.6645   -4.1510   -3.6495   -1.5300    3.2540   12.4485

  Column 12

   12.1947

We can also plot the derivatives, below is the figure for the function, first and second order derivatives respectively.

Plotting the first and second order numerical derivatives
Plotting the first and second order numerical derivatives

No comments:

Post a Comment