Skip to content

MATLAB - Double Integral

Integration is a fundamental concept in calculus used to find quantities such as areas, volumes, and total accumulations. While some functions can be integrated analytically, many real-world problems require numerical integration to approximate the solution.

MATLAB provides a suite of robust numerical integrators for single, double, and triple integrals, designed to handle a wide variety of functions and integration domains.

  • A single integral, $$\int_a^b f(x) , dx$$, computes the area under a curve in 2D.
  • A double integral, $$\iint_R f(x,y) , dA$$, computes the volume under a surface over a region R in 3D.
  • A triple integral, $$\iiint_V f(x,y,z) , dV$$, computes the hyperspace volume or accumulates a quantity over a 3D volume V.

The primary functions for numerical integration in modern MATLAB are:

  • integral(fun, xmin, xmax): For single integrals.
  • integral2(fun, xmin, xmax, ymin, ymax): For double integrals.
  • integral3(fun, xmin, xmax, ymin, ymax, zmin, zmax): For triple integrals.

For all these functions, fun is a function handle, typically defined using the @ syntax. For example, f = @(x,y) x.^2 + y.^2;.

The integral2 function numerically evaluates the double integral of a function fun(x,y) over a planar region.

% For rectangular domains
q = integral2(fun, xmin, xmax, ymin, ymax)
% For non-rectangular domains (where y-limits depend on x)
q = integral2(fun, xmin, xmax, ymin, ymax)
% With additional options
q = integral2(fun, xmin, xmax, ymin, ymax, Name, Value)
  • fun: A handle to the function to be integrated. It must be vectorized, meaning it can accept arrays for x and y.
  • xmin, xmax: The integration limits for x, which must be finite scalars.
  • ymin, ymax: The integration limits for y. These can be scalars (for rectangular regions) or function handles (for non-rectangular regions) that depend on x.

Calculate the volume under the surface $$f(x,y) = x^2 + y^2$$ over the rectangular region defined by $$0 \leq x \leq 1$$ and $$0 \leq y \leq 2$$.

% 1. Define the vectorized function handle
fun = @(x, y) x.^2 + y.^2;
% 2. Define the integration limits
xmin = 0;
xmax = 1;
ymin = 0;
ymax = 2;
% 3. Compute the double integral
volume = integral2(fun, xmin, xmax, ymin, ymax);
% 4. Display the result
fprintf('The calculated volume is: %f\n', volume);
% Output: The calculated volume is: 3.333333

Example 2: Non-Rectangular (Triangular) Domain

Section titled “Example 2: Non-Rectangular (Triangular) Domain”

Let’s integrate the function $$f(x,y) = e^{-x^2}y$$ over the triangular region bounded by the x-axis, the line $$y=2x$$, and the line $$x=1$$.

The integration limits are: $$0 \leq x \leq 1$$ and $$0 \leq y \leq 2x$$.

% 1. Define the vectorized function handle
fun = @(x, y) exp(-x.^2) .* y;
% 2. Define the x-limits (outer integral)
xmin = 0;
xmax = 1;
% 3. Define the y-limits as function handles of x (inner integral)
ymin = @(x) 0;
ymax = @(x) 2*x;
% 4. Compute the integral
result = integral2(fun, xmin, xmax, ymin, ymax);
% 5. Display the result
fprintf('The integral over the triangle is: %f\n', result);
% Output: The integral over the triangle is: 0.632121

You can control the accuracy of the integration using Name,Value pairs:

  • 'RelTol': Relative tolerance, default is 1e-6.
  • 'AbsTol': Absolute tolerance, default is 1e-10.

For example, to request higher accuracy:

q = integral2(fun, xmin, xmax, ymin, ymax, 'AbsTol', 1e-12, 'RelTol', 1e-9);

MATLAB’s integrators are adaptive and can often handle singularities at the boundary of the integration domain. However, for complex cases or singularities inside the domain, you may need to split the integral into multiple parts to avoid the problematic point.

The 'Method' option lets you choose the integration algorithm ('auto', 'tiled', 'iterated'). The default, 'auto', is usually the best choice, as it selects the optimal method based on the provided limits.