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

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

% How do I find the first derivative of a discrete function y(x) if the
% x values are equidistant.
% Language : Matlab 2010a;
% Authors : Autar Kaw and Sri Garapati;
% Mfile available at
% http://numericalmethods.eng.usf.edu/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
clear all


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

disp(‘ ‘)
disp(‘   Autar Kaw and Sri Garapati of https://autarkaw.wordpress.com’)
disp(‘ ‘)
disp(‘   http://numericalmethods.eng.usf.edu/blog/discrete_diff_equidistant_blog.m’)
disp(‘ ‘)
disp(‘   January 17, 2012’)
disp(‘ ‘)


% 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];
disp(‘  ‘)
% Creating a matrix to print input data as a table
disp(‘   X Data     Y Data’)
% Printing the input data as a table


% n returns the number of data points
% delta is the distance between consecutive x values

% “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

% 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

% finding the derivative from the 2nd order accurate backward divided
% difference formula at the first data point

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

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

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, 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://numericalmethods.eng.usf.edu/videos.  Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.


Author: Autar Kaw

Autar Kaw (http://autarkaw.com) is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare (nm.MathForCollege.com) annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

One thought on “Differentiating a Discrete Function with Equidistant Points”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: