Introduction: The Calculus Computer
If you’ve made it this far in the MATLAB course, you already know that MATLAB is short for “MATrix LABoratory.” But I think a better acronym would be “Numerical Computing Powerhouse.” Because while MATLAB excels at matrix operations, its true power lies in solving the kinds of mathematical problems that don’t have clean analytic solutions.
Here’s the thing about calculus: it’s beautiful in theory, but messy in practice. In a calculus class, you learn to compute integrals like ∫x²dx = x³/3 + C. Elegant! But then you encounter a real-world integral like ∫e^(-x²)dx from 0 to 1, and suddenly there’s no elementary antiderivative. You can’t solve it with pen and paper. You need numerical methods.
Similarly, differentiation in calculus is all about limits and symbolic derivatives. But in the real world, you often have discrete data points (not continuous functions), and you need to estimate derivatives from noisy measurements. The theoretical derivative doesn’t help you when your data is a vector of numbers with measurement errors.
And then there are equations — oh, the equations! Linear equations are solvable with matrix methods (which we covered in Article 04). But nonlinear equations? Systems of nonlinear equations? Differential equations? These are the bread and butter of engineering analysis, and they almost never have clean analytic solutions.
In this article, we’re going to tackle all three: numerical integration (quadrature), numerical differentiation, and numerical equation solving. We’ll use MATLAB’s powerful built-in functions like integral, gradient, fzero, fsolve, and the ODE solvers (ode45, ode15s, etc.). But we won’t just learn the syntax — we’ll understand the mathematics behind these functions, the trade-offs they make, and when to use each one.
By the end of this article, you’ll be able to:
- Numerically integrate functions that don’t have analytic integrals
- Estimate derivatives from discrete, possibly noisy data
- Solve nonlinear equations and systems of equations
- Solve ordinary and partial differential equations
- Choose the right numerical method for your specific problem
- Understand the concept of stiffness in differential equations (and why it matters)
Let’s dive in.
Numerical Integration: The integral Function Family
Numerical integration (or quadrature, if you want to sound fancy at parties) is the process of approximating the definite integral of a function. MATLAB provides a family of functions for this, all built around the integral function.
Basic Usage of integral
The integral function is MATLAB’s workhorse for numerical integration. Its syntax is deceptively simple:
% Integrate sin(x) from 0 to pi
f = @(x) sin(x);
result = integral(f, 0, pi);
% result = 2.0 (exactly, in theory)
% Integrate a more challenging function
g = @(x) exp(-x.^2);
result2 = integral(g, 0, 1);
% result2 = 0.7468... (this is (erf(1)*sqrt(pi))/2)
That’s it? That’s all there is to numerical integration?
Well, yes and no. The integral function is smart — really smart. It uses adaptive quadrature (we’ll explain what that means in a moment) to automatically refine its estimate until it reaches a specified tolerance. You don’t need to choose step sizes or worry about convergence — integral handles all of that.
But with great power comes great responsibility. Understanding how integral works will help you use it effectively and avoid common pitfalls.
How integral Works: Adaptive Quadrature
The integral function uses a sophisticated algorithm called adaptive quadrature. Let me explain the “adaptive” part first.
Non-adaptive quadrature (like the trapezoidal rule or Simpson’s rule) divides the integration interval into a fixed number of subintervals, approximates the integral on each subinterval, and sums them up. The problem? You have to choose the number of subintervals. Too few, and your answer is inaccurate. Too many, and you’re wasting computation.
Adaptive quadrature is smarter. It starts with a coarse approximation, then adaptively refines subintervals where the function is “difficult” (has high curvature or rapid variations). Here’s the basic algorithm:
- Approximate the integral on the whole interval using a quadrature rule (e.g., Simpson’s rule)
- Divide the interval in half and approximate the integral on each half
- Compare the sum of the halves to the whole-interval estimate
- If they’re close enough (within tolerance), accept the estimate
- If not, recursively apply steps 1-4 to the subintervals that failed
This is why it’s called “adaptive” — the algorithm automatically places more evaluation points where the function is hard to integrate and fewer where it’s easy.
% Demonstrate adaptive integration
f = @(x) sin(10*x); % Oscillates rapidly
result = integral(f, 0, 2*pi);
% integral will automatically use more points where sin(10x) oscillates
% Compare with a non-adaptive method (for illustration)
% (This is NOT how you should normally do it — just for illustration)
n = 1000;
x = linspace(0, 2*pi, n);
y = f(x);
non_adaptive = trapz(x, y); % Trapezoidal rule
fprintf('Adaptive (integral): %.10f\n', result);
fprintf('Non-adaptive (trapz): %.10f\n', non_adaptive);
Controlling integral: Tolerances and Options
By default, integral tries to achieve a relative error of about 1e-6. But you can control this (and other aspects) using the integral function’s name-value pair arguments:
% Set absolute and relative tolerances
f = @(x) exp(-x.^2);
result = integral(f, 0, 1, ...
'AbsTol', 1e-12, ... % Absolute tolerance
'RelTol', 1e-10); % Relative tolerance
% For integrands with singularities or discontinuities
f_bad = @(x) 1./sqrt(x); % Singular at x=0
result_bad = integral(f_bad, 0, 1, ...
'Waypoints', [0.001, 0.01, 0.1, 0.5, 0.9, 0.99, 0.999], ...
'MaxFunctionEvaluations', 10000);
% For complex-valued integration
f_complex = @(z) exp(1i*z);
result_complex = integral(f_complex, 0, pi, 'ArrayValued', true);
Key parameters:
'AbsTol': Absolute error tolerance (default: 1e-10)'RelTol': Relative error tolerance (default: 1e-6)'Waypoints': Points where the integrand has singularities or discontinuities'MaxFunctionEvaluations': Maximum number of function evaluations'ArrayValued': Set totrueif the integrand returns an array (for vector-valued or complex-valued integration)
Integrating Multidimensional Functions: integral2 and integral3
What if you need to integrate a function of two or three variables? MATLAB provides integral2 and integral3:
% 2D integration: integrate f(x,y) = x^2 + y^2 over a rectangle
f2 = @(x, y) x.^2 + y.^2;
result2d = integral2(f2, 0, 1, 0, 1);
% Result: 2/3 (theoretical)
% 2D integration over a non-rectangular region
% Integrate over a circle of radius 1
f2_polar = @(r, theta) r.^3; % In polar coordinates, includes Jacobian
result2d_circle = integral2(@(r, theta) f2_polar(r, theta), ...
0, 1, ... % r from 0 to 1
0, 2*pi); % theta from 0 to 2*pi
% (This is a simplified example — proper polar integration needs care)
% 3D integration
f3 = @(x, y, z) x + y + z;
result3d = integral3(f3, 0, 1, 0, 1, 0, 1);
% Result: 3/2 (theoretical)
The syntax of integral2 and integral3 is similar to integral, but you specify the integration limits for each variable. For non-rectangular regions, you can specify the limits as functions of the other variables.
When integral Fails (and What to Do About It)
The integral function is robust, but it can fail (or give inaccurate results) in certain situations:
1. Strong singularities
% This has a non-integrable singularity at x=0
f_singular = @(x) 1./x;
% result = integral(f_singular, 0, 1); % WARNING: May give wrong answer!
% Fix: Use waypoints to handle the singularity carefully
result = integral(f_singular, 0, 1, 'Waypoints', [0.001, 0.01, 0.1, 0.5, 0.9]);
2. Highly oscillatory integrands
% Rapid oscillations can fool adaptive quadrature
f_osc = @(x) sin(1000*x);
result = integral(f_osc, 0, 2*pi);
% May require increasing MaxFunctionEvaluations
3. Improper integrals (infinite limits)
% integral supports infinite limits
f_gaussian = @(x) exp(-x.^2);
result = integral(f_gaussian, -Inf, Inf);
% Result: sqrt(pi) ≈ 1.77245
Rule of thumb: Always compare your numerical result to a known analytical result (if available) to check accuracy. If you can’t do that, try tightening the tolerances ('AbsTol' and 'RelTol') to see if the result changes.



