Updated July 3, 2023

## Introduction to Matlab Integral

Matlab Integral is useful in finding areas under the curves. It is the reverse of differentiation in calculus; 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 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:**

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

#### 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:**

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

#### 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 that combines 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:**

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

#### 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:**

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

### Conclusion

‘integral function’ can be used in MATLAB to calculate the integration of any function. We can set the desired limits on the integration using the arguments of the integral function. Applying specific conditions using ‘name-value pair’ arguments is also possible.

### Recommended Articles

This is a guide to Matlab Integral. Here we discuss an introduction to Matlab Integral, syntax with explanation, examples with code, and output. You can also go through our other related articles to learn more –