P1 Matlab - Matlab in Control Systems

Embed Size (px)

Citation preview

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    1/24

    MATLAB in Control

    Systems

    Practical Session 1

    Commonly used commands

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    2/24

    Principles

    MATLAB: Matrix environment i.e. matrices, vectors,constants and labels as variables.

    Different toolboxes access different environments

    Poly-nomial

    ControlSystems

    SymbolicMath

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    3/24

    Organising the workspace

    All MATLAB commands are typed in lowercase .

    Recommendation: Capitalise your variables names

    Previous commands can be accessed by using theUP arrow, so that

    Commands issued incorrectly, can be accessed andedited

    Hitting will execute the edited command.

    Clear variables with clear Var or clear Clear command window with clc no effect on stored

    variables or command history

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    4/24

    Polynomial Domain

    Rows

    Matrices and vectors

    321

    khg

    fed

    cba

    Matrix row elements are separated by spaces or

    commas (or both), e.g. [2 3 4] or [2, 3, 4]

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    5/24

    Polynomial Domain

    Columns Columns are created by separating rows with semi-

    colons, e.g. [1, 2, 3; 4, 5, 6].

    Columns may also be ceated by entering a carriagereturn after each row:

    [2 4 6

    8 5 3];

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    6/24

    Polynomial DomainMatrix Manipulations

    Matrices and vectors may be transposed byadding an apostrophe after the specification,

    e.g. A or B=[1 2 4].

    Normal rules regarding dimensions applywhen arithmetic operations are performed.

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    7/24

    Polynomial DomainInverse and determinants

    The inverse of a matrix may be obtained by thecommands inv(A) or A-1, i.e. A^-1

    The determinant of a matrix may be found by thecommand det(A)

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    8/24

    Polynomial DomainVectors

    Vectors are matrices that have either a singlerow of elements, or a single column ofelements.

    Row vectors are used in Control Systems torepresent polynomials, i.e.

    [1 0 4 6 9]represents

    s4 + 0 + 4s2 + 6s + 9

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    9/24

    Polynomial DomainVariables

    Variables can be created by setting thevariable name equal to the value e.g.

    Mat = [2,6,8;2,4,6;9,7,8];

    K = 5;V = [4 6 8];

    L = Label;

    Depending on the type , various operationsmay be performed on variables, e.g.

    C=9;

    D=sqrt(C) OR D=C^0.5

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    10/24

    Polynomial DomainCreating Labels

    Labels are created by including them in singlequotes, e.g. Label

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    11/24

    Programming

    Commands are executed as they are issuedin the command window.

    A set of commands may be programmed inan m-file, and batch-executed from the m-file,by selecting (high-lighting) the commands,followed by pressing the F9 key.

    M-files stored in the specified path may alsobe executed from the command window, bytyping the name of the m-file.

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    12/24

    Programming

    Once the answer is obtained, it may becopied from the command window back into

    the m-file, and commentedExample: N=[1 4]; D=[2 4 6];

    tf(N,D)

    % s+4% --------------

    % 2s2+4s+6

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    13/24

    Commonly Used Commands

    Nise Chapter 2 (1)

    conv(A,B) Used to multiply polynomials,

    represented as row vectors, with each other.(Also conv([1 3 5],[2 4 6])

    poly(A) Where A=[-a, -b, -c]) Used tocreate polynomials if roots are known, i.e. findA as a polynomial if A=(s+a)(s+b)(s+c).

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    14/24

    Commonly Used Commands

    Nise Chapter 2 (2) roots(A) Used to extract the value of the roots of a

    polynomial.

    residue(A,B) Used to find the residues

    (coefficients) of the partial fractions of a function ofwhich the numerator is A (or [a b c ]) and the

    denominator is B (or [f g h ]).

    [c,r,d]=residue(A,B) Used to find the coefficients,

    roots and direct quotient of the function A/B as abovein a single operation. The answer is given as columnvectors c (coefficients), r (roots) and row vector d(direct quotient).

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    15/24

    Commonly Used Commands

    Nise Chapter 2 (3) tf(A,B) Creates the transfer function in the

    classical form

    zpk(N,D,k) Creates the transfer function inthe zero-pole-gain form. N are the roots of thenumerator, i.e. if the numerator is (s+4)(s -2),N = [-4, 2], and D the roots of the

    denominator.k is a coefficient, known as the direct gain.

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    16/24

    Commonly Used Commands

    Nise Chapter 2 (4) Once the transfer function is found in one

    form, in can be converted to the other form bythe same commands, e.g.

    A=tf([1,4,4],[1 3 5 7]);

    B=zpk(A) and

    C=tf(B) (=A)

    Transfer functions found in this way arereferred to as LTI objects (Linear TimeInvariant Objects)

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    17/24

    Converting between domains

    Control Systems to Polynomial Transfer functions (LTI objects) may be

    converted to the polynomial domain with the

    instruction: [N,D]=tfdata(G,v)

    Example:

    8145

    12223

    2

    sss

    ssG

    [N,D]=tfdata(G,v) yields:

    N=[0 1 2 12]

    D=[1 5 14 8]

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    18/24

    Converting between domains

    Polynomial to Symbolic Math Row and column vectors can be converted to

    symbolic math with the poly2sym(A)

    command Column vectors will yield the same result as

    the transposedrow vector.

    The default variable is x

    Example: If A=[1 3 5] and B=[1;3;5]

    poly2sym(A) yields: poly2sym(B) yields:

    x 2+3x+5 x 2+3x+5

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    19/24

    Converting between domains

    Symbolic Math to Polynomial (1) Variables must be declared for symbolic

    math e.g syms s

    The numerator and denominator of asymbolic math expression is firstseparated with `[N,D] = numden(H)

    The numerator and denominator arethen separately converted to thepolynomial domain with sym2poly(A)

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    20/24

    Converting between domains

    Symbolic Math to Polynomial (2)Example: syms s

    A=(s+4)/(s^2+2*s+24);

    [Ns,Ds]= numden(A)% Ns=s+4

    % Ds= s^2+2*s+24

    N=sym2poly(Ns)

    % N=[1 4]

    sym2poly(Ds)

    % D=[1 2 24]

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    21/24

    Laplace and Inverse Laplace

    Laplace transforms may be converted tosymbolic math functions with ilaplace(B)

    Example

    T=(s+3)/(s^2+8*s+25)

    Var=ilaplace(T)

    % exp(-4*t)*cos(3*t)-1/3*exp(- 4*t)*sin(3*t)

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    22/24

    Example 1

    Find the output (in the time domain) if a28-unit step is applied to a system with

    the given transfer function, using theresidue() command in MATLAB

    (For solution, see P1 Example1.doc file)

    )4110)(7(

    )9(205)(

    2

    sss

    ssT

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    23/24

    Assignment 1(a)

    Find the output (in the time domain) if a113-unit step is applied to a system with

    the given transfer function. Verify youranswer, using the residue() command inMATLAB

    )11316(15

    )4(26)(

    2

    sss

    ssT

  • 8/4/2019 P1 Matlab - Matlab in Control Systems

    24/24

    Assignment 1(b)

    Find the output (in the time domain) if a109-unit impulse is applied to a system

    with the given transfer function. Verifyyour answer, using MATLAB

    )12510(15

    )4(4

    )2(1)( 2 ss

    sss

    sT