The diff() Function in MATLAB

This tutorial will discuss finding differences and approximate derivatives using the diff() function in Matlab.

Find Differences and Approximate Derivatives Using the diff() Function in MATLAB

The diff() function is used to find the differences and approximate derivatives in Matlab. The syntax: diff(x) is used to find the differences between adjacent elements of a vector or matrix. If the input is a vector, then the difference will be the difference between adjacent values of the input vector. The size of the output vector will be one less than the size of the input vector. For example, let’s find the difference between the values of a vector. See the code below.

clc
x = [1 3 6 9];
y = diff(x)

Output:

y =

     2     3     3

In the output, the difference between the first two elements of the input vector 1 and 3 is two, which is stored in the variable y. If the input is a matrix, then the difference will be the difference between the rows of the input matrix, and the size of the rows will be equal to the difference between the length of the rows and the order of the difference. For example, let’s find the difference between the values of a vector. See the code below.

clc
x = [1 3 6 9; 1 2 3 4]
y = diff(x)

Output:

x =

     1     3     6     9
     1     2     3     4


y =

     0    -1    -3    -5

In the output, the difference between the first two elements of the first two rows of the input matrix is 0, which is stored in the variable y. If we increase one row in the input matrix, one row will also increase in the output matrix. We can also find the nth time difference between the vector or matrix elements using the second argument of the diff() function. For example, the diff(x,2) function will find the second-order difference between the input vector or matrix values. The statement diff(x,2) is same as the statement diff(diff(x)). For example, let’s find the 2nd order difference between the values of the above vector. See the code below.

clc
x = [1 3 6 9]
y = diff(x,2)

Output:

x =

     1     3     6     9


y =

     1     0

In the output, the size of the output vector has also decreased because the size will be equal to the difference between the length of the input vector and the order of difference. In the case of higher-order, the diff() function calls itself recursively to find the difference. We can also find the difference between the columns of a matrix instead of the row using the third argument of the diff() function. For example, let’s find the difference between columns of the above matrix. See the code below.

clc
x = [1 3 6 9; 1 2 3 4]
y = diff(x,1,2)

Output:

x =

     1     3     6     9
     1     2     3     4


y =

     2     3     3
     1     1     1

In case of difference between columns, the size of the columns will be equal to the difference between the length of columns and order of difference, and the row size will remain the same. We can also find the partial derivative of a function using the diff(f)/h function. Where f is the given function and h is the step size. For example, let’s find the partial derivative of sin(x) and plot it on a graph using the plot() function. See the code below.

h = 0.001;       
x = -2*pi:h:2*pi;    
f = sin(x);      
y = diff(f)/h;  
plot(x(:,1:length(y)),y,x,f)
legend('sin(x)','cos(x)')

Output:

derivative of sine wave

In the output, the blue line is the sine wave, and the red line is the cosine wave which is the derivative of the sine wave. The legend() function is used to draw legends on the graph to indicate which plot belongs to which data.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - MATLAB Vector

  • MATLAB Eigenvalues and Eigenvectors
  • Make a Column Vector in MATLAB