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
intfunction 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.
Syntax for int
Section titled “Syntax for int”% Declare symbolic variablessyms x t n
% Indefinite IntegralF = int(f, x) % Integrates function f with respect to variable x
% Definite IntegralA = int(f, x, a, b) % Integrates f with respect to x from a to bExample 1: Finding an Indefinite Integral
Section titled “Example 1: Finding an Indefinite Integral”Let’s find the indefinite integral of f(x) = ax^2 + sin(x).
syms x a
% Define the functionf = a*x^2 + sin(x);
% Calculate the indefinite integralF = 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)Example 2: Finding a Definite Integral
Section titled “Example 2: Finding a Definite Integral”Let’s calculate the exact area under the curve y = x^3 from x=0 to x=2.
syms x
% Calculate the definite integralarea = 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 =
4Numerical 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.
Syntax for integral
Section titled “Syntax for integral”% Define the function to be integrated using a function handlefun = @(x) ... your_expression_in_x ...;
% Calculate the integralQ = 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.
Example 3: Area of a Gaussian Function
Section titled “Example 3: Area of a Gaussian Function”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 functiongaussian_fun = @(x) exp(-x.^2);
% 2. Specify the integration limitsxmin = -Inf;xmax = Inf;
% 3. Calculate the numerical integralarea = integral(gaussian_fun, xmin, xmax)
% Let's verify with the known analytical solutionanalytical_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.772454Analytical Result: 1.772454Visualizing the Area of Integration
Section titled “Visualizing the Area of Integration”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 handlef = @(x) x .* sin(x);
% Define integration limitsa = 0;b = 2*pi;
% Calculate the integralresult = integral(f, a, b)
% Plot the function to visualize itfigure;fplot(f, [a, b]);hold on;
% Fill the area under the curve to represent the integralx_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.