Software Toolkit: MATLAB
Software Toolkit: MATLAB
1) Introduction to MATLAB:
MATLAB is a high level language for technical computing, which is often used by
engineers to help them design systems or analyze a system’s behavior. Following is a
simple beginner guide for the MATLAB package.
Starting MATLAB
Basic operations
Exercise
Vectors
More advanced features
Exercise
Vector manipulation
Exercise Exercise
Plotting Reference page
Exercise
Transfer functions
Exercise
More advanced
features
2) Starting MATLAB:
To run MATLAB, move the cursor to MATLAB icon and double-click on the left-
hand mouse button.
The MATLAB desktop will open. This is an integrated development environment for
working with MATLAB suite of toolboxes and programs. There are three open
windows, which represent:
Command window
Launch pad and workspace
Command history and current directory.
Command window: We type all our commands in this window at the prompt (>>) and
press 'enter' key to see the results of our operations.
3) Basic Operations:
We can assign a numerical value (data) to a variable using the equal (=) sign.
For example,
>> a=2
a=
OR
>> b= -2.5
b=
-2.5000
4) Vectors:
Enter each element of the row vector (separated by a space) between square brackets,
and set it equal to a variable. For example, to create the row vector x, enter into
Matlab:
>> x=[1 2 3 4]
x=
1 2 3 4
y=
1
2
3
4
We can determine the size of the vectors x and y by using the size command.
>> size(x)
ans =
1 4
>> size(y)
ans =
4 1
If we want to create a vector with elements between 0 and 5 evenly spaced in increments
of 0.5, we can use:
>> t=0:0.5:5
t=
Columns 1 through 7
5) Vector Manipulation
Manipulating vectors is almost as easy as creating them. Try the following operations:
>> a=[1 2 3 4]
>> x=a+2 Result:
>> x=a-2 Result:
>> x=a*2 Result:
>> x=a/2 Result:
Let b= [1 2 3].
What happen if we try to add two variables (vectors: a+b) of different dimensions? What
is the error message?
(We note that in the example x=a+2, variable a is size 1 х 4 and 2 is size 1 х 1.
Technically they could not be added together, as the dimensions are not compatible.
However, MATLAB assumers, when you ass/ subtract/multiply/divide a variable by a
number, that all elements of the vector should be operated on. This is not true with two
‘variables’, as we see now.
x+y ? Yes/no
x*y ? Yes/ no
y*x? Yes/no
In this we note that we could not add x+ y since they do not have appropriate dimensions.
However, if we transpose x=x’ (where ‘ indicates the transpose), we can then evaluate:
x+y’
The size command could be used to verify the dimensions of the vectors.
6) Polynomials
We can enter the coefficients of a polynomial as a row vector. The coefficients should be
entered in descending order of the powers of x. For example:
p(x)=x2 + 10x + 3
Matlab can interpret a vector of length n+1 as the coefficients of an n th order polynomial.
Thus, if a polynomial is missing any coefficients, we must enter zeros in the appropriate
place in the vector. For example
q(s) = 6s4 + 1
would be represented in MATLAB as:
q = [6 0 0 0 1]
If Z(s) = Q(s)Y(s) + R(s), then given Z(s) and Y(s) the deconv function will return the
result, Q, as well as the remainder R. Note that if there is more than one output for a
function, they should be put inside square brackets and be separated by commas. Try to
divide Z(x) by Y(x):
[Q R] = deconv(x,y)
How do we check the result?
7) Matrices
Entering matrices in to Matlab is the same as entering a vector, except that each row of
elements is separated by a semicolon. Try:
B = [1 2 3 4; 5 6 7 8; 9 10 11 12]
Matrices in Matlab can manipulated in many ways. For example, we can find the
transpose of a matrix using the apostrophe key (‘). Try:
C = B’
Remember that the order of multiplication is important when dealing with matrices.
Would the following operations be allowed?
D= B + C yes/no
D = B +C’ yes/no
D = C*B yes/no
D = B*C yes/no
E = B^3 (power of a matrix) yes/no
X = inv(B) (inverse of a matrix) yes/no
8) Functions
Matlab includes many standard functions (and constants). Each function is a block of
code that accomplishes a specific task.
Common functions
Sin, cos, log (loge), exp, sqrt, mean, std (standard deviation)
Common constants
Pi = ∏ returns 3.1416.The variables i or j represent the square root of –1 (complex
numbers).
Try the following sine function:
X= sin (pi/4) result =?
We wish to plot the sine wave of frequency 3rad/sec and amplitude 1 over a period of
time between 0 to 20 seconds (in steps of 0.1 sec):
(a) Write down the sine wave expression y(t)=?
(b) Calculate the vector y for the values of time given. Graph the results using an
appropriate plot command.
Each function in MATLAB has a number of inputs and outputs variables. We should
define all the necessary inputs before we use a function. We can use the help function to
obtain information about any function we want to use. For example, if we wanted to find
out about the use of the abs function, we could type
Help abs
Matlab returns:
ABS Absolute value.
ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the modulus (magnitude) of X.
Therefore by typing
Y=abs(x);
The variable y would hold the absolute values of x.
If we want to find the modulus and the angle of a complex number, we can enter the
following:
S = j*pi/4 define the complex number s
G = 1/s define complex function g
Mag = abs(g) find the magnitude: output: mag input: g
Ang=angle(g) find the angle: output: ang input: g
Once we have defined the output variables, they will be stored in Matlab workspace and
we can manipulate them as wish.
It lists the directories for help for many of the other toolboxes that come with Matlab that
we may use later.
For example, click on Matlab\el fun to see some basic functions. With more practice we
can write special MATLAB files (m-files) to save re-typing the same commands.
10) Plotting
It is easy to create plots in MATLAB. Suppose we make a time vector and then compute
the sine values of the vector at each time point.
t= 0:0.05:10
y = sin(t)
Plot(t,y)
Plot command has extensive add-on capabilities. Some useful features are given here.
Figure command
When we plot a graph, MATLAB opens a window called the figure Window. Every time
we plot a graph, this figure window is updated. If we want to keep the old graphs, we can
open a new window by using the command.
>> figure
Plot the first response graph required and then enter the command:
>> hold on
We can use the insert pull-down menu to place Text, labels and lines on a graph.
Use the sine wave as previously generated.
t=0:0.05:10;
y=sin(t);
plot(t,y)
Method 1:
1. We can use the command tf(num,den), where num and den are vector
coefficients of the numerator and denominator polynomials, respectively.
Enter the TF:
g1 = (3s+1}/(s2+3s+2)
num = [3,1];
den = [1 3 2]
g1 = tf(num,den)
s = tf(‘s’)
g1 = (3*s+1)/(s^2+3*s+2)
Method 2
1. We can use the command zpk(zeros,poles,gain), where zeros, poles and
gain are vectors of the zeros, poles and gain of the transfer function. For
example, we can enter the following transfer function in the form:
4( s 5 )
g2 =
( s 3 )( s 10 )
s = zpk(‘s’)
g2 = 4*(s+5)/((s+3)*(s+10))
Once various transfer functions have been entered, we can combine them together.
Use the transfer functions
g6 = 1/(s+2) and g7 = 5/(s+3)
Operation Result
Addition g6+g7
Subtraction g6 – g7
Multiplication g6*g7
Division g6/g7
Combination of g6/(1+g6)
Operations
For some functions, such as step or bode, MATLAB graphs are automatically generated.
For, these graphs, we can use the cursor to find the coordinates of different points.
Example: Type the following
>> s = tf(‘s’)
>> g = 1/(s+1)
>> step(g)
Help window:
This can be activated by choosing 'Help' (or F1) from the menu. We can use it to search
for help on any command or function.
Example:
Write an m-file which enters the following transfer function, plots the two step responses
corresponding to k = 1 and k = 2 on the same plot and titles the plot.
G1(s) = k/(s+1)
Select file, New and the M-file to open the editor,
Enter the following code
S=tf(‘s’);
k=1;
g=k/(s+1);
step(g);
hold;
k=2;
g=k/(s+1);
step(g);
title(‘step response for k=1 and k=2’)
Now select File, save as and save the file as twoplot.m in the working or your own
directory. Return to the Matlab command window. By typing
>> Twoplot
We can import models into the design tool by clicking on the file menu and then
choosing Import. The Import system Data window:
There is a basic feedback loop with the transfer function blocks representing the plant or
process, G, the sensors or measurement, H, the compensator or controller, C, and a
prefilter, f, which acts on the reference input.
Note that models should be entered into Matlab environment before we can import them
into the design tool. We can either import the compensator (controller) or enter it using
the red cross(poles) or the circle(zero) on the top left-hand corner of the SISO design
tool window.
By choosing the tools menu and loop responses, we can display the Step, Impulse, Bode,
Nyquist or Nichols plot for the system.