Differences and Approximate Derivatives
Y = diff(X)
Y = diff(X,n)
Y = diff(X,n,dim)
X is a vector of length
Y = diff(X) returns a vector of length
The elements of
Y are the differences between adjacent
Y = [X(2)-X(1) X(3)-X(2) ... X(m)-X(m-1)]
X is a nonempty, nonvector p-by-m
Y = diff(X) returns a matrix of size
(p-1)-by-m, whose elements are the differences between the rows of
Y = [X(2,:)-X(1,:); X(3,:)-X(2,:); ... X(p,:)-X(p-1,:)]
X is a 0-by-0 empty matrix,
Y = diff(X) returns a 0-by-0 empty matrix.
Create a vector, then compute the differences between the elements.
X = [1 1 2 3 5 8 13 21]; Y = diff(X)
Y = 0 1 1 2 3 5 8
Y has one fewer element than
Create a 3-by-3 matrix, then compute the first difference between the rows.
X = [1 1 1; 5 5 5; 25 25 25]; Y = diff(X)
Y = 4 4 4 20 20 20
Y is a 2-by-3 matrix.
Create a vector and compute the second-order difference between the elements.
X = [0 5 15 30 50 75 105]; Y = diff(X,2)
Y = 5 5 5 5 5
Create a 3-by-3 matrix, then compute the first-order difference between the columns.
X = [1 3 5;7 11 13;17 19 23]; Y = diff(X,1,2)
Y = 2 2 4 2 2 4
Y is a 3-by-2 matrix.
diff function to approximate partial derivatives with the syntax
Y = diff(f)/h, where
f is a vector of function values evaluated over some domain,
h is an appropriate step size.
For example, the first derivative of
sin(x) with respect to
cos(x), and the second derivative with respect to
-sin(x). You can use
diff to approximate these derivatives.
h = 0.001; % step size X = -pi:h:pi; % domain f = sin(X); % range Y = diff(f)/h; % first derivative Z = diff(Y)/h; % second derivative plot(X(:,1:length(Y)),Y,'r',X,f,'b', X(:,1:length(Z)),Z,'k')
In this plot the blue line corresponds to the original function,
sin. The red line corresponds to the calculated first derivative,
cos, and the black line corresponds to the calculated second derivative,
Create a sequence of equally-spaced datetime values, and find the time differences between them.
t1 = datetime('now'); t2 = t1 + minutes(5); t = t1:minutes(1.5):t2
t = 1×4 datetime array 24-Feb-2017 11:40:07 24-Feb-2017 11:41:37 24-Feb-2017 11:43:07 24-Feb-2017 11:44:37
dt = diff(t)
dt = 1×3 duration array 00:01:30 00:01:30 00:01:30
diff returns a
X— Input array
Input array, specified as a vector, matrix, or multidimensional
X can be a numeric array, logical array,
datetime array, or duration array.
Complex Number Support: Yes
n— Difference order
Difference order, specified as a positive integer scalar or
The default value of
n is 1.
It is possible to specify
large so that
dim reduces to a single (
= 1) dimension. When this happens,
calculating along the next array dimension whose size does not equal
1. This process continues until a 0-by-0 empty matrix is returned.
dim— Dimension to operate along
Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.
Consider a two-dimensional p-by-m input array,
diff(A,1,1) works on successive
elements in the columns of
A and returns a (p-1)-by-m
diff(A,1,2) works on successive
elements in the rows of
A and returns a p-by-(m-1)
Y— Difference array
Difference array, returned as a scalar, vector, matrix, or multidimensional
X is a nonempty array, then the dimension
X acted on by
diff is reduced
in size by
n in the output.
This function supports tall arrays with the limitations:
You must use the three-input syntax
Y = diff(X,N,dim).
For more information, see Tall Arrays.
Usage notes and limitations:
If supplied, the arguments representing the number
of times to apply
diff and the dimension along
which to calculate the difference must be constants.
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).