MATLAB - Double Integral
MATLAB - Numerical Integration
Section titled “MATLAB - Numerical Integration”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.
Single, Double, and Triple Integrals
Section titled “Single, Double, and Triple Integrals”- 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.
MATLAB’s Numerical Integration Suite
Section titled “MATLAB’s Numerical Integration Suite”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;.
Solving Double Integrals with integral2
Section titled “Solving Double Integrals with integral2”The integral2 function numerically evaluates the double integral of a function fun(x,y) over a planar region.
Syntax
Section titled “Syntax”% For rectangular domainsq = 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 optionsq = integral2(fun, xmin, xmax, ymin, ymax, Name, Value)Syntax Explanation
Section titled “Syntax Explanation”fun: A handle to the function to be integrated. It must be vectorized, meaning it can accept arrays forxandy.xmin,xmax: The integration limits forx, which must be finite scalars.ymin,ymax: The integration limits fory. These can be scalars (for rectangular regions) or function handles (for non-rectangular regions) that depend onx.
Example 1: Rectangular Domain
Section titled “Example 1: Rectangular Domain”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 handlefun = @(x, y) x.^2 + y.^2;
% 2. Define the integration limitsxmin = 0;xmax = 1;ymin = 0;ymax = 2;
% 3. Compute the double integralvolume = integral2(fun, xmin, xmax, ymin, ymax);
% 4. Display the resultfprintf('The calculated volume is: %f\n', volume);% Output: The calculated volume is: 3.333333Example 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 handlefun = @(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 integralresult = integral2(fun, xmin, xmax, ymin, ymax);
% 5. Display the resultfprintf('The integral over the triangle is: %f\n', result);% Output: The integral over the triangle is: 0.632121Best Practices and Advanced Usage
Section titled “Best Practices and Advanced Usage”Controlling Accuracy
Section titled “Controlling Accuracy”You can control the accuracy of the integration using Name,Value pairs:
'RelTol': Relative tolerance, default is1e-6.'AbsTol': Absolute tolerance, default is1e-10.
For example, to request higher accuracy:
q = integral2(fun, xmin, xmax, ymin, ymax, 'AbsTol', 1e-12, 'RelTol', 1e-9);Handling Singularities
Section titled “Handling Singularities”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.
Choosing the Integration Method
Section titled “Choosing the Integration Method”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.