# MATLAB – Differentiation

In general, differentiation is nothing but the rate of change in a function based on one of its variables. MATLAB is very useful in solving these derivatives, integrals etc. There are certain rules to be followed while solving derivatives, which will be discussed in the later part. Let’s see some examples to understand things better.

**Syntax:**

diff(f,n)

Parameters:

f:Functionn:Order of derivative

**Example 1: **

## MATLAB

`% MATLAB program to illustrate` `% differentiation using diff() function` ` ` `syms t` ` ` `% function f(t) to be passed into diff()` `f = 3*t^2 + 2*t^(-2);` `diff(f)` |

**Output:**

ans = 6*t - 4/t^3

**Elementary Rules of Differentiation**

Let’s quickly recall the rules to be followed while solving and manipulating the functions. Let us consider the same traditional notation for representing the order of derivative function (i.e f'(x) for first-order derivative and f”(x) for second-order derivative). Following are some important rules of differentiation:

**Rule 1:**

For any functions, **f **and **g, b, **any real numbers **a **and **b **are the constants of the functions.

h(x) = af(x) + bg(x), with respect toxis h'(x) = af'(x) + bg'(x)

**Rule 2:**

The **sum **and ** subtraction **rules of derivatives are as follows:

(f(x) + g(x))' = f'(x) + g'(x) (f(x) - g(x))' = f'(x) - g'(x)

**Rule 3:**

If h(x) is product of two functions f(x) and g(x), then h'(x) will be:

(f(x) * g(x))' = (f'(x) * g(x)) + (f(x) * g'(x))

**Rule 4:**

The **quotient** rule states that *(Low * derivative of High) – (High * derivative of Low), divided by (square of the Low)*. Let’s understand it better by taking function f(x) and g(x).

( f/g )' = (g*f' - fg') / g^{2}

**Rule 5:**

The **reciprocal **rule is defined as, if **f(x) **is a function, then the derivative of its reciprocal (i.e 1/f) will be as follows.

(1/f(x))' = -f / f^{2}

**Rule 6:**

The **power** rule is described as if **f(x)** **= y ^{n }**is a function, then it’s derivative is.

y(n)' = n * y^{n-1}

Now let’s see some examples to understand the above rules better.

**Example 2:**

## MATLAB

`% MATLAB program to illustrate` `% rules of derivatives` ` ` `% Sum rule` `f = 2*x + 3*y;` `sumDer = diff(f)` ` ` `% Subtraction rule` `f = x^3 - 2;` `subDer = diff(f)` ` ` `% Product rule` `f = x^3 * 5;` `prodDer = diff(f)` ` ` `% Quotient rule` `f = (2*x^2)/(x^2 + 2);` `quoDer = diff(f)` ` ` `f = (x^2 + 1)^17;` `powDer = diff(f)` |

**Output:**

sumDer = 2 subDer = 3*x^2 prodDer = 15*x^2 quoDer = (4*x)/(x^2 + 2) - (4*x^3)/(x^2 + 2)^2 powDer = 34*x*(x^2 + 1)^16