MATLAB - Double Integral



An integral in mathematics is a concept that represents the area under a curve. It is a fundamental concept in calculus and can be thought of as the accumulation of quantities. Integrals are used to find areas, volumes, central points, and many useful things.

There are two main types of integrals

Definite Integrals − Calculate the area under a curve between two specific points.

Indefinite Integrals − Represent a family of functions and include a constant of integration.

Definite Integral Example

If you have a function f(x), the definite integral from a to b is written as −

$$\mathrm{\displaystyle\int_{b}^{a} f(x) \: dx}$$

This gives the area under the curve of f(x) from x = a to x = b.

What is a Double Integral?

A double integral extends the concept of a single integral to functions of two variables. It is used to calculate the volume under a surface in three-dimensional space. The double integral of a function f(x,y) over a region R in the xy-plane is written as.

$$\mathrm{\iint_{R} \: f(x,y) \: dA}$$

where dA represents a tiny area element in the region R.

Double Integral Example:

If you have a function f(x,y) and you want to find the volume under the surface over a rectangular region R = [a,b] x [c,d], the double integral is.

$$\mathrm{\iint_{R} \: f(x,y) \: dA \: = \: \int_{a}^{b}\int_{c}^{d}f(x,y)dy \: dx}$$

MATLAB and Double Integrals

MATLAB is a powerful tool for numerical computations, including evaluating integrals and double integrals.

Single Integral in MATLAB

To compute a single integral in MATLAB, you use the integral function. For example, to find the integral of f(x) = x2 from 0 to 1 −

f = @(x) x.^2;
result = integral(f, 0, 1);
disp(result);

Double Integral in MATLAB

To compute a double integral in MATLAB, you use the integral2 function. Here's how you can calculate the double integral of f(x,y) = x2 + y2 over the region.

$$\mathrm{0 \: \leq \: x \: \leq \: 1 \: and \: 0 \: \leq \: y \: \leq \: 1}$$

f = @(x, y) x.^2 + y.^2;
result = integral2(f, 0, 1, 0, 1);
disp(result);

In this example

f is an anonymous function representing f(x,y) = x2 + y2.

integral2 computes the double integral over the specified limits for x and y.

Let us now understand integral2() method in more detail.

Matlab Double Integral using integral2() function

integral2() function numerically evaluate double integral.

Syntax

q = integral2(fun,xmin,xmax,ymin,ymax)
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value)

Syntax Explanation

q = integral2(fun,xmin,xmax,ymin,ymax): in this syntax

  • fun is the function you want to integrate.It is written in terms of x and y.
  • xmin and xmax are the limits for the x-variable.The function will be integrated from x=xmin to x=xmax.
  • ymin and ymax these are the limits for the y-variable.The integration will be done from y=ymin to y=ymax.These are constants or functions of x.

q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value): in this syntax

  • fun − The function to integrate.
  • xmin and xmax − The limits for the x-variable.
  • ymin and ymax − The limits for the y-variable.
  • "Name,Value" pairs allow you to customize how MATLAB performs the integration.

Common "Name,Value" Pairs

  • 'RelTol' − Sets the relative tolerance for accuracy.
  • 'AbsTol' − Sets the absolute tolerance for accuracy.
  • 'Method' − Specifies the integration method (e.g., 'auto', 'iterated', 'tiled').
  • 'Waypoints' − Specifies intermediate points that the integrator should include.

Using integral2() function to solve double integrals

Example 1

using integral2() to Integrate Triangular Region with Singularity at the Boundary.

We want to compute the integral of a function f(x,y) over a triangular region in the xy-plane. A singularity at the boundary means that the function might become infinite or undefined at the boundary of the region. This can make the integral more challenging to compute, but MATLAB's integral2 can handle such cases with proper limits and function definitions.

Defining the Triangular Region

Consider a triangular region with vertices at points (0,0),(1,0), and (0,1). The region can be described with the limits −

0   x 1

0  y  1x

Let's consider the function

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

This function has a singularity at the origin (0,0), where it becomes infinite.

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Compute the double integral over the triangular region
q = integral2(fun, xmin, xmax, ymin, ymax);

% Display the result
disp(q);

Explanation of Code

We define the function shown below using an anonymous function handle in MATLAB.

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

Set Integration Limits:For x, the limits are from 0 to 1.

For y, the lower limit is 0 and the upper limit is 1x. Note that ymin is set to 0 (a constant), while ymax is a function of x.

Compute the Integral: We use the integral2 function to compute the double integral. By ensuring ymin is a constant and ymax is a function of x, we meet the requirements of the integral2 function.

The result of the integral is displayed using disp(q).

When you execute the code in matlab the output we get is -

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Compute the double integral over the triangular region
q = integral2(fun, xmin, xmax, ymin, ymax);

% Display the result
disp(q);
   1.2465

Example 2

Using integral2() to evaluate the Double Integral of a Parameterized Function with Specific Method and Error Tolerance.

To compute the double integral of a parameterized function with additional options such as the method of integration and error tolerance, you can use the integral2 function with "Name,Value" pair arguments.

Example Function and Region

Let's consider the function over the triangular region with vertices at points (0,0), (1,0) and (0,1).

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

Here's how to set up and compute this integral using MATLAB with additional options for the method and error tolerance:

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Define additional options: Relative Tolerance, Absolute Tolerance, and Method
options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'};

% Compute the double integral over the triangular region with additional options
q = integral2(fun, xmin, xmax, ymin, ymax, options{:});

% Display the result
disp(q);

Explanation of Code

The function is defined using an anonymous function handle.

$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$

Set Integration Limits

For x, the limits are from 0 to 1.

For y, the lower limit is 0 (a constant), and the upper limit is 1x (a function of x).

The additional options we defined are −

'RelTol', 1e-6 specifies a relative tolerance of 10-6

'AbsTol', 1e-8 specifies an absolute tolerance of 10-8

'Method', 'auto' lets MATLAB choose the most appropriate method automatically. Other methods like 'iterated' or 'tiled' can also be specified if needed.

The integral2 function is called with the function, integration limits, and additional options specified in the options cell array. The options{:} syntax unpacks the cell array into a comma-separated list of arguments.

The result of the integral is displayed using disp(q).

When you execute the code in matlab command window the output we get is as follows :

% Define the function with a singularity at the boundary
fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2);

% Define the limits for x
xmin = 0;
xmax = 1;

% Define the limits for y as functions of x
ymin = 0;
ymax = @(x) 1 - x;

% Define additional options: Relative Tolerance, Absolute Tolerance, and Method
options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'};

% Compute the double integral over the triangular region with additional options
q = integral2(fun, xmin, xmax, ymin, ymax, options{:});

% Display the result
disp(q);
   1.2465
Advertisements