Integral Calculus and MATLAB
Integral calculus is a fundamental concept in mathematics used to calculate areas under curves, accumulate quantities, and solve differential equations. MATLAB provides robust tools to handle definite and indefinite integrals symbolically and numerically.
Basic Concepts
Integration can be performed either symbolically (using the int
function) or numerically (using the integral
function).
Symbolic Integration
Symbolic integration requires the syms
command to define symbolic variables.
\[ \int x^2 \, dx \]
% Define a symbolic variable
syms x
% Indefinite integral of x^2
F = int(x^2);
disp(F); % Output: x^3/3
% Definite integral of x^2 from 0 to 2
F_def = int(x^2, 0, 2);
disp(F_def); % Output: 8/3
Output for indefinite integral: x^3/3 Output for definite integral: 8/3
Numerical Integration
Numerical integration is performed using integral
, which evaluates definite integrals for functions defined as MATLAB expressions or anonymous functions.
\[ \int_0^2 x^2 \, dx \]
% Define an anonymous function
f = @(x) x.^2;
% Numerical integration of x^2 from 0 to 2
F_num = integral(f, 0, 2);
disp(F_num); % Output: 2.6667
Output: 2.6667
Integration of Trigonometric Functions
MATLAB handles trigonometric functions efficiently using symbolic and numerical integration. Below is an example:
\[ \int \sin(x) \, dx \]
% Symbolic integration of sin(x)
syms x
F_trig = int(sin(x));
disp(F_trig); % Output: -cos(x)
% Definite integral of sin(x) from 0 to pi
F_trig_def = int(sin(x), 0, pi);
disp(F_trig_def); % Output: 2
Output for indefinite integral: -cos(x) Output for definite integral: 2
Integration of Exponential Functions
Below is an example of integrating an exponential function:
\[ \int_0^1 \exp(x) \, dx \]
% Symbolic integration of exp(x)
syms x
F_exp = int(exp(x));
disp(F_exp); % Output: exp(x)
% Definite integral of exp(x) from 0 to 1
F_exp_def = int(exp(x), 0, 1);
disp(F_exp_def); % Output: e - 1
Output for indefinite integral: exp(x) Output for definite integral: e - 1
Triple Integral over a Specified Region
We can evaluate triple integrals over more complex regions defined by constraints.
\[ \int_0^1 \int_0^z \int_0^y z^2 \, dx \, dy \, dz \]
% Symbolic triple integral over a specific region
syms x y z
% Compute the integral of z^2 over the region 0 ≤ z ≤ 1, 0 ≤ y ≤ z, 0 ≤ x ≤ y
F_region = int(int(int(z^2, x, 0, y), y, 0, z), z, 0, 1);
disp(F_region); % Output: 1/60
Output: 1/60
Triple Integral of a Cylindrical Region
Integration can also be performed in cylindrical or spherical coordinates. For example, to find the volume of a cylinder:
% Triple integral in cylindrical coordinates
syms r theta z
% Compute the volume of a cylinder with radius 2 and height 3
F_cylinder = int(int(int(r, z, 0, 3), r, 0, 2), theta, 0, 2*pi);
disp(F_cylinder); % Output: 12*pi
Output: 12*pi
Integration of Piecewise-Defined Functions
Piecewise-defined functions can be integrated in MATLAB by defining them using the piecewise
function. The integration can then be performed symbolically or numerically.
Example: Integration of a Piecewise Function
Consider the piecewise function:
- f(x) = x2, for 0 ≤ x < 1
- f(x) = 2x, for 1 ≤ x ≤ 2
We will compute the definite integral of this function over the interval [0, 2].
% Define the piecewise function
syms x
f = piecewise(0 <= x & x < 1, x^2, 1 <= x & x <= 2, 2*x);
disp(f); % Display the piecewise function
% Compute the definite integral of the piecewise function over [0, 2]
I = int(f, x, 0, 2);
disp(I); % Output the result
Piecewise Function: ⎧ x² 0 ≤ x < 1 ⎨ ⎩ 2x 1 ≤ x ≤ 2 Output: 11/6
Integration in Fourier Transforms
Fourier transforms often involve integrating exponential functions combined with other functions. MATLAB provides symbolic tools to perform these integrations analytically.
Example: Fourier Transform of a Rectangular Pulse
Consider the rectangular pulse defined as:
- f(t) = 1, for -1 ≤ t ≤ 1
- f(t) = 0, otherwise
We compute the Fourier transform of f(t) using the definition:
F(ω) = ∫-∞∞ f(t) e-jωt dt
\[ \int_{-\infty}^\infty f(t) \cdot e^{-i w t} \, dt \]
% Define the symbolic variables
syms t w
% Define the rectangular pulse function
f = piecewise(-1 <= t & t <= 1, 1, 0);
disp(f); % Display the piecewise function
% Define the Fourier transform integral
F_w = int(f * exp(-1i * w * t), t, -inf, inf);
disp(F_w); % Output the Fourier transform
Piecewise Function: ⎧ 1 -1 ≤ t ≤ 1 ⎨ ⎩ 0 otherwise Fourier Transform Result: 2⋅sin(w)/w
Explanation:
- The Fourier transform integral is evaluated over the region where f(t) ≠ 0, i.e., [-1, 1]:
- F(ω) = ∫-11 e-jωt dt
- Solution involves the sine integral formula, leading to: 2⋅sin(w)/w.
Plotting the Magnitude of the Fourier Transform
We can visualize the magnitude of the Fourier transform to understand the frequency-domain representation:
% Define a numeric version of the transform result
w_vals = linspace(-10, 10, 1000);
F_numeric = 2 * sin(w_vals) ./ w_vals;
F_numeric(w_vals == 0) = 2; % Handle the singularity at w = 0
% Plot the magnitude of F(w)
plot(w_vals, abs(F_numeric));
title('Magnitude of Fourier Transform');
xlabel('Frequency (\omega)');
ylabel('|F(\omega)|');
grid on;
(A plot showing the magnitude of the Fourier Transform will be displayed.)
Integration in Laplace Transforms
Laplace transforms involve integrating functions multiplied by an exponential decay term. MATLAB provides symbolic tools to perform these integrations analytically.
Example: Laplace Transform of an Exponential Function
Consider the function:
- f(t) = e-at, for t ≥ 0
The Laplace transform of f(t) is defined as:
F(s) = ∫0∞ e-at e-st dt
\[ \int_{0}^\infty e^{-a t} \cdot e^{-s t} \, dt \]
% Define the symbolic variables
syms t s a
% Define the function f(t)
f = exp(-a * t);
disp(f); % Display the function
% Define the Laplace transform integral
F_s = int(f * exp(-s * t), t, 0, inf);
disp(F_s); % Output the Laplace transform
Function: e^(-a⋅t) Laplace Transform Result: 1/(s + a), Re(s) > -Re(a)
Explanation:
- The integral is solved by combining the exponential terms and integrating:
- F(s) = ∫0∞ e-(s+a)t dt
- The integral evaluates to 1/(s+a), valid for Re(s) > -Re(a).
Plotting the Result for Specific Parameters
We can plot the Laplace transform result for a specific value of a
:
% Define numeric values for a and s
a = 2;
s_vals = linspace(-5, 5, 1000);
F_numeric = 1 ./ (s_vals + a);
% Plot the Laplace transform result
plot(s_vals, abs(F_numeric));
title('Laplace Transform |F(s)| for f(t) = e^{-2t}');
xlabel('s');
ylabel('|F(s)|');
grid on;
(A plot showing the magnitude of the Laplace Transform will be displayed.)
Useful MATLAB Functions for Integration
int(expr)
performs symbolic integration of expr
.integral(fun, a, b)
numerically evaluates the integral of fun
from a
to b
.syms var
declares var
as a symbolic variable.quad(fun, a, b)
numerically integrates fun
using adaptive quadrature.Practice Questions
Test Yourself
1. Compute the indefinite integral of e^(-x^2)
symbolically.
2. Evaluate the definite integral of sin(x)
from 0 to pi/2 using both symbolic and numerical methods. Compare the results.
3. Integrate the function x^3 - 2x^2 + x
numerically from -1 to 1.
4. Solve a real-world problem: Find the area under the curve y = x^2 + 3x + 2
from x = -2 to x = 2.