# Differentiation and Integration in Matlab

Symbolic expressions can allow for the evaluation of equations as shown in a previous post on symbolics. Symbolics can further be used to solve equations that vary with time or with respect to one another. Calculating this change, either as a derivative or integral, can be done implicitly using the functions ‘sym’,’diff’, and ‘int’.

Let’s start out by initializing our symbolic variables x, y, and z.

>> syms x y z

As we’ve shown before, a function f(x) can be written based on these symbolics. In this case, let’s look at a simple equation sin(x) and its derivative.

>> f = sin(x);
>> deriv_f = diff(f)
deriv_f =
cos(x)

The ‘diff’ operator will return the difference between the n and n+1 row or column of data if applied to an array of numbers. When applied to symbolic variables, ‘diff’ will return the approximate derivative. The derivative, as defined geometrically, is the tangent of a curve at a given point. When applied to a function, the derivative returns a new function that is the tangent at every point in the domain. Mathematically, this derivative is the infinitesimally small change in the dependent variable from point a to a+h, over the change in h, where h approaches 0 (similar to rise/run or slope for linear equations). The derivative of our function f(x) is equal to cos(x).

To evaluate this derivative, or slope of f(x), at a certain value on the curve, we can use the function ‘subs’. For example to evaluate f'(pi):

>> subs(deriv_f,pi)
ans =
-1

A partial derivative can also be performed in Matlab. A partial derivative is defined as a derivative of a multivariable function with respect to one variable, with all other variables treated as constants. Let’s generate a new equation based on x, y, and z: g(x,y,z) = x*y^2 – sin(z). There are 3 possible partial derivatives of this equation: 1) g’x, with y and z treated as constants; 2) g’y, with x and z treated as constants; 3) g’z, with x and y treated as constants. Let’s see how all 3 of those are evaluated.

>>g = x*y^2 - sin(z);

>>deriv_gx = diff(g,x)
deriv_gx =
y^2

>>deriv_gy = diff(g,y)
deriv_gy =
2*x*y

>>deriv_gz = diff(g,z)
deriv_gz =
-cos(z)

The opposite of a derivative is the integral, or the anti-derivative, which provides the area under a curve. The first fundamental theorem of calculus guarantees that a continuous function will have an antiderivative. Matlab also has a nice built-in function ‘int’ which allows us to both determine the explicit and numerical solutions to an equation. (Note: sometimes an explicit solution cannot be found when evaluated on a non-continuous function). We can further use ‘int’ to calculate both the indefinite (int(f)) and definite integrals over the range of a to b (int(f,a,b)). Let’s evaluate the integral of f(x), both based on the indefinite integral and over the range of 0 to pi:

>> int(f)
ans =
-cos(x)

>> int(f,0,pi)
ans =
2

Just like the ‘diff’ function, we can utilize the ‘int’ function to evaluate an equation with respect to x (dx), y (dy) or z (dz). This is specified with the 2nd argument (i.e. ‘int(g,x)’). If a definite integral is to be performed, this can be done using 4 arguments, with limits a to b specified (i.e. ‘int(g,y,a,b)’).

>> int(g,y)
ans =
(x*y^3)/3 - y*sin(z)

>> int(g,y,0,2)
ans =
(8*x)/3 - 2*sin(z)

We can even evaluate multiple integrals for functions with more than one variable! While a single integral can be thought of as the area under the curve, a double integral can be visualized as the volume underneath a surface. Let’s take the following double integral for example:

>> h = x*y^2
h =
x*y^2

>> h_intx = int(h,x,0,2)
h_intx =
2*y^2

>>h_doubleint int(h_intx,y,0,1)
h_doubleint=
2/3

While we have covered a couple of useful functions to estimate the implicit and explicit results by using symbolic variables, we’ll investigate other methods for determining the slope (derivative) and area under the curve (integral) of functions in future posts. Let us know if you have other suggestions for using symbolics in the comments or on our forums.

## 5 thoughts on “Differentiation and Integration in Matlab”

1. Hi all
How we can perform derivate of a function and then calculate integral for it
Let me show the mean:
syms x y;
f=@(x,y) (y^2)/sin(x);
diff(f,x)

now I wanna evaluate integral of diff(f,x) respect to y

I was wondering if you could suggest any idea

2. Useful and understandable article, good job!

Anyway I think there are a couple of typos.
1. In the box dedicated to the partial derivatives, all three of them are named”deriv_gx”. I think it is clearer to name them “deriv_gx”, “deriv_gy” and “deriv_gz”.

2. In the last box the integration ranges are swapped. The first integration should be in x from 0 to 1 and the second in y from 0 to 2 as the order in the equation suggests.

• Thanks for the comments Domenico!

I changed your #1 comment as suggested. Good catch.

As for your second comment, I’d have to respectfully disagree. When performing double integrals, you work from inside out, therefore, the limits of x are from 0:2 and for y from 0:1 in this example.

Take care,
Vipul