Integration in MATLAB (original) (raw)

Last Updated : 28 Apr, 2021

Integration is defined as the process of finding the anti derivative of a function. It is used to calculate area, volume, displacement, and many more. In this article, we will see how to perform integration on expressions in MATLAB.

There are two types of Integration:

Before moving to Integration, we first need to assign an expression to a variable in MATLAB which can be done by using the inline() function. It creates a function to contain the expression.

Syntax:

f = inline(expr, var)

Here f is the inline function created to contain the expression, expr can be any expression and var is the variable in that expression.

Now, after assigning the expression using the inline() function, we need to integrate the expression. This task can be performed using the int() function. The int() function is used to integrate expressions in MATLAB.

Syntax:

int(f,v)

Parameters:

Indefinite Integral

Indefinite integrals are those integrals that do not have any limit and containing an arbitrary constant.

Step-wise Approach:

Step 1: Use Inline function for the creation of the function for integration.

Matlab `

% create a inline function f=inline('x^2+3*x' ,'x'); g=inline( 'sin(y) + cos(y)^2', 'y');

`

Step 2: Create a symbolic function.

Matlab `

% create a symbolic function syms x; syms y;

`

Step 3: Use int to find out the integration.

Matlab `

% for the integration use int() % and pass the parameters % (expression, variable) % variable= variable for integration p=int (f(x),x); q=int (g(y),y);

`

Complete Code:

Matlab `

% create a inline function f=inline('x^2+3*x' ,'x'); g=inline( 'sin(y) + cos(y)^2', 'y');

% create a symbolic function syms x; syms y;

% variable= variable for % integration p=int (f(x),x); q=int (g(y),y);

% display p q

`

Output:

Definite Integral

Definite integrals are those integrals that have an upper limit and a lower limit. Let's take the above example and add the limits.

Step-wise Approach:

Step 1: Use the Inline function for the creation of the function for integration.

Matlab `

% create a inline function f=inline('x^2+3*x' ,'x'); g=inline( 'sin(y) + cos(y)^2', 'y');

`

Step 2: Create a symbolic function.

Matlab `

% create a symbolic function syms x; syms y;

`

Step 3: Use int to find out the integration and pass down the values of the lower limit, upper limit.

Matlab `

%for the integration use int() % and pass the parameters %(expression, lower limit, % upper limit) p=int (f(x),1,4); q=int (g(y),1,3);

% use double to know the value % in decimal double(q);

`

Complete Code:

Matlab `

% create a inline function f=inline('x^2+3*x' ,'x'); g=inline( 'sin(y) + cos(y)^2', 'y');

% create a symbolic function syms x; syms y;

% for the integration use int() and pass the parameters % (expression, lower limit, upper limit) p=int (f(x),1,4); q=int (g(y),1,3); double(q);

% display p q

`

Output: