DEPARTMENT OF MECHANICAL ENGINEERING UNIVERSITY COLLEGE LONDON
MECH3005 Automatic Control MATLAB Tutorial
The aim of this class is to introduce MATLAB to those who may not have used it before, and to demonstrate specific functions from the control systems toolbox that are particularly useful in control engineering. MATLAB 6.1 is found as an icon on the MENG desktop – double click to start…
Type your commands at the prompt…
Previous commands are here –use the cursor keys in the command window to reuse them. You can also drag and drop commands from the history to the command window
Course Notes for BEng/MEng Mechanical Engineering B M Hanson, email firstname.lastname@example.org Course web page: http://www.meng.ucl.ac.uk/~b_hanson/MECH3005.htm
Work through the following exercises to give you a taster of some of the MATLAB commands and to get you used to the user interfaces. This exercise will help you when you come to do the examples on which this course is assessed. Start with some basic commands and record what you see in the boxes below. 1. MATLAB can do sums like a calculator Your Input: MATLAB output:
5+3 5*sin(30*pi/180 ) 3^2 – 6*3
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): Your Input: MATLAB output:
5+3 ans + 3 ans + 5
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): Your Input: MATLAB output:
grav =9.8 radius =6.3e6 escape_velocity=2*grav*radius
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 preassigned i, j, (as the square root of -1) and some other constants.
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':
b = a + 2 b = 3 4 5 6 7 8 11 10 9
Here we’re adding a scalar to a vector- try a similar method to multiply the elements of b by a scalar (eg 3.75)
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.
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) ans = 0.7071
Note: MATLAB performs trigonometry in radians only
For more information on the usage of any function, type help [function name] at the Matlab command window. For example help sqrt
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
x = [1 3 -15 -2 9] x = 1 3 -15 -2 9
Note how the elements of the vector correspond to the polynomial coefficients
If your polynomial is missing any coefficients, you must enter zeros in the appropriate place in the vector. For example, s4 + 1 would be represented in Matlab as: y = [1 0 0 0 1] Also, just s2 on its own would be represented by y = [1 0 0]
You can find the value of a polynomial using the polyval() function. For example, to find the value of s 4 + 3s 3 − 15s 2 − 2 s + 9 at s = 2:
z = polyval([1 3 -15 -2 9], 2) z = -15
Note: this function takes two inputs, or “arguments”: one vector and one scalar, separated by a comma.
You can also extract the roots of a polynomial using the function roots(). This is useful when you have a high-order polynomial, for example s 4 + 3s 3 − 15s 2 − 2 s + 9 :
roots([1 3 -15 -2 9]) ans = -5.5745 2.5836 -0.7951 0.7860
Note: this also finds complex roots, so it’s very handy for plotting root positions, or root locus diagrams
So the factorised polynomial would be (s + 5.57)(s – 2.58)(s + 0.80)(s – 0.79) 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]; z = conv(x,y) z = 1 6 16 16
Note: z = conv([1 2],[1 4 8]) would achieve the same result… You can check the answer by multiplying out the brackets by hand.
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:
+ 0 s + 0 s + 0 s + 4 s + 8 = s + 4 s 4 + 8s 3
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.
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):
The denominator can be entered as follows (there are other ways to perform the convolution):
den=conv(conv([1 0],[1 5]), conv([1 15],[1 20]));
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:
We can show the positions of the poles and zeros on the s-plane:
And we can even plot a root locus diagram with one simple command – how useful!:
Closed-loop Transfer Function
Matlab will even compute a closed-loop transfer function from an open-loop transfer function – the function cloop() should be sufficient for this course. It assumes negative feedback, with unity gain. See “help feedback” for more details/options. Here, the result of the function cloop() is a two-element vector, which we’re assigning to the two element vector “[numCL, denCL]”. Each element of that array is itself a vector.
[numCL, denCL] = cloop(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:
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…
MATLAB generates a frequency response with no trouble at all:
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
To plot in polar format, try the following: have a look at help polar() (Here, we’ve needed to convert the phase to radians.) Phase & Gain margins? No problem at all:
Though do make sure you know what you’re entering – i.e. the open loop transfer function!
Using the Remote Position Control Servo Example.
This example compares two models of the same system – one model ignores the effect of coil impedance st in the electric motor – the other includes it, as a 1 order lag. The two models:
Without coil impedance
J amplifier motor f
With coil impedance included
J amplifier motor f
V = kθ i
T = 1 .0 × V
V = kθ i
1 V Ls + R
output shaft θ position
Equation of motion:
T = θ 0 Js 2 + fs = kθ i
• Laplace transfer function:
output shaft position
Equation of motion:
T = θ 0 Js 2 + fs =
) (Lsk+ R ) θ
θ o (s ) k = θ i (s ) s( Js + f )
f = 0.05 N m s,
θ o (s ) k = θ i (s ) s( Js + f )(Ls + R )
J = 0.01 kg m ,
R = 1 Ohm,
L = 5 x 10 Henrys
With k = 0.1, find the positions of the closed loop roots Hence define the impulse response of the c.l. system in terms of ζ, ωn - What effect does inclusion of coil impedance have?
Hint: use cloop() to find the closed loop transfer function (numerator, denominator) then use pzmap()
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) 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 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! Further: have a look at PM & GM as k varies.
Hint: use rlocus(), remember to use the numerator and denominator of the open-loop transfer function
Hint: use rlocus(), and by mouse-clicking on the locus, you can identify the gain for a particular point
Hint: use margin()
Hint: use margin(), either change the value of “k”, and recalculate “num” and “den” or multiply “num” by a scalar.
Copy and paste the contents of the command history (see first page of these notes) into an email to email@example.com. Make sure your email contains your name!