Overview
Plotting and Integrating Signals in MATLAB
In this project, you will learn how to plot continuous-time signals in MATLAB and how to take
definite integrals of functions.
I- Plotting continuous-time functions in MATLAB
Since MATLAB is a computer program, it cannot handle continuous-time signals because it
cannot store infinite number of data points. To solve this problem, we should take finite
number of samples from the function. Obviously, the more samples we take from the function,
the more accurate our calculations will be. However, selecting a large number of samples could
result in a long computation time. Therefore, we should strike a balance between the number
of points and the accuracy of our calculations. For smooth and slow varying signals, we can
choose around 1000 points for the given time interval.
To plot a signal we need two vectors. The first vector is the time vector in which the time
instances of the data points are stored. At each of these time instances, the value of the
function is stored in the second vector. For example, the goal is to present the function
( ) sin(2 ) f t t ? = in MATLAB in the interval of 24t ?? . First, we should generate the time
vector “t” that is extended from 2 to 4. This can be easily done by the following commands in
MATLAB:
T = 4-2;
step = T/1000;
t = 2:step:4-step;
In this command, the interval [2,4) is divided into 1000 points and the vector t will contain
values from 2 to 4 at the step size of 1/500. To include the last value 4 into the vector (or define
the interval [2,4]) we can use the following command:
step = T/1000;
t = 2:step:4;
In this case, the number of entries of the vector t will be 1001. Now we can define the values
of the function f (t) simply by evaluating the function at the points given in the vector t:
f = sin(2*pi*t);
The vector pair t and f represent the function f (t) . We can plot this function using “plot”
command in MATLAB:
plot(t,f)
The result is illustrated in Figure-1.
Figure-1
In the above example, the function is defined by on expression. Sometimes the function
comprises several expressions. This kind of function is called piecewise. For example, the
following function is a piecewise function:
2
2 0
1 0 1
( )
1 1 2
0
t t
t t
g t
t t
elsewhere
? ? ? ? ?
? ?
?? + ? ? ?
=? ?
? ? ? ? ?
? ?
? ?
Note that ()gt has a discontinuity at 0 t = . To plot this function using MATLAB, we should
represent it in terms of unit step functions. We know that a pulse between 1t and 2 t can be
represented by two until step functions. For example, the following pulse can be represented
by
1 2 pt ,t (t) = u(t ?t1) ?u(t ?t2) .
Figure-2
It should be noted that
1 2 pt ,t (t1) =1 but
1 2 pt ,t (t2) = 0 . This feature is actually very useful, and it
allows us to define discontinuous piecewise functions like g(t) . Creating
1 2 pt ,t (t) in MATLAB
can be done by defining the time vector “t” over the overall time interval and creating a mask
over the [t1, t2] . Assume t1 = 3, t2 = 7 and the overall time interval is [?2,13] . We should first
define the overall time interval (The number of points in the overall interval is chosen to be
4000 for having enough number of points in each subinterval:
T = 13-(-2);
step = T/4000 ;
t = -2:step:13;
Now, we should define 3,7 ()pt:
P37 = (t>=3) & (t<7);
In the above MATLAB command, p37 has the same length as the vector t does and all of its
entries are zero except the ones corresponding to 3 t ? and 7 t ? , which are equal to 1.
Therefore, p37 acts as a mask. For each section of the function, a mask like that can be
generated and then multiplied by the expression of the function at the given section. Then all of
the generated vectors for all sections can be added to create the function over the entire
interval.
To use the aforementioned method on ()gt , we should first represent it in terms of
1 2 , ()t t pt
functions:
2
g(t) = (t) p?2,0 (t) + (?t +1) p0,1(t) + (1?t) p1,2 (t)
Now we can easily write the MATLAB code to create the time and magnitude vectors.
T = 2-(-2);
step = T/4000 ;
t = -2:step:2;
p1 = (t>=-2) & (t<0);
p2 = (t>=0) & (t<1);
p3 = (t>=1) & (t<=2);
g = p1.*t+p2.*(-t.^2+1)+p3.*(1-t);
plot(t,g)
The plot is depicted in Figure-3. It should be noted that the multiplications and the squaring
were done element-by-element because we are not doing matrix calculations. It should also be
noted that t = 2 is included in the MATLAB calculation but is not included in the formula
because p1,2 (t) does not include t = 2.
Figure-3
II- Integrating continuous-time functions in MATLAB
If the function ()ft is smooth, its definite integral over the interval [ , ] ab can be easily
approximated by the trapezoidal rule:
( ) ( ( 1) 2 ( 2) 2 ( 3) 2 ( ) ( 1))
2
b
N N
a
b a
f t dt f t f t f t f t f t
N
+
?
? = + + + +
where the interval [a,b] is divided into N equally spaced segments, i.e., there are N +1 grid
points. For more information, please see: http://en.wikipedia.org/wiki/Trapezoidal_rule
This is the most primitive way of calculating an integral. There are many other methods that provide
better and more accurate integration such as Simpson’s rule, Gaussian quadrature, Celshaw-Curtis and
Fejer rules.
The trapezoidal rule is provided in MATLAB as the “trapz” command. To use this command, we need
two vectors: the time vector t and the magnitude vector f. Finding these two vectors for a given
function is shown in the previous section. For example, we would like to find the following integral
3/2
1/2
? g(t)dt , where g(t) is defined in the previous section. The exact result of this integral can be found
by manual calculation:
3/2 1 3/2 1 3/2 3 2
2
1/2 1/2 1 1/2 1
( ) ( 1) (1 )
3 2
1 1/ 8 3 9 / 4 1 1
1 1/ 2 1
3 3 2 2 2 12
t t
g t dt t dt t dt t t
? ? ? ?
= ? + + ? = ?? + ? + ? ? ? ? ? ? ?
? ? ? ?
? ? ? ? ? ? ? ?
= ? ? + ? ? ? ? + ? + ? ? ? ? ? ? ? =
? ? ? ? ? ? ? ?
? ? ?
To use trapezoidal rule in MATLAB we need to create the time and magnitude vectors over the interval
determined by the limits of the integral. Using the method explained in the previous section we need to
evaluate ()ft over [1 / 2, 3 / 2] . This can be done by:
T1 = 3/2-(1/2);
step1 = T1/4000 ;
tt = 1/2:step1:3/2;
p11 = (tt>=1/2) & (tt<1);
p22 = (tt>=1) & (tt<=3/2);
gt = p11.*(-tt.^2+1)+p22.*(1-tt);
I1 = trapz(tt,gt)
The value of I would be 0.0833333, which is 1/12 as the manual calculation indicates. Another
way of doing this is to use the function generated by MATLAB in the previous section. Suppose
that g and t are available from the previous section.
t1 = t((t>=1/2 & t<=3/2));
g1 = g((t>=1/2 & t<=3/2));
I2 = trapz(t1,g1)
The expression in the parentheses is a Boolean mask that selects the values of t and g in the
interval of
1 3
,
2 2
t
? ?
?? ?
? ?
. There is a small difference between I1 and I2 which comes from the
fact the interval of integration is divided into 4000 segments for I1 but it has 1000 segments
for calculating I2. Nevertheless, this small difference will be negligible if we divide the interval
of integration into a large enough number of segments.
Questions
1. Plot the following function by using the method mentioned in the previous section I:
3
cos( / 2) 2 1
1 1 0
( ) 1 0 1
1 sin(2 ) 1 2
0
t t
t t
h t t
t t
elsewhere
?
?
? ? ? ? ?
?
+ ? ? ? ??
= ? ? ?
?
? + ? ?
??
2. Find the following integrals both manually and by MATLAB and compare the results:
a.
1
1
2
() I h t dt
?
= ?
b.
2
2
2
I h(t)dt
?
= ?
c.
1.5
3
0.5
I h(t)dt
?
= ?
d.
1
4
1
I t.h(t)dt
?
= ?