Matlab Marina Integration Primer
Matlab Marina Integration Primer
Engineering Studies
MATLAB Marina – Integration Primer
Prerequisites
The Integration Primer assumes knowledge of the MATLAB IDE, MATLAB help, arithmetic
operations, built in functions, scripts, variables, arrays, logic expressions, conditional structures,
iteration, functions, debugging, characters and strings, cell arrays, structures, file input and
output, 2D plotting, 3D plotting, interpolation, curve fitting, and numerical differentiation.
Material on these topics is covered in the MATLAB Marina Introduction to MATLAB module,
MATLAB Marina Variables module, MATLAB Marina Arrays module, MATLAB Marina Logic
Expressions module, MATLAB Marina Conditional Structures module, MATLAB Marina Iteration
module, MATLAB Marina Functions module, MATLAB Marina debugging module, MATLAB
Marina Character and Strings module, MATLAB Marina Cell Arrays module, MATLAB Marina
Structures module, MATLAB Marina File Input and Output module, MATLAB Marina Plotting
module, MATLAB Marina Interpolation Module, MATLAB Marina Curve Fitting Module, and
MATLAB Marina Differentiation Module.
Learning Objectives
1. Be able to use MATLAB to compute numerical integrals.
2. Understand the limitations of numerical integration.
Terms
numerical integration, complete (definite) integral, continuous integral, noisy data
Numerical Integration
b
The definite integral of a function f ( x ) can be interpreted as an area R = ∫ f ( x ) dx and the
a
1
The Trapezoidal rule and Simpson's rule are two common methods of performing numerical
integration for definite integrals. For the Trapezoidal rule, the area between the function and
the x-axis is approximated by a trapezoid.
b
f ( a ) + f (b)
∫a f ( x ) dx ≈ ( b − a )
2
For numerical integration using the composite Trapezoidal rule, the interval of integration is
split into N small uniform subintervals, each approximated by a trapezoid, and the area of all
the trapezoids is summed.
b−a
b
∫a f ( x ) dx ≈ 2 N ( f ( x0 ) + 2 f ( x1 ) + 2 f ( x2 ) + + 2 f ( xN −1 ) + f ( xN ) )
Where the xk are the end points of the trapezoids
b−a
xk= a + k , k = 0,1, , N
N
For numerical integration using Simpson's rule, the interval is approximated by a quadratic
function, and the integral is approximated by the area between the quadratic functions and the
x-axis.
b
(b − a ) a+b
∫a f ( x ) dx ≈ 6 f ( a ) + 4 f 2 + f ( b )
Breaking the interval up into 2N subintervals and approximating each interval by a quadratic
function, and summing the areas between the quadratic functions and the x-axis, the integral is
approximated by the composite Simpson's rule
b
∫a f ( x ) dx ≈ 3 ( f ( x0 ) + 4 f ( x1 ) + 2 f ( x2 ) + 4 f ( x3 ) + 2 f ( x2 N −2 ) + 4 f ( x2 N −1 ) + f ( x2 N ) )
h
∫ f (t ) dt
−∞
2
with respect to x. The vectors x and y must be the same length and the interval for the
approximate definite integral is specified via the x vector values.
The MATLAB functions cumtrapz and cumsum are used to approximate the anti-derivative of
a function (cumulative numerical integral). The cumtrapz function takes two arguments x and
y and returns the cumulative integral of y with respect to x using the trapezoidal method. The
vectors x and y must be the same length, the vector x should have uniform spacing, and the
result is a vector the same size as x and y. If the spacing in x is one (distance between x values is
one), then cumtrapz can be used with a single argument y.
∫ (x )
+ 2 x + 1 dx and cumulative integral (approximate anti-derivative) of ∫ (u )
+ 2u + 1 du .
2 2
−5 −5
figure(1)
plot(x,f,'b-',x,cumintf,'g-')
xlabel('x'), legend('f(x)','approximate anti-derivative of f(x)')
If the spacing in x is not uniform, then cumtrapz(y) can be used to compute the cumulative
integral with unit spacing. This result must then be element by element multiplied with the
spacings. The cumsum function can be used similar to cumtrapz to perform cumulative
numerical integration except it only takes one argument and must either be multiplied by the
spacing increment or element by element multiplied with the spacings for non-uniform spacing.
Remember that the MATLAB function trapz is for computing approximate definite integrals
and the result will be a scalar and that cumtrapz is for computing cumulative (indefinite)
integrals and the result will be a vector.
3
100
f(x)
90 approximate anti-derivative of f(x)
Continuous
80 Integration
70
60
50
40
30
20
10
0
-5 -4 -3 -2 -1 0 1 2 3 4 5
x
Integration tends to reduce the effect of error or noise whereas differentiation tends to
magnify the effect of error or noise. Integration is similar to averaging and tends to reduce the
effects of things that are small but vary rapidly. Differentiation which gives the rate of change
of something, magnifies things that vary rapidly even if the magnitude of the variation is small.
The derivative of the error or noise in data may be larger in magnitude than the derivative of
the underlying function of the data.
Figures 2a shows a MATLAB program that generates a noisy signal and determines the
approximate integral and approximate derivative. The underlying function is a unit amplitude
cosine of 250Hz. The additive noise is a cosine of 60Hz with amplitude uniformly distributed
over the range -0.05 to 0.05.
Figures 2b -2d show plots of the noisy data, the approximate integral of the noisy data, and the
approximate derivative of the noisy data. Notice in the plot of Figure 2b, that the noise in the
data is barely discernible except at a few of the peaks. The signal looks like a unit amplitude
cosine of 250Hz (period 4msec).
4
% noisy data, 250Hz cosine with additive noise
% modeled as a 60Hz cosine with varying amplitude
t = 0:1/250/20:0.02;
% unit amplitude cosine of 250Hz
g = 1.0*cos(2*pi*250*t);
% noise, random amplitude 60Hz cosine
noise = (-0.05 + (0.05 - -
0.05)*rand(1,length(t))).*cos(120*pi*t);
% data plus noise
gNoisy = g + noise;
Noisy Data
1.5
0.5
g(t)
-0.5
-1
-1.5
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018 0.02
t (sec)
The integral of the noisy data, shown in Figure 2c, is a sin of approximately 250Hz. The
amplitude is much smaller than one might expect, but recall that ∫ cos = (ωt )dt ω1 sin (ωt ) + c , so
the amplitude the sine wave should be scaled by 1
ω times the amplitude of the noisy signal.
5
-4
x 10 Numerical Integral of Noisy Data
8
2
integral g(t)
-2
-4
-6
-8
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018 0.02
t (sec)
1500
1000
500
d g(t)/dt
-500
-1000
-1500
-2000
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018 0.02
t (sec)
6
noise = (-0.05 + (0.05 - -0.05)*rand(1,length(t)));
The effect of the noise is barely noticeable on the approximate integral but more pronounced
on the approximate derivative.
-4
x 10 Numerical Integral of Noisy Data
8
2
integral g(t)
-2
-4
-6
-8
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018 0.02
t (sec)
1500
1000
500
d g(t)/dt
-500
-1000
-1500
-2000
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018 0.02
t (sec)
This work by Thomas Murphy is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0
Unported License.