## Introduction to MATLAB Derivative of Function

MATLAB contains a variety of commands and functions with numerous utilities. This article is focussed on understanding how MATLAB command ‘diff’ can be used to calculate the derivative of a function. ‘diff’ command in MATLAB is used to calculate symbolic derivatives. In its simplest form, a function, whose derivative we wish to compute, is passed as an argument to the diff command.

**Understanding of Differentiation or Derivatives**

Differentiation is a fundamental calculus tool that represents considerable small changes in quantities. Differentiation is a rate at which a function changes w.r.t one of its variables. It calculates the sensitivity to change of an output value with respect to change in its input value.

For example, an object’s velocity is the derivative of the position of that moving object with respect to time. The derivate Velocity here shows how quickly the position of the object changes when time moves. In mathematical terms, it can be shown as dx,dy,dz, etc. It can be written as f(x)/dy, where dy will represent the small change of f (x) with respect to x.

Now that we have refreshed our concepts of differentiation, let us now understand how it is computed in MATLAB.

**Syntax of derivative:**

- diff (f)
- diff (f, var)
- diff (f, n)
- diff (f, var, n)

### Examples of Derivative of Function in MATLAB

Now we will understand the above syntax with the help of various examples

#### 1. diff (f)

diff (f) will differentiate ‘f’ with the variable identified by symvar (f,1)

Here is an example where we compute the differentiation of a function using diff (f):

4.5 (6,739 ratings)

View Course

Lets us take a sine function defined as:

sin (x ^ 3)

**Code:**

`syms f(x)`

f(x) = sin (x ^ 3);

df = diff (f, x)

**Output:**

The function will return the differentiated value of function sin (x ^ 3):

3 x^2 cos (x ^ 3)

#### 2. diff (f, var)

diff (f, var) will differentiate ‘f’ w.r.t the variable which is passed as an argument (mentioned as ‘var’ in the command).

Here is an example where we compute differentiation of a function using diff (f, var):

Let us take a sin function defined as:

sin (x * t ^ 4)

**Code:**

`syms x t`

diff (sin (x* t ^ 4))

**Output:**

The function will return the differentiated value of function sin (x * t ^ 4):

`t ^ 4 cos(t ^ 4 x)`

As we can notice, the function is differentiated w.r.t ‘t’

#### 3. diff (f, n)

diff (f, n) will compute nth derivative (as passed in the argument) of the function ‘f’ w.r.t the variable determined using symvar.

Here is an example where we compute differentiation of a function using diff (f, n):

Let us take a function defined as:

4t ^ 5

We will compute the 3^{rd}, 4^{th} and 5^{th} derivative of our function.

**Code:**

`syms t`

d3 = diff (4 * t ^ 5, 3)

d4 = diff (4 * t ^ 5, 4)

d5 = diff (4 * t ^ 5, 5)

**Output:**

The function will return 3^{rd}, 4^{th} & 5^{th} derivative of function 4t ^ 5 as below:

d3 = 240 t ^ 2

d4 = 480 t

d5 = 480

#### 4. diff (f, var, n)

diff(f, var, n) will compute ‘nth’ derivative of given function ‘f’ w.r.t the variable passed in argument (mentioned as ‘var’ in the syntax).

Here is an example where we compute differentiation of a function using diff (f, var, n):

Let us take a function defined as:

x * sin (x * t)

**Code:**

`syms x t`

diff (x * sin(x * t), t, 3)

**Output:**

The function will return 3^{rd} derivative of function x * sin (x * t), differentiated w.r.t ‘t’ as below:

-x^4 cos(t x)

As we can notice, our function is differentiated w.r.t. ‘t’ and we have received the 3^{rd} derivative (as per our argument). So, as we learned, ‘diff’ command can be used in MATLAB to compute the derivative of a function. We can also control the degree of derivative that we want to calculate by passing ‘n’ (for nth derivative) as an argument.

### Recommended Articles

This is a guide to MATLAB Derivative of Function. Here we discuss the introduction, understanding of differentiation or derivatives, syntax and the examples in MATLAB. You can also go through our other suggested articles to learn more –