Categories
1. Basics of Matlab

Integral and double Integral

Integral

Matlab Integral is useful in finding areas under the curves. It is the reverse of differentiation in calculus and hence the functions are integrated by finding their anti-derivatives.

Integrals are of 2 types:

1. Indefinite integrals (Integrals without limits)

2. Definite integrals (Integrals with limits)

Syntax

Let us now understand the syntax of ‘integral function’ in MATLAB:

A = integral (Fx, Xminimum, Xmaximum)

Explanation:

1. ‘integral function’ will calculate the numeric integration of input function ‘Fx’

2. ‘Xminimum’ and ‘Xmaximum’ will be used as a minimum and maximum limits for integration respectively

3. If we want to use more specific options for integral, we can use the syntax:

A = integral (Fx, Xminimum, Xmaximum, Name, Value)

Examples to Implement Matlab Integral

Let us now understand how the code for ‘integral function’ looks like in MATLAB with the help of various examples:

Example 1:

In this example, we will use a simple polynomial function of degree 2 and will integrate it between the limits 0 to 4. We will follow the following 2 steps:

Step 1: Create the function of degree 2 in MATLAB

Step 2: Use the integral function to calculate the integration

Code:

syms x
[Initializing the variable ‘x’] Fx = @(x) 4*x.^2
[Creating the polynomial function of degree 2] A = integral (Fx, 0, 4)
[Passing input function ‘Fx’ and the required limits to the ‘integral function’] [Mathematically, the integral of 4*x ^ 2, between the limits 0 to 4 is 85.3333]

Output:

integrate

Explanation: As we can see in the output, we have obtained integral of our input function ‘Fx’ as 85.3333 using ‘integral function’, which is the same as expected by us.

Example 2:

In this example, we will use a polynomial function of degree 4 and will integrate it between the limits 0 to 2. We will follow the following 2 steps:

Step 1: Create the function of degree 4 in MATLAB

Step 2: Use the integral function to calculate the integration

Code:

syms x
[Initializing the variable ‘x’] Fx = @ (x) (4 * x.^4 + x.^3 -2 * x.^2 +1)
[Creating the polynomial function of degree 4] A = integral (Fx, 0, 2)
[Passing input function ‘Fx’ and the required limits to the ‘integral function’] [Mathematically, the integral of 4 * x. ^ 4 + x. ^ 3 -2 * x. ^ 2 +1, between the limits 0 to 2 is 26.2667]

Output:

degree 4

Explanation: As we can see in the output, we have obtained integral of our input function ‘Fx’ as 26.2667 using ‘integral function’, which is the same as expected by us.

Example 3:

In this example, we will learn how to integrate a function between the limits 0 and infinity. For this example, we will use a function which is a combination of logarithmic and exponential functions. The code will comprise of the following 2 steps:

Step 1: Create a function containing logarithmic and exponential functions

Step 2: Use the integral function to calculate the integration

Code:

syms x
[Initializing the variable ‘x’] Fx = @(x) exp(-x. ^3). * log(2 * x). ^3;
[Creating the function containing the exponential and logarithmic functions] A = integral (Fx, 0, inf)
[Passing input function ‘Fx’ and the required limits to the ‘integral function’. Note that we have passed ‘inf’ which signifies infinity, as the upper limit] [Mathematically, the integral of exp (-x. ^3). * log (2 * x). ^3, between the limits 0 to infinity is         -2.9160]

Output:

Matlab Integral3

Explanation: As we can see in the output, we have obtained integral of our input function ‘Fx’ as -2.9160 using ‘integral function’, which is the same as expected by us.

Example 4:

In this example, we will learn how to use the syntax A = integral (Fx, Xminimum, Xmaximum, Name, Value)

For this example, we will use a vector function which is of the form [log(x) log(2x) log (3x) log (4x)]. The code will comprise of the following 2 steps:

Step 1: Create a function containing vector values

Step 2: Use the integral function to calculate the integration and add a ‘name-value pair’ argument

Code:

syms x
[Initializing the variable ‘x’] Fx = @(x) log((1 : 4) * x);
[Creating the function containing vector values] A = integral(Fx, 0, 2, 'ArrayValued', true)
[Passing input function ‘Fx’ and the required limits to the ‘integral function’. Note that we have passed ‘ArrayValued’, ‘true’, as the name value pair; which is used to calculate the integral of vector values]

Output:

Matlab Integral4

Explanation: As we can see in the output, we have obtained integral of all the vector values in our array using integral function and ‘name-value pair’ argument.

Double Integral:

Double Integral

Matlab Double Integral is the extension of the definite integral. In double integral, the integration is performed for functions with 2 variables. In its simplest form, integration for functions with 1 variable is done over a 1-Dimensional space, likewise, integration of functions with 2 variables is done over a 2-D space. In MATLAB, we use ‘integral2 function’ to get the double integration of a function.

Syntax:

Let us now understand the syntax of integral2 function in MATLAB:

I = integral2 (Func, minX, maxX, minY, maxY)
I = integral2 (Func, minX, maxX, minY, maxX, Name, Value)

Explanation:

I = integral2 (Func, minX, maxX, minY, maxY) will integrate the function ‘Func’ (here ‘Func’ is a function of 2 variables X and Y) over the region minX ≤ X ≤ maxX and minY ≤ Y ≤ maxY

I = integral2 (Func, minX, maxX, minY, maxX, Name, Value) can be used to pass more options to the integral2 function. These options are passed as paired arguments

Examples to Implement Matlab Double Integral

Let us now understand the code to calculate the double integral in MATLAB using ‘integral2 function’.

Example 1:

In this example, we will take a function of cos with 2 variables ‘x’ and ‘y’. We will follow the following 2 steps:

Step 1: Create a function of x and y

Step 2: Pass the function and required limits to the integral2 function

Code:

Func = @ (x,y) (x + cos (y) + 1)
[Creating the cos function in ‘x’ and ‘y’] I = integral2 (Func, 0, 1, 0, 2)
[Calling the integral2 function and passing the desired limits as 0 <= x <= 1; 0 <= y <= 2)] [Mathematically, the double integral of x + cos (y) + 1 is 3.9093]

Output:

Matlab Double Integral1

Explanation: As we can see in the output, we have obtained double integral of our input function as 3.9093, which is the same as expected by us.

Example 2:

In this example, we will take a function of sin with 2 variables ‘x’ and ‘y’. We will follow the following 2 steps:

Step 1: Create a sin function of x and y

Step 2: Pass the function and required limits to the integral2 function

Code:

Func = @(x,y) (sin(y) + x.^3 + 2)
[Creating the sin function in ‘x’ and ‘y’] I = integral2 (Func, 0, 1, 0, 2)
[Calling the integral function and passing the desired limits as 0 <= x <= 1; 0 <= y <= 2)] [Mathematically, the double integral of sin(y) + x.^3 + 2 is 5.9161]

Output:

Matlab Double Integral2

Explanation: As we can see in the output, we have obtained double integral of our input function as 5.9161, which is the same as expected by us.

Example 3:

In this example, we will take a polynomial function of x and y. We will follow the following 2 steps:

Step 1: Create a polynomial function of x and y

Step 2: Pass the function and required limits to the integral2 function

Code:

Func = @(x,y) (35*y.^3 - 15*x)
[Creating the polynomial function in ‘x’ and ‘y’] I = integral2 (Func, 0, 1, 0, 2)
[Calling the integral function and passing the desired limits as 0 <= x <= 1; 0 <= y <= 2)] [Mathematically, the double integral of 35*y.^3 - 15*x is 125]

Output:

Matlab Double Integral3

Explanation: As we can see in the output, we have obtained double integral of our input function as 125, which is the same as expected by us.

Example 4:

In this example, we will take a polynomial function of x and y and of degree 3. We will follow the following 2 steps:

Step 1: Create a polynomial function of x and y and degree 3

Step 2: Pass the function and required limits to the integral2 function

Code:

Func = @(x,y) ((20*y.^3) + 3*x.^2)
[Creating the polynomial function in ‘x’ and ‘y’] I = integral2 (Func, 0, 1, 0, 2)
[Calling the integral function and passing the desired limits as 0 <= x <= 1; 0 <= y <= 2)] [Mathematically, the double integral of (20*y.^3) + 3*x.^2 is 82]

Output:

Degree 3

Explanation: As we can see in the output, we have obtained double integral of our input function as 82, which is the same as expected by us.

Example 5:

In this example, we will take a polynomial function with the division. We will follow the following 2 steps:

Step 1: Create a polynomial function of x and y and with division

Step 2: Pass the function and required limits to the integral2 function

Code:

Func = @(x,y) 1./(sqrt(x.^3 + y.^2))
[Creating the polynomial function in ‘x’ and ‘y’ and division] I = integral2 (Func, 0, 1, 0, 2)
[Calling the integral function and passing the desired limits as 0 <= x <= 1; 0 <= y <= 2)] [Mathematically, the double integral of 1./(sqrt(x.^3 + y.^2))  is 2.9012]

Output:

polynomial function

Explanation: As we can see in the output, we have obtained double integral of our input function as 2.9012, which is the same as expected by us.

Leave a Reply

Your email address will not be published. Required fields are marked *