zmatlab tut sheet

Upload: wesnamtrow

Post on 08-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Zmatlab Tut Sheet

    1/7

    Course Notes for BEng/MEng Mechanical EngineeringB M Hanson, email [email protected] web page: http://www.meng.ucl.ac.uk/~b_hanson/MECH3005.htm

    DEPARTMENT OF MECHANICAL ENGINEERINGUNIVERSITY COLLEGE LONDON

    MECH3005 Automatic ControlMATLAB Tutorial

    IntroductionThe aim of this class is to introduce MATLAB to those who may not have used it before, and todemonstrate specific functions from the control systems toolbox that are particularly useful in controlengineering.

    Type your commands at therom t

    Previous commands are here use the cursorkeys in the command window to reuse them.You can also drag and drop commands fromthe history to the command window

    MATLAB 6.1 is found as an icon on theMENG desktop double click to start

  • 8/7/2019 Zmatlab Tut Sheet

    2/7

    Simple ArithmeticWork through the following exercises to give you a taster of some of the MATLAB commandsand to get you used to the user interfaces. This exercise will help you when you come to do theexamples 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: 5+3 5*sin(30*pi/180 ) 3^2 6*3MATLAB output:

    2. Notice that the output says ans= 8 (for the first example). What MATLAB does is assign theanswer to the variable ans (by default). This is very useful as you can now use ans for furthercalculations. Try the following and record the output (note that spaces are ignored here):

    Your Input: 5+3 ans + 3 ans + 5MATLAB output:

    3. So you get the first answer, add 3 with the second command and add 5 more with thethird command. As well as the default variable ans , you can assign your own variables. Try outthe following sequence (note 6.3 x 10 6 is entered using e for exponent):

    Your Input: grav =9.8 radius =6.3e6 escape_velocity=2*grav*radius

    MATLABoutput:

    So the escape velocity you have calculated (in m/s incidentally) is stored in the variableescape_velocity. You do need to be careful with variable names you wouldnt call a variablepi 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.

    VectorsEnter each element of the vector separated by a space, between square brackets , and set itequal 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 inincrements 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

  • 8/7/2019 Zmatlab Tut Sheet

    3/7

    Manipulating vectors is almost as easy as creating them. First, suppose you would like to add 2to each of the elements in vector 'a':

    b = a + 2

    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.

    FunctionsMatlab includes many standard functions. Each function is a block of code that accomplishes aspecific 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

    For more information on the usage of any function, type help [function name] at theMatlab command window. For example help sqrt

    PolynomialsIn Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simplyenter each coefficient of the polynomial into the vector in descending order . For instance, let'ssay you have the following polynomial:

    92153 234 ++ ssss 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

    If your polynomial is missing any coefficients, you must enter zeros in the appropriate place inthe vector. For example,

    s4 + 1 would be represented in Matlab as: y = [1 0 0 0 1]

    Also, just s 2 on its own would be represented by y = [1 0 0]

    Here were adding a scalar to a vector- trya similar method to multiply the elementsof b by a scalar (eg 3.75)

    Note: MATLAB performs trigonometry in radians only

    Note how the elements of the vector correspond to the polynomial coefficients

  • 8/7/2019 Zmatlab Tut Sheet

    4/7

    You can find the value of a polynomial using the polyval() function. For example, to find thevalue of 92153 234 ++ ssss at s = 2:

    z = polyval([1 3 -15 -2 9], 2)

    z =-15

    You can also extract the roots of a polynomial using the function roots() . This is useful whenyou have a high-order polynomial, for example 92153 234 ++ ssss :roots([1 3 -15 -2 9])

    ans =-5.5745

    2.5836-0.7951

    0.7860

    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 isfound by taking the convolution of their coefficients. Matlab's function conv() that will dothis for you. To perform this multiplication: ( ) 16166842 232 +++=+++ ssssss wed enter:x = [1 2];y = [1 4 8];z = conv(x,y)

    z =

    1 6 16 16

    Notice how the elements of vector z relate to the coefficients of s in the answer of themultiplication above. Also remember to watch out for missing coefficients, for exampleThis multiplication: ( ) 34523 8484 ssssss ++=++ would have to be calculated as:( )( ) ( )0008484000 2345223 ++++=+++++ ss plusssssssss 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 theanswer each time! This is essential for calculations on large arrays.

    Note: this function takes two inputs, or arguments: one vector and onescalar, separated by a comma.

    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.

    Note: this also finds complex roots , soits very handy for plotting root

    positions, or root locus diagrams

  • 8/7/2019 Zmatlab Tut Sheet

    5/7

  • 8/7/2019 Zmatlab Tut Sheet

    6/7

    Frequency ResponseMATLAB generates a frequency response with no trouble at all:bode(num,den)

    Its as simple as that but you can add more complexity. For example, by default, bode() plotsa 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 themagnitude response, but NOT in dB, just to confuse you. You can (of course) convert to dBusing something like:mag_in_dB = 20*log10(mag);

    You can also generate a frequency response over any frequency range you like, not justMATLABs 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 10 3

    bode(num,den,my_freqs)

    To plot in polar format, try the following:polar(phase*pi/180, mag) have a look at help polar() (Here, weve needed to convert the phase to radians.)

    Phase & Gain margins? No problem at all:margin(num,den)Though do make sure you know what youre entering i.e. the open loop transfer function!

  • 8/7/2019 Zmatlab Tut Sheet

    7/7

    Equation of motion:

    Laplace transfer function:

    i errorsignal

    outputshaftposition

    J f

    ( ) ik fs JsT =+= 20( )( ) )( f Jss

    k ss

    i

    o

    +=

    motor amplifier

    ik V = V T = 0.1

    motor

    i errorsignal

    outputshaftposition

    J f

    ( )( ) i R Ls

    k fs JsT

    +=+= 20

    Equation of motion:

    Laplace transform:

    amplifier

    ik V = V R LsT

    += 1

    ( )( ) ( )( ) R Ls f Jss

    k ss

    i

    o

    ++=

    Example:

    Using the Remote Position Control Servo Example.This example compares two models of the same system one model ignores the effect of coil impedancein the electric motor the other includes it, as a 1 st order lag.

    The two models:

    Without coil impedance With coil impedance included

    J = 0.01 kg m 2, f = 0.05 N m s, R = 1 Ohm, L = 5 x 10 -3 Henrys

    With k = 0.1, find the positions of the closed loop rootsHint: use cloop() to find the closed loop transfer function (numerator, denominator) then use pzmap()

    Hence define the impulse response of the c.l. system in terms of , n - What effect doesinclusion 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 pointat 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 naturalfrequency 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()

    Further: have a look at PM & GM as k varies.Hint: use margin(), either change the value of k, and recalculate num and den or multiply num by a scalar.

    Final task:Copy and paste the contents of the command history (see first page of these notes) into an emailto [email protected]. Make sure your email contains your name !