Differentiating a Discrete Function with Equidistant Points

Many students ask me how do I do this or that in MATLAB.  This is a new addition to the “How do I do that in MATLAB”  series.
 
In this blog, I show you how to find the first derivative of a discrete function y(x).  We are assuming that the x values are eqidistant and the data is sorted in ascending or descending order by the x values.  The latter requirement can be relaxed easily in programs such as MATLAB where one can use the sortrows command to put the data in the required order.
 
To keep the accuracy of all calculated first derivatives to be the same, we use the following formulas:
 
For the first data point, we use the forward divided difference formula
        f ‘(x) =(-f(x+2h) + 4 f(x+h) -3 f(x))/(2h)+order(h^2)
 
For the interior points, we use the central divided difference formula
       f ‘(x) =(f(x+h) -f(x-h))/(2h)+order(h^2)
 
For the last data point, we use the backward divided difference formula
       f ‘(x) =(f(x-2h) – 4 f(x-h) +3 f(x))/(2h)+order(h^2)
 
Here are the links for the program.
The mfile is here
The published version of the mfile is here

%% HOW DO I DO THAT IN MATLAB SERIES?
% In this series, I am answering questions that students have asked
% me about MATLAB.  Most of the questions relate to a mathematical
% procedure.

%% TOPIC
% How do I find the first derivative of a discrete function y(x) if the
% x values are equidistant.
%% SUMMARY
% Language : Matlab 2010a;
% Authors : Autar Kaw and Sri Garapati;
% Mfile available at
% http://nm.mathforcollege.com/blog/discrete_diff_equidistant_blog.m
% Last Revised : January 17, 2012;
% Abstract: This program shows you how to differentiate discrete data if
% the x values are equally spaced
clc
clear all

%% INTRODUCTION

disp(‘ABSTRACT’)
disp(‘   This program shows you how to differentiate discrete data if’)
disp(‘   the x values are equally spaced ‘)

disp(‘ ‘)
disp(‘AUTHOR’)
disp(‘   Autar Kaw and Sri Garapati of http://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘MFILE SOURCE’)
disp(‘   http://nm.mathforcollege.com/blog/discrete_diff_equidistant_blog.m’)
disp(‘ ‘)
disp(‘LAST REVISED’)
disp(‘   January 17, 2012’)
disp(‘ ‘)

%% INPUTS

% Inputs assuming
%    that three or more points are given
%    all x values are equidistant
%    x values are in ascending or descending order.
x=[2.3   3.4   4.5    5.6   6.7   7.8];
y=[4.6   7.9   13.0   12.3  3.2   1.9];
%% DISPLAYING INPUTS
disp(‘  ‘)
disp(‘INPUTS’)
% Creating a matrix to print input data as a table
data=[x;y]’;
disp(‘   X Data     Y Data’)
% Printing the input data as a table
disp(data)

%% THE CODE

% n returns the number of data points
n=length(x);
% delta is the distance between consecutive x values
delta=x(2)-x(1);

% “dy” is an array which stores the value of the derivative at each x-value

% finding the derivative from the 2nd order accurate forward divided
% difference formula at the first data point
dy(1)=(-y(3)+4*y(2)-3*y(1))/(2*delta);

% finding the derivative from the 2nd order accurate central divided
% difference formula at the second to (n-1)th data point
for i=2:1:n-1
    dy(i)=(y(i+1)-y(i-1))/(2*delta);
end

% finding the derivative from the 2nd order accurate backward divided
% difference formula at the first data point
dy(n)=(y(n-2)-4*y(n-1)+3*y(n))/(2*delta);

% creating a matrix with input data and calculated derivative value at
% each data point for printing as a table
xdy=[x’ y’ dy’];

%% DISPLAYING OUTPUTS
disp(‘  ‘)
disp(‘OUTPUTS’)
disp(‘     XData   YData  Derivative’)
% printing the input data points and calculated derivative values(Outputs)
disp(xdy)
_________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, the textbook on Introduction to Programming Concepts Using MATLAB, and the YouTube video lectures available at http://nm.mathforcollege.com/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.