Definite Numerical Integration Using Quad in MATLAB


In this tutorial, we will learn how to calculate definite numerical integration using Quad function in MATLAB. But before that, let us first understand what is definite numerical integration.

What is Definite Numerical Integration?

In mathematics, a method of determining the approximate value of the definite integral of a function over a certain interval is termed as definite numerical integration. Where, the definite integral specifies the signed area under the curve of a function within the specified interval.

The definite numerical integration is beneficial in approximating the value of a definite integral of a function when it is not possible to find the integral of the function using a standard integration technique.

The definite numerical integration is performed by dividing the total specified interval into smaller subintervals. Then, the area under the curve is evaluated for each subinterval. Finally, all the areas under the curve determined for subintervals are summed up to obtain the total area under the curve.

Definite numerical integration is commonly used in the field of engineering, physics, mathematics, economics, computer science, and more to determine the cumulative effect of a continuous function over a certain range.

Calculate Definite Numerical Integration in MATLAB

MATLAB provides several built-in functions to compute the definite numerical integration of a function. But in this article, we will use the 'quad' function to calculate the definite numerical integration of a function using MATLAB.

In MATLAB, the 'quad' function can be used to perform definite numerical integration of a function which is defined for only single variable over a certain interval. To perform definite numerical integration, we use the following syntax of the 'quad' function −

I = quad(f, a, b);

Here, f is a single-variable function that is to be integrated, a and b are the lower limit and upper limits of the integration.

Now, let us perform definite numerical integration of a function for with different specified conditions.

(1). Perform Definite Numerical Integration over a Specified Interval

Syntax

The following syntax of the “quad” function is used to perform the definite numerical integration of a function over a specified interval −

I = quad(f, a, b);

Where, f is the function to be integrated, [a, b] is the specified interval over which the definite numerical integration is to be performed.

Consider the following MATLAB code to understand the use of this syntax to perform the definite numerical integration.

Matlab Example (1)

% Define a function to be integrated
f = @(x) sin(x);

% Set the upper and lower limits
a = 0;
b = pi;

% Calculate the definite integral
I = quad(f, a, b);

% Display the result
disp('Definite integral of sin (x) from 0 to pi:');
disp(I);

Output

Definite integral of sin (x) from 0 to pi:
2

Explanation

This MATLAB program computes and display the definite numerical integration of the function 'sin(x)'.

In this code, we started by defining a function '@(x) sin(x)' which is an anonymous function that calculates the sine of an input variable 'x'. This function is stored in a variable 'f'. Next, we specify the lower and upper limits 'a and b' of the integration, in this case [0, pi].

After that we use the 'quad' function to calculate the integral value of the function 'f' over the specified interval and the result is stored in a variable 'I'. Finally, we display the definite integral value by using the function 'disp'.

(2). Perform Definite Numerical Integration over Specified Interval with Specified Tolerance

Syntax

The following syntax of the 'quad' function is used to calculate the definite numerical integration of a given function over a specified interval with a specified tolerance −

I = quad(f, a, b, t);

Here, f is the function to be integrated, a and b are the lower and upper limits of the interval, and t is the specified tolerance that represent the error in the result.

Matlab Example (2)

The following MATLAB program demonstrates the code implementation of the 'quad' function to perform the definite numerical integration over a specified interval with a specified tolerance.

% MATLAB program to perform definite numerical integration with specified tolerance
% Define a function to be integrated
f = @(x) sin(x);

% Set the upper and lower limits
a = 0;
b = pi;

% Specify the tolerance for the error in the result
t = 1e-3;

% Calculate the definite integral
I = quad(f, a, b, t);

% Display the result
disp('Definite integral of sin (x) from 0 to pi with specified tolerance:');
disp(I);

Output

Definite integral of sin (x) from 0 to pi with specified tolerance:
2

Explanation

The code implementation and execution of this MATLAB program is similar to that of the previous one. This MATLAB code calculates and displays the definite numerical integration of the function 'sin(x)' over the specified interval [0, pi] with a specified tolerance '1e-3' for error in the result.

(3). Calculate Definite Numerical Integration with Tracing Enabled

The 'quad' function with the following syntax can be used to perform the definite numerical integration of a function over a specified interval with a specified tolerance with tracing enabled −

I = quad(f, a, b, t, trace);

Here, the 'trace' option takes a Boolean value either 'true' or 'false' to display the debugging information during integration calculation.

Matlab Example (3)

The following MATLAB program demonstrates the implementation of this syntax of the 'quad' function to perform the definite numerical integration with tracing enabled.

% MATLAB program to perform definite numerical integration with specified tolerance
% Define a function to be integrated
f = @(x) sin(x);

% Set the upper and lower limits
a = 0;
b = pi;

% Specify the tolerance for the error in the result
t = 1e-3;

% Enable the tracing option
trace = true;

% Calculate the definite integral
I = quad(f, a, b, t, trace);

% Display the result
disp('Definite integral of sin (x) from 0 to pi with tracing enabled:');
disp(I);

Output

Definite integral of sin (x) from 0 to pi with tracing enabled:
2.0000

Explanation

The code implementation and execution of this MATLAB program is similar to that of the previous codes. This MATLAB program calculates the definite numerical integral of the function 'f' with a specified tolerance and traces the debugging information displayed during the calculation.

Conclusion

This is all about calculating definite numerical integration in MATLAB using the 'quad' function. In this tutorial, we have explained the concept of definite numerical integration and different syntaxes of the 'quad' function. Also, we have included some example MATLAB programs to demonstrate how to calculate the definite numerical integration of a given function. You can try all these MATLAB codes with different mathematical functions.

Updated on: 06-Sep-2023

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements