File Exchange

image thumbnail


version (4.12 KB) by Robert Canfield
Calculates numeric gradient using second-order difference formula for unevenly spaced data


Updated 01 Oct 2015

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

The central_diff function calculates a numeric gradient using second-order accurate difference formula for evenly or unevenly spaced coordinate data. It operates in a similar fashion to the MATLAB function, gradient, except that it permits only one independent variable, x, and correctly handles unevenly spaced values of the x-coordinate data. Accuracy is increased at the ends relative to the MATLAB gradient function, which uses only first-order forward or backward differences at the ends, by instead using second-order forward difference at the left end and second-order backward difference at the right end.

Cite As

Robert Canfield (2020). central_diff.m (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Alvaro Valcarce

I think that line 98 should be (notice the "=" sign)

if n >= 3

to account for the case of length 3 vectors.

Shyang-Wen Tseng

This is a very good and usefull add-on function. Thank you.

godlove njie teku


Second-order accurate forward and backward difference formulae are used now at the left and right ends, instead of first-order forward/backward differences at the ends. Logical operators & and | were replaced with && and ||, respectively.


updating description

updating description

update description

update description


MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Inspired: central_diff2(y,deltat,d,a), DGradient