Skip to content

MATLAB - Integration

MATLAB - Symbolic and Numerical Integration

Section titled “MATLAB - Symbolic and Numerical Integration”

Integration is a core concept in calculus with two main applications: finding a function from its derivative (indefinite integration or anti-differentiation) and calculating the total accumulation of a quantity, such as the area under a curve (definite integration). MATLAB provides a powerful and distinct tool for each of these scenarios.

  • Symbolic Integration: Used to find an exact, analytical solution to an integral. It treats variables as abstract symbols, not numerical values. This is handled by the int function from the Symbolic Math Toolbox.
  • Numerical Integration: Used to find a numerical approximation of a definite integral. This is necessary when an exact solution is impossible to find or not required. The modern function for this is integral.

Symbolic Integration (Indefinite and Definite)

Section titled “Symbolic Integration (Indefinite and Definite)”

When you need an exact mathematical expression as the result, symbolic integration is the right choice. It requires the Symbolic Math Toolbox.

% Declare symbolic variables
syms x t n
% Indefinite Integral
F = int(f, x) % Integrates function f with respect to variable x
% Definite Integral
A = int(f, x, a, b) % Integrates f with respect to x from a to b

Let’s find the indefinite integral of f(x) = ax^2 + sin(x).

syms x a
% Define the function
f = a*x^2 + sin(x);
% Calculate the indefinite integral
F = int(f, x)
% Note: MATLAB's symbolic integration does not add the constant of integration '+ C'.

MATLAB returns the exact analytical result:

F =
(a*x^3)/3 - cos(x)

Let’s calculate the exact area under the curve y = x^3 from x=0 to x=2.

syms x
% Calculate the definite integral
area = int(x^3, x, 0, 2)
% To get a numeric value from the symbolic answer, use double()
double_area = double(area);

The result is:

area =
4

Numerical Integration (Definite Integrals)

Section titled “Numerical Integration (Definite Integrals)”

When dealing with complex functions that have no simple antiderivative, or when you are working with numerical data instead of a symbolic expression, you must use numerical integration. The modern, recommended function for this is integral.

Deprecation Notice: Older functions like quad, quadl, and quadgk are no longer recommended. The integral function is more robust, often faster, and should be used in all new code.

% Define the function to be integrated using a function handle
fun = @(x) ... your_expression_in_x ...;
% Calculate the integral
Q = integral(fun, xmin, xmax)

Best Practice: Using anonymous functions (@(x) ...) is the standard and most efficient way to define the integrand for integral. It avoids the overhead of symbolic variables and is highly readable.

Let’s find the area under the Gaussian curve f(x) = exp(-x^2) from -Inf to Inf. This is a classic integral whose analytical value is sqrt(pi).

% 1. Define the integrand as an anonymous function
gaussian_fun = @(x) exp(-x.^2);
% 2. Specify the integration limits
xmin = -Inf;
xmax = Inf;
% 3. Calculate the numerical integral
area = integral(gaussian_fun, xmin, xmax)
% Let's verify with the known analytical solution
analytical_solution = sqrt(pi);
fprintf('Numerical Result: %f\n', area);
fprintf('Analytical Result: %f\n', analytical_solution);

The output shows a highly accurate numerical approximation:

area =
1.7725
Numerical Result: 1.772454
Analytical Result: 1.772454

It’s often helpful to visualize the function you are integrating. The modern fplot function is perfect for plotting expressions or function handles.

Deprecation Notice: The ezplot function is not recommended for new code. Use fplot for its superior control and performance.

Example 4: Integrating and Plotting x*sin(x)

Section titled “Example 4: Integrating and Plotting x*sin(x)”
% Define the function handle
f = @(x) x .* sin(x);
% Define integration limits
a = 0;
b = 2*pi;
% Calculate the integral
result = integral(f, a, b)
% Plot the function to visualize it
figure;
fplot(f, [a, b]);
hold on;
% Fill the area under the curve to represent the integral
x_fill = linspace(a, b, 200);
y_fill = f(x_fill);
fill(x_fill, y_fill, 'c', 'FaceAlpha', 0.3); % 'c' for cyan
title('Integral of f(x) = x*sin(x) from 0 to 2*pi');
xlabel('x');
ylabel('f(x)');
grid on;
legend('f(x)', 'Integrated Area');

This code not only calculates the integral but also produces a clear, professional plot that visually represents the concept of integration.