## Introduction to Differentiation in Matlab

Differentiation in Matlab is used to find the rate of change of a quantity w.r.t the other. For example, differentiation can be used to calculate the rate at which velocity changes with time (which is acceleration). Using differentiation, we can also find the rate at which ‘x’ changes w.r.t ‘y’. This change in ‘x’ w.r.t ‘y’, when represented using a graph, will give gradient for the curve.

**Syntax**

Below is the syntax for Differentiation in Matlab:

`diff (A)`

diff (A, var)

diff (A, n)

**Explanation:** diff (A) will calculate the differentiation of A w.r.t variable provided by symvar (A, 1). diff (A, var) can be used to calculate the differentiation of A w.r.t the desired variable, i.e. the variable passed as an argument. diff (A, n) can be used to get the ‘nth’ derivative of the function. By default, ‘diff (A)’ provides us with 1^{st} derivative of the input function.

### Examples to Implement Differentiation in Matlab

Let us now understand the code to calculate the differentiation in MATLAB using ‘diff (A) function’:

#### Example #1

In this example, we will use a polynomial function of degree 4 and will differentiate it w.r.t the default variable. We will follow the following 2 steps:

**Step 1:** Create the function of degree 4 in MATLAB

**Step 2:** Calculate the differentiation using ‘diff function’

**Code:**

`syms x`

A = x.^5 + 7*x.^4 - 2*x.^2

diff (A)

**Output:**

**Explanation:** Initializing the variable ‘x’. Creating the polynomial function of degree 4. Passing input function ‘A’ to the diff function. Mathematically, the differentiation of x.^5 + 7*x.^4 – 2*x.^2 w.r.t ‘x’ is 5*x^4 + 28*x^3 – 4*x. As we can see in the output, we have obtained differentiation of our input function ‘A’ as 5*x^4 + 28*x^3 – 4*x using ‘diff (A) function’, which is the same as expected by us.

#### Example #2

In this example, we will use a polynomial function of variables ‘x’ and ‘t’ and will differentiate it w.r.t ‘t’. We will follow the following 2 steps:

**Step 1:** Create the function of variables ‘x’ and ‘t’

**Step 2:** Calculate the differentiation using ‘diff (A, var) function’

**Code:**

`syms x t`

A = sin(x*t^2) - cos(x*t^3)

diff (A, t)

3 Online Courses | 1 Hands-on Project| 8+ Hours| Verifiable Certificate of Completion

4.5

View Course

**Output:**

**Explanation:** Initializing the variable ‘x’ & ‘t’. Creating the polynomial function with variables ‘x’ and ‘t’. Passing input function ‘A’ to the diff function]. Please note that we have passed ‘t’ as 2^{nd} argument. Our input ‘A’ will be differentiated w.r.t this variable ‘t’. Mathematically, the differentiation of sin (x*t^2) – cos (x*t^3) w.r.t ‘t’ is 2*t*x*cos (t^2*x) + 3*t^2*x*sin (t^3*x). As we can see in the output, we have obtained differentiation of our input function ‘A’ as 2*t*x*cos (t^2*x) + 3*t^2*x*sin (t^3*x) using ‘diff (A, var) function’, which is same as expected by us.

#### Example #3

In this example, we will use a polynomial function of degree 6 and will find its 3^{rd} derivative. We will follow the following 2 steps:

**Step 1: **Create the function of degree 6

**Step 2: **Calculate the differentiation using ‘diff (A, n) function’

**Code:**

`syms x`

A = 5*x.^6 + 4*x.^5 - 2*x.^2

diff (A, 3)

**Output:**

**Explanation:** Initializing the variable ‘x’. Creating the polynomial function of degree 6. Passing input function ‘A’ to the diff function. Please note that we have passed ‘3’ as 2^{nd} argument. This will give us the 3^{rd} derivative of our input function. Mathematically, the 3^{rd} derivative of 5 * x.^6 + 4 * x.^5 – 2 * x.^2 is 600 * x^3 + 240 * x^2. As we can see in the output, we have obtained the 3rd derivative of our input function ‘A’ as 600*x^3 + 240*x^2 using ‘diff (A, n) function’, which is the same as expected by us.

### Conclusion

‘Differentiation’ can be done in MATLAB using diff function. By default, the differentiation is done w.r.t the variable identified by ‘symvar’. If we need differentiation w.r.t the variable of our choice, we can use diff (A, var). diff (A, n) can be used to get the ‘nth’ derivative in MATLAB.

### Recommended Articles

This is a guide to Differentiation in Matlab. Here we discuss an introduction to Differentiation in Matlab, syntax with programming examples. You can also go through our other related articles to learn more –