Differential or Derivatives in MATLAB


In mathematics, differential or derivative is one of the fundamental operations used to determine the rate of change of a function. Therefore, derivatives are crucial to analyze various scientific and engineering processes or functions. We can perform derivates using various mathematical tools like MATLAB.

In this tutorial, we will explore how to calculate derivative of a given mathematical function using MATLAB. But before that let's first get an overview of derivative.

What are Derivatives?

Derivative is a mathematical operation used to compute the rate of change occurring in a function or a process. In terms of geometry, derivates is defined as a slope of a tangent line to the curve of a given mathematical function at a certain point.

Mathematically, if f is a function of x, i.e. f(x), then its derivative can be represented as f'(x) or df/dx. Here, the f'(x) represents the rate of change in the function f(x) at each value of x.

In MATLAB, the derivative of a given mathematical function can be calculated by using a built-in function 'diff()'.

The 'diff' function can calculate the derivative of given numerical data or symbolic expressions. Based on different use cases, the 'diff' function can take various different syntaxes given below −

  • f' = diff(f);

  • f' = diff(f, n);

  • f'= diff(f, n, dim);

Now, we will explore each of these syntaxes of the 'diff' function with example programs.

(1). Calculate First Derivative of a Function

Syntax

The first derivative of a function can be calculated by using the following syntax of the 'diff' function −

f' = diff(f);

Consider the following MATLAB program to understand how to calculate the first derivative of a function using the 'diff' function.

Example

% MATLAB program to calculate first derivative of a function
% Declare a symbolic variable
syms x;

% Define a function for variable x
f = x^2 + 5*x + 3;

% Calculate the derivative of the function f
dfdx = diff(f);

% Display the result
disp('Derivative of the function f is:');
disp(dfdx);

Output

Derivative of the function f is:
2*x + 5

Explanation

In this MATLAB code, we start by creating a symbolic variable 'x', then we define a function 'f' in this variable. Next, we calculate the derivative of the function 'f' using the function 'diff' and store the result in a variable 'dfdx'. Finally, we use the 'disp' function to display the result.

(2). Calculate Derivative of a Function with Respect to Specified Variable

Syntax

The following syntax of the 'diff' function is used to calculate the derivative of a function with respect to a specified variable.

f' = diff(f, b);

This will calculate the derivative of the function 'f' with respect to the variable 'b'.

Let us now take an example program in MATLAB to understand the implementation of this syntax.

Example

% MATLAB program to calculate derivative of function with respect to specified variable
% Declare symbolic variables
syms x y;

% Define a function for variable x and y
f = x^2 + 5*x*y + 3*y^2;

% Calculate the derivative of the function f with respect to y
dfdy = diff(f, y);

% Display the result
disp('Derivative of the function f with respect to y is:');
disp(dfdy);

Output

Derivative of the function f with respect to y is:
5*x + 6*y

Explanation

In this MATLAB program, firstly we create two variables 'x' and 'y'. Then, we define a function for these two variables and store in it in a variable 'f'. After that we use the 'diff' function to calculate the derivative of the function 'f' with respect to the variable 'y' and store the result in a variable 'dfdy'. Finally, we display the result using the 'disp' function.

(3). Calculate Derivative of Specified Order of a Function

Syntax

The following syntax of the 'diff' function is used to calculate the derivative of a specified order of a function −

f' = diff(f, n);

Here, 'f' is the function and 'n' is the order of derivative.

Example

Consider the following MATLAB program to understand the implementation of this function.

% MATLAB program to calculate derivative of specified order of a function 
% Declare a symbolic variable
syms x;

% Define a function for variable x
f = x^3 + 5*x^2 + 3*x;

% Calculate first-order derivative of the function f (n = 1)
dfdx1 = diff(f, 1);

% Calculate second-order derivative of the function f (n = 2)
dfdx2 = diff(f, 2);

% Calculate third-order derivative of the function f (n = 3)
dfdx3 = diff(f, 3);

% Display the results
disp('First order derivative of the function f is:');
disp(dfdx1);
disp('Second order derivative of the function f is:');
disp(dfdx2);
disp('Third order derivative of the function f is:');
disp(dfdx3);

Output

First order derivative of the function f is:
3*x^2 + 10*x + 3
 
Second order derivative of the function f is:
6*x + 10
 
Third order derivative of the function f is:
6

Explanation

The implementation and execution of this code is similar to that of the above codes.

(4). Calculate Differences Between Elements of a Vector

Syntax

We can use the following syntax of the 'diff' function to calculate the differences between elements of a vector −

D = diff(A);

Here, A is the input vector.

Example

Let us take an example to understand this syntax of the 'diff' function.

% MATLAB program to calculate differences between vector elements
% Create a vector
A = [1 2 3 4 5 6 7 8 9 10];

% Calculate the differences between elements of the vector
D = diff(A);

% Display the result
disp('Differences between elements of the vector A is:');
disp(D);

Output

Differences between elements of the vector A is:
     1     1     1     1     1     1     1     1     1

Explanation

This MATLAB code calculates the difference between two consecutive elements of the input vector starting from left.

(5). Calculate the Derivative of a Multidimensional Array along Specified Dimension

Syntax

The following syntax of the 'diff' function is utilized to calculate the derivative of a multidimensional array along a specified dimension −

D = diff(A, n, dim);

Here, A is the multidimensional array, n is the order of derivative, and 'dim' is the specified dimension.

If 'dim = 1', the derivative or difference will be calculated along the rows of the array. If 'dim = 2', the derivative of difference will be calculated along the columns of the array.

Example

The following MATLAB program demonstrates the implementation of this syntax of the 'diff' function.

% Create a multidimensional array
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% Calculate the differences along rows (dim = 1)
D_Row = diff(A, 1, 1);

% Calculate the differences along columns (dim = 2)
D_Column = diff(A, 1, 2);

% Display the original array and differences
disp('The original array A is:');
disp(A);
disp('Differences of A along rows is:');
disp(D_Row);
disp('Differences of A along columns is:');
disp(D_Column);

Output

The original array A is:
     1     2     3
     4     5     6
     7     8     9

Differences of A along rows is:
     3     3     3
     3     3     3

Differences of A along columns is:
     1     1
     1     1
     1     1

Explanation

This MATLAB code calculates the differences between along the rows (first dimension) and columns (second dimension) of the of the array A.

Conclusion

This is all about derivatives and differential in MATLAB. We have explained the concept of derivatives or differential with the help of MATLAB examples. In conclusion, MATLAB is a digital tool that provides a built-in function 'diff' to calculate the differential or derivative or difference of a given symbolic expression or numerical dataset.

Updated on: 06-Sep-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements