# Differentiation in Matlab

## Differentiation in Mathematics

In maths, Derivatives are an essential device of math or calculus. For instance, derivative of the situation of a moving article regarding time is the item's speed: this action how rapidly the situation of the item changes with time period.

The derivative of a component of a variable at a given input, when it exists, is the slant of the digression line to the chart of the function by then. Thus, the derivative is frequently depicted as the "prompt pace of progress", the proportion of the quick change in the reliant variable to that of the autonomous variable.

Derivative can be summed up to elements of a few genuine factors. In this speculation, the derivative is re-evaluated as a straight change whose chart is (after a fitting interpretation) the best direct estimation to the diagram of the first capacity.

For better understanding we try to compare with Jacobean matrix:

The Jacobian matrix that addresses this straight change regarding the premise given by the decision of free and ward factors. It very well may be determined as far as the halfway derivative as for the autonomous factors. For a genuine esteemed capacity of a few factors, the Jacobian matrix lessens to the angle vector.

The way toward tracking down a derivative is called **differentiation**. The converse cycle is called **antidifferentiation**. The central hypothesis of analytics relates antidifferentiation with coordination. Separation and coordination comprise the two central activities in single-variable calculus.

## Differentiation in Matlab

In Matlab, Differentiation is utilized to discover the rate of progress of an amount with respect to the quantity of other. For instance, differentiation can be utilized to compute the rate at which speed changes with the given time (which is speed increase). Utilizing differentiation separation, we can likewise discover the rate at which 'p' changes with respect to 'q'. This adjustment of 'p' with respect to 'q', when addressed utilizing a diagram, will give slope for the bend.

**Syntax**

In Matlab, syntax for Differentiation:

```
diff (B)
diff (B, variable)
diff (B, M)
```

**Explanation: **diff (B) statement will compute the differentiation value of B with respect to the given variable provided by symbol variable (B, 1). diff (B, variable) can be used to compute the differentiation value of B with respect to the required variable, that is the variable sent as an value argument type. diff (B, m) can be used to get the last value of differentiation of the defined function. Generally, 'diff (B)' gives us with first derivative of the given input function by the users.

## General example:

Here we will use the function polynomial of variables 'y' and 's' and will differentiate it with respect to 's'. We will complete this in two steps:

**Step 1:** we will Create a function of variables 'y' and 's'

**Step 2:** we will compute the differentiation using function 'diff (B, var)'

**Example Code: **Create a script file code and run through command prompt.

```
syms y s
B = sin(y*s^5) - cos(y*s ^ 4)
diff (B, s)
```

**Output:**

```
B =
sin(s^5*y) - cos(s^4*y)
ans =
5*s^4*y*cos(s^5*y) + 4*s^3*y*sin(s^4*y)
```

## Explanation of the above example:

Introducing the variable 'y' and 's'. Making the polynomial capacity with factors 'y' and 's'. Passing info work 'B' to the diff function]. Kindly note that we have passed 's' as second contention. Our info 'B' will be separated with respect to this variable 's'. Numerically, the separation of transgression (x*t^2) – cos (x*t^3) with respect to 's' is 5* s^4*y*cos(s^5*y) + 4*s^3*y*sin(s^4*y)

. As we can find in the yield i.e output, we have gotten separation of our info work 'B' as 5*s^4*y*cos(s^5*y) + 4*s^3*y*sin(s^4*y)

utilizing 'diff (B, var) work', which is same true to form by us.

## Elementary Rules of Differentiation

Let us explore different equations for differentiation of any given functions and verify the prescribed rules.

The elementary type rules for differentiation.

**Rule 1:**

For f1 and g1 functions and a and b are real numbers the derivative of any function would be -

```
h1(z) = af1(z) + bg1(z) with respect to z is given by -
h1'(z) = af1'(z) + bg1'(z)
```

**Rule 2:** The **sum** and **subtraction** rules

```
(f1 + g1)' = f1' + g1'
(f1 – g1)' = f1' – g1'
```

**Rule 3:** The **product** rule

`(f1.g1)' = f1'.g1 + g1'.f1`

**Rule 4:** The **quotient** rule

`(f1/g1)' = (f1'.g1 – g1'.f1)/g12`

**Rule 5:** The **polynomial** or power rule

`if y = f1(y) = yn, then f1' = n. y(n-1)`

An immediate output is that the derivative of this rule's any constant number is zero, i.e., if **x = k**, any number constant, then we get

**f1' = 0**

**Rule 6: **The **chain** rule

`H1'(y)= f1'(g1(y)).g1'(y)`

**Example**

Create a script file code and run through command prompt.

```
syms syms y
syms s
f1 = (y +2)*(y^2 + 3)
deri1 = diff(f1)
f1 = (s^2 + 3)*(sqrt(s) + s^3)
deri2 = diff(f1)
f1 = (y^2 - 2*y + 1)*(3*y^3 - 5*y^2 + 2)
deri3 = diff(f1)
f1 = (2*y^2 + 3*y)/(y^3 + 1)
deri4 = diff(f1)
f1 = (y^2 + 1)^17
deri5 = diff(f1)
f1 = (s^3 + 3* s^2 + 5*s -9)^(-6)
deri6 = diff(f1)
```

**Output:**

```
f1 =
(y^2 + 3)*(y + 2)
deri1 =
2*y*(y + 2) + y^2 + 3
f1 =
(s^(1/2) + s^3)*(s^2 + 3)
deri2 =
(s^2 + 3)*(3*s^2 + 1/(2*s^(1/2))) + 2*s*(s^(1/2) + s^3)
f1 =
(y^2 - 2*y + 1)*(3*y^3 - 5*y^2 + 2)
deri3 =
(2*y - 2)*(3*y^3 - 5*y^2 + 2) - (- 9*y^2 + 10*y)*(y^2 - 2*y + 1)
f1 =
(2*y^2 + 3*y)/(y^3 + 1)
deri4 =
(4*y + 3)/(y^3 + 1) - (3*y^2*(2*y^2 + 3*y))/(y^3 + 1)^2
f1 =
(y^2 + 1) ^ 17
deri5 =
34*y*(y^2 + 1)^16
f1 =
1/(s^3 + 3*s^2 + 5*s - 9)^6
deri6 =
-(6 * (3 * s ^ 2 + 6 * s + 5))/(s^3 + 3 * s ^ 2 + 5 * s - 9) ^ 7
When you run the file, MATLAB displays the following result -
```

## Derivatives of Functions like Logarithmic, Exponential, and Trigonometric.

The derivatives of few important exponential, trigonometric and logarithmic functions are provided in the following table -

Functions | Derivative of the function |

ca.y | ca.y.ln c.a (ln is log) |

ey | Ey |

ln y | 1/y |

lncy | 1/y.ln o |

yy | yy.(1 + ln y) |

sin(y) | cos(y) |

cos(y) | -sin(y) |

tan(y) | sec2(y), or 1/cos2(y), or 1 + tan2(y) |

cot(y) | -csc2(y), or -1/sin2(y), or -(1 + cot2(y)) |

sec(y) | sec(y).tan(y) |

csc(y) | -csc(y).cot(y) |

## The above functions are demonstrated through various symbols and variable in the below example.

Create a script file code and run through command prompt.

```
syms p
y1 = exp(p)
diff(y1)
y1 = p^9
diff(y1)
y1 = sin(p)
diff(y1)
y1 = tan(p)
diff(y1)
y1 = cos(p)
diff(y)
y1 = log(p)
diff(y)
y1 = log10(p)
diff(y)
y1 = sin(p)^2
diff(y1)
y1 = cos(3*p^2 + 2*p + 1)
diff(y1)
y1 = exp(p)/sin(p)
diff(y1)
```

**Output:**

```
y1 =
exp(p)
ans =
exp(p)
y1 =
p^9
ans =
9*p^8
y1 =
sin(p)
ans =
cos(p)
y1 =
tan(p)
ans =
tan(p)^2 + 1
y1 =
cos(p)
ans =
1
y1 =
log(p)
ans =
1
y1 =
log(p)/log(10)
ans =
1
y1 =
sin(p)^2
ans =
2*cos(p)*sin(p)
y1 =
cos(3*p^2 + 2*p + 1)
ans =
-sin(3*p^2 + 2*p + 1)*(6*p + 2)
y1 =
exp(p)/sin(p)
ans =
exp(p)/sin(p) - (exp(p)*cos(p))/sin(p)^2
```

## Conclusion

In MATLAB, ' Differentiation' operations should be possibly using diff() function. Few insights drawn from the analysis of our examples is

- Naturally, the differentiation is done with respect to the variable distinguished by 'symvar' (symbol variable).

- In the above instances, we also need separation with respect to our variable, so diff (B, variable) and diff (B, m) functions can be utilized to get the 'nth' subordinate of equation in MATLAB.