2. Notice that the output says ans= 8 (for the first example). What MATLAB does is assign the
answer to the variable ans (by default). This is very useful as you can now use ans for further
calculations. Try the following and record the output (note that spaces are ignored here):
3. So you get the first answer, add 3 with the second command and add 5 more with the
third command. As well as the default variable ans, you can assign your own variables. Try out
the following sequence (note 6.3 x 106 is entered using “e” for exponent):
So the escape velocity you have calculated (in m/s incidentally) is stored in the variable
escape_velocity. You do need to be careful with variable names – you wouldn’t call a variable
pi as MATLAB would get confused with its internal value of pi. MATLAB has also pre-
assigned i, j, (as the square root of -1) and some other constants.
Vectors
Enter each element of the vector separated by a space, between square brackets, and set it
equal to a variable. For example, to create a vector “a”, enter into the Matlab command window:
a = [1 2 3 4 5 6 9 8 7]
Matlab should return:
a =
1 2 3 4 5 6 9 8 7
Let's say you want to create a vector with elements between 0 and 20 evenly spaced in
increments of 2 (this method is frequently used to create a time vector):
t = 0:2:20
t =
0 2 4 6 8 10 12 14 16 18 20
Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2
to each of the elements in vector 'a':
Here we’re adding a scalar to a vector- try
b = a + 2 a similar method to multiply the elements
of b by a scalar (eg 3.75)
b =
3 4 5 6 7 8 11 10 9
Now suppose, you would like to add two vectors together. If the two vectors are the same length,
it is easy. Simply add the two as shown below:
c = a + b
c =
4 6 8 10 12 14 20 18 16
Subtraction of vectors of the same length works exactly the same way.
Functions
Matlab includes many standard functions. Each function is a block of code that accomplishes a
specific task. Matlab contains all of the standard functions such as sin(), cos(), ln(),
log10(), exp(), sqrt(), as well as many others.
sin(pi/4)
Note: MATLAB performs trigonometry in
ans = radians only
0.7071
For more information on the usage of any function, type help [function name] at the
Matlab command window. For example help sqrt
Polynomials
In Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply
enter each coefficient of the polynomial into the vector in descending order. For instance, let's
say you have the following polynomial:
s 4 + 3s 3 − 15s 2 − 2 s + 9
To enter this into Matlab, just enter it as a vector in the following manner
If your polynomial is missing any coefficients, you must enter zeros in the appropriate place in
the vector. For example,
ans =
-5.5745 Note: this also finds complex roots, so
2.5836 it’s very handy for plotting root
-0.7951 positions, or root locus diagrams
0.7860
Very often, we want to multiply two polynomials together. The product of two polynomials is
found by taking the “convolution” of their coefficients. Matlab's function conv() that will do
( )
this for you. To perform this multiplication: (s + 2 ) s 2 + 4 s + 8 = s 3 + 6 s 2 + 16 s + 16 we’d enter:
x = [1 2];
y = [1 4 8]; Note: z = conv([1 2],[1 4 8])
z = conv(x,y) would achieve the same result…
Notice how the elements of vector z relate to the coefficients of s in the answer of the
multiplication above. Also remember to watch out for “missing” coefficients, for example
( )
This multiplication: s 3 s 2 + 4 s + 8 = s 5 + 4 s 4 + 8s 3 would have to be calculated as:
(s 3
)( )
+ 0 s + 0 s + 0 s + 4 s + 8 = s + 4 s 4 + 8s 3
2 2 5
( plus 0s 2 + 0s + 0 )
x = [1 0 0 0];
y = [1 4 8];
z = conv(x,y)
z =
1 4 8 0 0 0
Note: using a semicolon at the end of the line (as seen above) stops MATLAB from printing the
answer each time! This is essential for calculations on large arrays.
Transfer Functions
Consider an open loop system which has a transfer function of
Y (s) s+7
H (s) = =
U ( s ) s ( s + 5)( s + 15)( s + 20)
Enter the transfer function by entering the two polynomials (called num and den here just to
indicate to ourselves that they’re numerator and denominator, but we’re free to call them other
things, e.g. A, B, plantnum, plantden):
num=[1 7];
The denominator can be entered as follows (there are other ways to perform the convolution):
Note that the “s” term is represented by the vector [1 0] meaning 1 times “s” and 0 times “s0”).
Each “conv” function returns a vector result of a multiplication, and “den” is set to be the result
of the multiplication of those two vectors.
You can check the value of any variable by just typing the name and pressing enter:
>> den
den =
1 40 475 1500 0
Once a system has been entered in this format, MATLAB can do lots of useful things:
Step and impulse responses:
step(num,den)
impulse(num,den)
We can show the positions of the poles and zeros on the s-plane:
pzmap(num,den)
And we can even plot a root locus diagram with one simple command – how useful!:
rlocus(num,den)
We’re using the terms “numCL” and “denCL” here, but you can call the closed loop numerator
and denominator whatever you like.
Now you can look at the step response of the closed loop transfer function:
step(numCL,denCL)
You might notice that the steady state error to a step input is zero, since the open-loop transfer
function has a pole at the origin…
Frequency Response
MATLAB generates a frequency response with no trouble at all:
bode(num,den)
It’s as simple as that – but you can add more complexity. For example, by default, bode() plots
a graph, but you can also assign the results to variables in the workspace:
[mag,phase,w]=bode(num,den); you might want a semicolon here
Where w is the list of frequencies used, phase is the phase angle in degrees, and mag is the
magnitude response, but NOT in dB, just to confuse you. You can (of course) convert to dB
using something like:
mag_in_dB = 20*log10(mag);
You can also generate a frequency response over any frequency range you like, not just
MATLAB’s chosen range. In that case, the logspace command is useful (see its help file):
my_freqs = logspace(-2,3,100); Generates 100 log-spaced points between 10-2 and 103
bode(num,den,my_freqs)
motor motor
amplifier amplifier
1
θi V = kθ i T = 1 .0 × V θi V = kθ i T= V
error error Ls + R
signal signal
output output
• Equation of motion: • Equation of motion:
shaft θ shaft θ
(
T = θ 0 Js 2 + fs = kθ i ) position (
T = θ 0 Js 2 + fs = ) (Lsk+ R ) θ i
position
θ o (s ) k θ o (s ) k
= =
• Laplace transfer function: θ i (s ) s( Js + f )
• Laplace transform:
θ i (s ) s( Js + f )(Ls + R )
2 -3
J = 0.01 kg m , f = 0.05 N m s, R = 1 Ohm, L = 5 x 10 Henrys
• Hence define the impulse response of the c.l. system in terms of ζ, ωn - What effect does
inclusion of coil impedance have?
Hint: these are calculated from the positions of the poles on the s-plane. If you mouse-click on the poles, MATLAB
displays information about the position of the poles – damping and natural frequency.
• Now plot the full root locus for the each system (as a check, this should pass through your point
at k=0.1)
Hint: use rlocus(), remember to use the numerator and denominator of the open-loop transfer function
• For each system, find the value of K to give ζ = 0.7 and 0.5 and find the damped natural
frequency ωd at those points
Hint: use rlocus(), and by mouse-clicking on the locus, you can identify the gain for a particular point
• Fully describe the stability of each system with k = 0.1 (phase margin, gain margin). Critical gain
can be found using Routh array or Nyquist method – try both, see which you prefer!
Hint: use margin()
Final task:
Copy and paste the contents of the command history (see first page of these notes) into an email
to b.hanson@ucl.ac.uk. Make sure your email contains your name!