Integrals enable quantitative analysis across science and engineering by determining the area under a curve or finding an anti-derivative. While simple analytical expressions can be integrated symbolically, MATLAB provides powerful numerical methods for tackling more complex functions. This definitive guide explores techniques for evaluating both definite and indefinite integrals.
Core Integration Functions in MATLAB
MATLAB offers a variety of functions for symbolic and numeric integration:
Symbolic:
int(): Integrates expressions with symbolic mathassume(),unassume(): Set assumptions for improving symbolic evaluation
Numeric:
quad(): Adaptive Simpson‘s rule for numerical integrationquadl(): Higher precision with error estimationsdblquad(),triplequad(): Double and triple integralsintegral(): Calculate integrals of vector-valued functions
This arsenal of tools enables flexible integration of problems ranging from straightforward polynomials to multivariate functions with singularities. We will explore both symbolic and numeric techniques.
Using int() for Symbolic Integration
The int() function leverages symbolic math for indefinite and definite integration of analytic expressions without numerical approximation.
Indefinite Integrals
To find an indefinite integral, omit the bounds:
syms x;
f = sqrt(x);
F = int(f,x)
F =
(2/3)*x^(3/2)
This returns the anti-derivative $\int \sqrt{x}\,dx = \frac{2}{3}x^{3/2} + C$.
Non-elementary functions can also be integrated:
f = sin(x) / x;
F = int(f,x)
F =
Si(x)
Returning the sine integral $Si(x) = \int \frac{\sin{x}}{x} dx$
Definite Integrals
To evaluate definite integrals, specify lower and upper bounds after the integration variable:
syms x;
f = x / (1+x^2);
a = 0;
b = 1;
I = int(f,x,a,b)
I =
log(2)
Computing the definite integral $\int_0^1 \frac{x}{1+x^2}dx = \log 2$.
Piecewise functions can be integrated by defining separate cases:
f = piecewise(x < 0, x, x >= 0, x^2);
F = int(f,x)
F =
piecewise(x < 0, 0.5*x^2, x >= 0, 1/3*x^3)
Assumptions for Faster Symbolic Integration
By default int() attempts complete generality, but performance can be improved by setting assumptions to limit the domain with assume() and unassume():
assume(x,‘positive‘);
f = log(x);
F = int(f,x)
x*log(x) - x
unassume(x)
With x constrained to positive, integration is faster.
Numeric Integration with quad() and quadl()
For numerical integration of functions without symbolic equivalents, MATLAB provides adaptive Simpson quadrature methods.
quad() for Numeric Definite Integrals
The quad() function accepts an anonymous function handle, integrating between specified bounds:
f = @(x) exp(-x.^2);
a = 0;
b = 1;
q = quad(f,a,b)
q =
0.7468
Computing $\int_0^1 e^{-x^2}dx \approx 0.7468$ numerically.
quad() recursively samples more points where the function is changing most rapidly, balancing accuracy and speed.
quadl() for Higher Precision
For more precision at additional computational expense, quadl() has improved error estimations:
f = @(x) log(1 + sqrt(x))./sqrt(x);
a = 0;
b = 1;
q = quadl(f,a,b)
q =
-4.0000
Accurately integrating the log-geometric mean despite the singularity.
Multidimensional Numeric Integration
To numerically integrate over two independent variables x and y:
f = @(x,y) x.*sin(y);
x1 = 0; x2 = 1;
y1 = 0; y2 = pi;
q = dblquad(f,x1,x2,y1,y2)
q =
2
The dblquad() approaches quickly handle higher dimensionality.
Comparison to Other Tools
How does MATLAB integration compare to Mathematica and NumPy?
| Functionality | MATLAB | Mathematica | NumPy |
|---|---|---|---|
| Symbolic integration | int() |
Integrate[] | – |
| Numeric integration | quad(), integral() |
NIntegrate[] | numpy.trapz() |
| Adaptive sampling | Yes | Some functions | – |
| Multidimensional | dblquad(), triplequad() |
IteratedIntegral[] |
scipy.nquad() |
| Handles singularities | Yes | Yes | No |
| Symbolic assumptions | assume(), unassume() |
Assuming | – |
MATLAB provides the easiest access to both symbolic and numeric integration with smooth handling of singularities.
Applications of Integration
Beyond basic area computations, integrals enable:
- Volume calculations using triple integrals
- Estimating centroids and moments
- Modeling growth rates in biology
- Frequency domain analysis with Fourier & Laplace transforms
- Determining arc length, surface area, work, fluid flow
- Solving differential equations numerically
- Modal and harmonic analysis in engineering
Let‘s look at some applied examples.
Calculating Volumes
We can calculate the volume under a surface by integrating the height at each (x,y) coordinate:
f = @(x,y) x.^2 + y.^2;
x1 = 0; x2 = 1;
y1 = 0; y2 = 2;
V = dblquad(f,x1,x2,y1,y2)
V =
2.6667
Integrating to compute the volume under the paraboloid $z = x^2 + y^2$ between the planes.
Centroids and Moments with integral()
The integral() function can integrate vector-valued functions to find centroids and moments. Given a density function $\rho(x, y)$:
rho = @(x,y) sin(x).*cos(y);
cx = integral2(@(x,y) x.*rho(x,y),0,2*pi,0,2*pi) / integral2(rho,0,2*pi,0,2*pi)
cy = integral2(@(x,y) y.*rho(x,y),0,2*pi,0,2*pi) / integral2(rho,0,2*pi,0,2*pi)
This finds the centroid at $(cx, cy) = (0,0)$, while higher moments describe the spreading.
Modal Vibration Analysis
Integrating the product of mass and stiffness matrices is useful for finding modal vibration frequencies in mechanical engineering:
M = [1 0; 0 2]; % Mass matrix
K = [8 -2; -2 4]; % Stiffness matrix
W = sqrt(eig(K,M)); % Natural frequencies
f = W/(2*pi); % Convert to Hz
This provides a faster approach than finite element analysis for simple systems.
Best Practices for Tricky Integrals
Certain integrals involve tradeoffs between accuracy, speed, and complexity:
- Watch for slow convergence with steep integrands
- Singularities require analytic transforms or small perturbations
- Use
assume()to guide symbolic processing for multi-valued functions - Add
Analyticassumptions to integrate branch cuts continuously - Oscillatory integrals benefit from higher order quadrature rules
- Principal value integrals handle removable discontinuities
Getting optimal performance requires assessing convergence and numerics.
Leveraging MATLAB Toolboxes
Dedicated toolboxes provide integration tailored for specialized disciplines:
| Toolbox | Details |
|---|---|
| Statistics | Distribution functions (cdfs, pdfs) |
| Symbolic Math | Theoretical manipulation and simplification |
| Signal Processing | Filter design integrals, spectral analysis |
| Econometrics | Finance integration like stochastic differential equations, Brownian processes |
| Global Optimization | Integrating objective functions and constraints |
The breadth highlights the centrality of integration across quantitative fields.
Handling Improper Integrals
With infinite limits of integration, ensuring convergence is paramount:
f = @(x) exp(-x)./x;
a = 0; b = Inf;
assumeAlso(x>0); % Add assumption for convergence
q = int(f,x,a,b)
I = 1 % Exponential integral
Here assumptions guide the improper integration.
Relationship to Differentiation
Through the fundamental theorem of calculus, differentiation and integration are almost inverses:
$\frac{d}{dx} \int_a^x f(t) dt = f(x)$
In MATLAB, functions can transform between derivatives and integrals:
f = @(x) x.^3;
dF = matlabFunction(int(f)) % Integral
ddF = matlabFunction(diff(dF)) % Derivative
This allows translating between dynamic models and their static equivalents.
Contrasting Numeric and Symbolic Methods
Computational tradeoffs between symbolic and numeric integration include:
| Attribute | Symbolic | Numeric |
|---|---|---|
| Exactness | Analytic integrals, full generality | Approximations with error bounds |
| Speed | Can be slow with complexity | Efficient sampling strategies |
| Precision | Arbitrary precision available | Limited by tolerance |
| General functions | Broad symbolic knowledge | Requires vectorization |
| Outputs | Equations, parameterized results | Floating point numbers |
| Multidimensional | Triple+ integrals available | Easier higher dimensionality |
Integrate symbolically where possible, then utilize numeric methods for efficiency and scalability.
Accelerating Numeric Evaluation
For fastest numerical integration:
- Vectorize function handles with array operations
- Set suitable error tolerances to avoid oversampling
- Use parallel options or GPU code generation
- Differentiate functions without closed forms
Improved algorithms also include:
- Gaussian quadrature: Sample at optimal points
- Newton–Cotes: Uniformly spaced interpolation
- Monte Carlo: Random sampling for high dimensions
Tailor methods to the smoothness and domain of each problem.
Conclusion
MATLAB offers extensive well-optimized routines for evaluating both definite and indefinite integrals symbolically and numerically. Assumptions can accelerate symbolic analysis, while numeric quadrature adapts sampling density for precision without overcomputation. Alongside the core methods highlighted, domain-specific toolboxes provide further specialized integration. Mastering MATLAB‘s integration functions enables powerful quantitative analysis and modeling across the natural and engineering sciences.


