Matlab Review
Matlab Review
MATLAB Review
J. M. Sebeson, DeVry University, 2009
This course makes use of MATLAB for calculations and the plotting of functions. MATLAB has
extensive capabilities, but we will focus on just a few of them. These notes will review the
following topics on the use of MATLAB:
1. Variables and operations: scalars, vectors, and matrices; uniformly spaced vector
sequences; element-by-element calculations.
2. Representation of complex numbers, magnitude and angle.
3. Common built-in functions; trig and exponential functions.
4. Entering and plotting functions and signals.
5. Polynomials and their roots.
6. MATLAB help. Tabbing for command names; lookfor command.
7. Symbolic Math toolbox; transforms and inverse transforms; ordinary differential
equations
Topic 1 Variables and operations
Numbers and variables in MATLAB can be represented as scalars, vectors (either row or column
strings) or matrices (row and column arrays). For example:
>> a=[2]
a=
2
% A scalar
Continuing with our example, row vectors can be turned into column vectors and vice versa with
the transpose character ().
>> b'
ans =
1
2
3
4
In the construction of signals, it is often necessary to create a vector of values over a certain
range in evenly spaced intervals. For example, suppose you wish to generate the function
s (t ) sin(2 5t )
over the interval of t = 0 to t = 1 second in intervals of 0.01 seconds. The commands would be:
>> t=0:.01:1;
>> s=sin(2*pi*5*t); % The number pi = 3.1416.. is just written pi in MATLAB
> length(s)
ans =
101
3
Notice the use of the colon (:) in the construction of the t vector. Both the t and the s vectors
would contain 101 elements.
Other useful commands for constructing vectors with evenly spaced values are linspace and
logspace. (See their help descriptions.)
When carrying out certain operations with vectors, such as addition, subtraction, and element-byelement multiplication or division, the length of the operand vectors must be the same (that is,
they must have the same number of elements). If you wish to carry out element-by-element
operations, you must use the dot-operation syntax. For example:
>> b=[1,2,3,4];
>> d=[5,6,7,8];
>> b.*d
ans =
5 12
>> b.^3
ans =
1
% b and d have the same length. The .* defines the operation of element-by-element
% multiplication of b and d
21
32
% note the use of .^ to define the operation of cubing each element of vector b
27
64
>> i*i
ans =
-1
>> j*j
ans =
-1
MATLAB always represents complex numbers in their rectangular form:
>> x=exp(-j*pi/4) % A complex number in polar form
x=
0.7071 - 0.7071i % Rectangular form
>> real(x)
ans =
0.7071
2.7183
> format long % This commands extends the number of decimal places displayed
>> exp(1)
ans =
2.71828182845905
Topic 4 Plotting graphs - continuous and discrete signals
MATLAB has very sophisticated graphics capabilities that can be explored in the help
documentation. However, the most basic commands for plotting are plot (connects the data
points with continuous lines) or stem (creates stem plots of discrete data).
Example 1: Plot the sum of two equal amplitude sinusoids of frequencies 50 Hz and 100 Hz
from 0 to 50 milliseconds. Use an increment of .01 ms in the plot. Note that the signal to be
plotted is
s (t ) sin(2 f1t ) sin(2 f 2t )
In the above equation, f1 = 50 Hz and f2 =100 Hz and t is in units of seconds. This can also be
written as:
t
t
s (t ) sin 2 sin 2
T1
T2
In this equation, T1 is the period of f1 and T2 is the period of f2. T and t can be in any units of
time as long as they are the same units (milliseconds in this example).
>> t=0:.01:50; % A time vector from 0 to 50 in increments of .01. This vector is in ms.
>> f1=50;
% Frequencies in Hz
>> f2=100;
>> T1=1000/f1; % Periods in milliseconds (there are 1000 milliseconds per second)
>> T2=1000/f2;
>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2); % Note that t and T would have the same units (ms)
>> plot(t,s)
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
10
15
20
25
30
35
40
45
50
Example 2: Using the same signal as above, plot the discrete-time signal (digital signal) that
would result from sampling s(t) at a frequency of 500 Hz (that is, a sample every 2 milliseconds).
For this example, we will generate a stem plot, because that is the standard way to represent
discrete-time signals and functions. We can use the same code as above except that we will
make the time vector run from 0 to 50 milliseconds with an increment of 2 ms and use stem in
place of plot.
>> t=0:2:50; % Now the t vector has an increment of 2 instead of .01
>> f1=50;
% Frequencies in Hz
>> f2=100;
>> T1=1000/f1; % Periods in milliseconds
>> T2=1000/f2;
>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);
>> stem(t,s)
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
10
15
20
25
30
35
40
45
50
To see that the above plot is in fact a sample of the continuous function, we can re-plot the
original graph on the stem plot by using the hold function.
>> f1=50;
>> f2=100;
>> T1=1000/f1;
>> T2=1000/f2;
>> t=0:2:50; % Time vector with a 2 ms interval
>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);
>> stem(t,s)
>> t=0:.01:50; % Time vector with a .01 ms interval
>> s=sin(2*pi*t/T1)+sin(2*pi*t/T2);
>> hold % hold the last plot
%Current plot held
>> plot(t,s,'k') % The signal s is plotted with a black line (k flag)
>> hold off % Release the plot so that it is not re-used by later plots
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
10
15
20
25
30
35
40
45
50
9
>> c=[1,2,3,4]
% The coefficients in descending powers of z
c=
1 2 3 4
>> roots(c)
% The roots command only needs the vector of coefficients.
ans =
-1.6506
-0.1747 + 1.5469i
-0.1747 - 1.5469i
Example 2. Find the roots of the polynomial z 8 1 0 . Notice that this polynomial is
equivalent to:
z 8 0 z 7 0 z 6 0 z 5 0 z 4 0 z 3 0 z 2 0 z1 1 0
We expect to find eight roots, all of which will have the magnitude of one.
>> c=[1,0,0,0,0,0,0,0,-1];
>> r=roots(c)
r=
-1.0000
-0.7071 + 0.7071i
% Notice that all complex roots come in complex conjugate pairs
-0.7071 - 0.7071i
0 + 1.0000i
0 - 1.0000i
1.0000
0.7071 + 0.7071i
0.7071 - 0.7071i
>> abs(r)
ans =
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
1.0000
A picture of the location of the roots in the complex plane can be generated by the command
zplane.
>> c=[1,0,0,0,0,0,0,0,-1];
>> zplane(c) % In this case zplane uses the vector of polynomial coefficients
10
1
0.8
0.6
Imaginary Part
0.4
0.2
8
0
-0.2
-0.4
-0.6
-0.8
-1
-1
-0.5
0
Real Part
0.5
In the above figure, the roots of z 8 1 0 are shown as small open circles. The dotted circle
defines the unit circle in the complex plane where the magnitude of complex numbers is equal
to one.
Topic 6 MATLAB help
MATLAB has extensive built-in help and examples. It is a good idea to always read the help file
for a command or an M-file (custom MATLAB program) to understand the syntax and options
for the command.
Finding the commands you want can sometimes be challenging. Several useful tips are the
following:
1. Try help elfun and help specfun for lists of the many mathematical functions in
MATLAB.
2. Try help general for general purpose commands.
3. Try help graph2d for 2 dimensional plotting commands
4. If you think the command name or M-file may start with a particular set of letters, try
help with your guess, followed by tab. This will list all the commands that start with
the letters you entered.
5. The command lookfor will find matches to a word or phase in the first line of the help
documentation for all commands.
11
Example: Try to find a command that will carry out numerical integration. Our first guess
would be help integ <tab>. Trying this, you will only find a match for integerMath. Next, try
the lookfor command:
>> lookfor integral % Search for the word integral in the first line of a help file.
ELLIPKE Complete elliptic integral.
EXPINT Exponential integral function.
DBLQUAD Numerically evaluate double integral.
QUAD Numerically evaluate integral, adaptive Simpson quadrature.
QUAD8 Numerically evaluate integral, higher order method.
QUADL Numerically evaluate integral, adaptive Lobatto quadrature.
TRIPLEQUAD Numerically evaluate triple integral.
COSINT Cosine integral function.
SININT Sine integral function.
COSINT Cosine integral function.
FOURIER Fourier integral transform.
IFOURIER Inverse Fourier integral transform.
SININT Sine integral function.
Notice that the lookfor command found the commands involving QUAD, which, it turns out,
are the commands for carrying out a numerical integration.
Topic 7 Symbolic Math Toolbox
When we discuss Laplace transforms, Z-transforms, and Fourier transforms, we will show how
MATLAB can help find transforms and inverse transforms using the Symbolic Math Toolbox.
This is a much more convenient method than the traditional table look-up of transforms. The
Symbolic Math Toolbox has other capabilities, but we will mainly restrict our attention to finding
transforms and their inverses. The commands for transform analysis are the following:
1.
2.
3.
4.
5.
6.
When using the Symbolic Math Toolbox, those characters that are to be used as symbols must be
declared as symbols by the syms command. A few examples will illustrate the usage.
Example: Find the Laplace transform of the function f (t ) sin(t ) . This is a sinusoid with a
frequency of radians/sec. Since MATLAB does not use Greek characters, we will use w
instead of .
12
>> syms t s w
% This declares the symbols
>> f=sin(w*t);
% The function for which we will find the Laplace transform
>> F=laplace(f) % The command to find the Laplace transform
F=
w/(s^2 + w^2)
This result is equivalent to:
F (s)
s 2
2
Reference to a table of Laplace transforms will verify that this is the correct answer.
Example: Find the inverse Laplace transform of
F (s)
1
s3