1.MatlabReview

Embed Size (px)

Citation preview

  • 8/12/2019 1.MatlabReview

    1/59

    MATLAB/Simulink

    Basic MATLAB -

    matrices

    operators

    script and function files

    flow control

    plotting

    Basics of Simulink -

    state-space models (1storder ordinary diff eqns)

    setting integration properties

    setting initial conditions

    input types

    getting data to the workspace

  • 8/12/2019 1.MatlabReview

    2/59

    Basic MATLAB

    screen shot of the Matlab window

    command window

    optional windows

    workspace

    current directorytype commands here

  • 8/12/2019 1.MatlabReview

    3/59

    Matlabs help features

    type help at the command prompt

    and Matlab returns a list of help topics

  • 8/12/2019 1.MatlabReview

    4/59

    Matlabs help features

    >> help lang

    Matlabs language constructs

  • 8/12/2019 1.MatlabReview

    5/59

    Matlabs help features

    >> help for

    how to use Matlabs for statement

  • 8/12/2019 1.MatlabReview

    6/59

    Matlabs help features

    you can also access on-line help by clicking the

    question mark in the toolbar

    separate window

  • 8/12/2019 1.MatlabReview

    7/59

    MATLAB Variables

    all variables are stored in 32bit floating point format

    no distinction between real and integer

    >>a = 3;

    >>a = 3.0;

    same assignment for a

    Matlab is case sensitive

    >>A=3;

    >>a=2;

    Aa

  • 8/12/2019 1.MatlabReview

    8/59

    MATLAB Variables

    can use numbers and underscore in variable names

    >>case34=6.45;

    names must start with a letter

    >>case_34=6.45;

    OK

    >>34case=23.45; results in a syntax error

    string (text) variables enclosed in single quotes.

    The variable is stored as array of characters

    >>title=This is the title;

  • 8/12/2019 1.MatlabReview

    9/59

    MATLAB Variables

    to clear a variable from memory

    >>a=4

    >>clear a

    if a variable is defined,typing the variable name returns its value

    >>a=45.57;

    a =

    45.57 Matlab returns the value

    >>a

  • 8/12/2019 1.MatlabReview

    10/59

    MATLAB Variables

    Matlab will echo commands unless a semi-colon is used

    >>a=23.2;

    >>

    >>a=23.2

    a =

    23.2>>

    Matlab echoes the command

  • 8/12/2019 1.MatlabReview

    11/59

    MATLAB Variables

    Vectors

    column vectors row vectors1

    a 2

    3

    a 1 2 3

    >>a=[1;2;3];

    >>a

    a =

    12

    3

    >>a=[1,2,3];

    >>a

    a =

    1 2 3

    use semi-colon

    to separate rows

    use comma

    to separate columns

  • 8/12/2019 1.MatlabReview

    12/59

    MATLAB Variables

    Matrices

    2-dimensional matrices1 2 3

    a4 5 6

    >>a=[1,2,3;4,5,6];>>a

    a =

    1 2 3

    4 5 6

    again, separate columns with commas and rows with semi-colons

  • 8/12/2019 1.MatlabReview

    13/59

    MATLAB Variables

    A vector is a special type of matrix

    row vector is a 1 x n matrix, 1 row n columns

    column vector is a n x 1 matrix, n rows 1 column

    Indexing Matrix elements

    >>a=[1,2,3];

    >>a(2)ans =

    2

    could also reference by a(1,2)

    note, a(2,1) would produce an error

    because a only has one row

  • 8/12/2019 1.MatlabReview

    14/59

    MATLAB Variables

    Indexing Matrix elements

    >>a=[1,2,3;4,5,6];1 2 3

    a4 5 6

    more examples

    >>a(2,3)

    ans =

    6

    >>a(2,2)=9;

    >>a

    a =

    1 2 3

    4 9 6

    addressingassigning

  • 8/12/2019 1.MatlabReview

    15/59

    MATLAB Variables

    complex-valued numbers

    Typically, the variable i or j is used to represent the

    complex variable; e.g.

    i 1

    Then, a complex number is represented as

    z = a + ib Re(z) = a

    Im(z) = b

  • 8/12/2019 1.MatlabReview

    16/59

    MATLAB Variables

    complex-valued numbers

    Unless i or j has been previously defined, Matlab assigns

    i and j the complex variable value

    In Matlab, a complex variable is represented in the

    following format

    (assuming all variables are cleared)

    >>z=23+i*56;

    >>zz =

    23.00 + 56.00i

    >>z=23+j*56;

    >>zz =

    23.00 + 56.00i

    Matlab always uses the symboli to represent a complex number

  • 8/12/2019 1.MatlabReview

    17/59

    MATLAB Variables

    complex-valued numbers

    What happens in this case?

    >>i=3;

    >> z=23+i*56;

    >>z

    z =

    What happens in this case?

    >>a=sqrt(-1);

    >>z=23+a*56;

    >>z

    z =

  • 8/12/2019 1.MatlabReview

    18/59

    MATLAB Variables

    complex-valued numbers

    Note, a real-valued number is a special case of a

    complex-valued number

    assigning any element of a matrix as complex-valuedmakes the entire matrix complex-valued

    >>a=[1,2];

    >>a

    a =

    1 2

    >>a(1)=1+i*5;>>a

    a =

    1.00+5.00i 2.00+0.00i

  • 8/12/2019 1.MatlabReview

    19/59

    MATLAB Variables

    Advanced data types

    n-dimensional arrays

    structures

    cell arrays

  • 8/12/2019 1.MatlabReview

    20/59

    MATLAB Operations

    Basic operationsaddition +

    subtraction -

    multiplication *

    divisionright division /

    left division \

    >>a=3;b=4;>>c1=a/b;

    >>c2=a\b;

    ?

    c1=0.75

    c2=1.3333.so, be careful!

  • 8/12/2019 1.MatlabReview

    21/59

    MATLAB Operations

    Mixed Real and Complex valued Variables

    if both variables are real-valued, a real-valued result is obtained

    if one variable is complex-valued, Matlab recasts the real

    variable as complex and then performs the operation. The

    result is complex-valued

    however, the type casting is done internally, the real-valued

    variable remains real after the operation

  • 8/12/2019 1.MatlabReview

    22/59

    MATLAB Operations

    Other (Scalar) Operations

    Math representation Matlab interpretationxz y

    xy e

    y ln(x)

    y log(x)

    y sin(x)

    y cos(x)

    y tan(x)

    1

    1

    1

    y sin (x)

    y cos (x)

    y tan (x)

    >>z=y^x;

    >>y=exp(x);

    >>y=log(x);

    >>y=log10(x)

    >>y=sin(x);

    >>y=cos(x);

    >>y=tan(x);

    >>y=asin(x);

    >>y=acos(x);

    >>y=atan(x);

  • 8/12/2019 1.MatlabReview

    23/59

    MATLAB Operations

    Examples

    y x>>y=x^0.5;

    >>y=x^(1/2);

    >>y=sqrt(x);

    All variables in the preceding operations can be

    real or complex, negative or positive

    for x < 0, y is complex. Matlab assumes you allow complex

    valued numbers. If y is notto be complex, you must

    provide error checking.

  • 8/12/2019 1.MatlabReview

    24/59

    MATLAB Operations

    Matrices

    Only matrices of the same dimension can be added and subtracted

    For multiplication, the inner dimensions must be the same

    1 2 3

    4 5 6

    A 2 3 4

    5 6 7

    B

    4 5

    6 7

    8 9

    C

    No error Error

    >>D=A+B;

    >>D=A-B;

    >>D=A*C;

    >>D=C*A;

    >>D=A+C;

    >>D=A*B;

    >>D=B*A;Matrix multiplication

    not commutative

  • 8/12/2019 1.MatlabReview

    25/59

    MATLAB Operations

    Left(\) and Right(/) Matrix division

    Math representation Matlab interpretation

    1C A B

    1C BA

    >>C=A\B;

    >>C=B/A;

    Remember, Amust be square and full rank

    (linearly independent rows/columns)

  • 8/12/2019 1.MatlabReview

    26/59

    MATLAB Operations

    Matrix Transpose

    Math representation Matlab interpretation

    TC A >>C=A;

    For complex-valued matrices, complex conjugate transpose

    1 2 3

    4 5 6

    A

    1 4

    2 5

    3 6

    B

    >>B=A;

    1 j2 3 j4 a

    1 j2

    3 j4

    b

    >>b=a;

  • 8/12/2019 1.MatlabReview

    27/59

    MATLAB m-files

    Two types of m-files

    script files

    collection of commands that Matlab executes

    when the script is run

    function files

    collection of commands which together

    represent a function, a procedure or a method

    Both types are separate files with a .m extension

  • 8/12/2019 1.MatlabReview

    28/59

    MATLAB m-files

    To create an m-file, open the Matlab text editor

    Click on the page icon

    The Matlab text editor window will open

  • 8/12/2019 1.MatlabReview

    29/59

    MATLAB m-files

    Script Files

    On the command line

    >>x=3.0;

    >>y=x^2;

    >>y

    y =9.0

    >>

    In the script file named test.m

    On the command line

    >>test

    y =

    9.0

    >>

  • 8/12/2019 1.MatlabReview

    30/59

    MATLAB m-files

    Script Files

    script files share the workspace memory

    >>x=5.0;

    >>test>>y

    y =

    25.0

    >>

    test.m script

  • 8/12/2019 1.MatlabReview

    31/59

    MATLAB m-files

    Script Files

    script files can call other script files

    >>outter

    y =

    36.0

    >>

    inner.m script

    outter.m script

  • 8/12/2019 1.MatlabReview

    32/59

    MATLAB m-files

    Function Files

    Matlab identifies function files from script files byusing the function and return keywords

    the name of the function file must be

    the same name as the function

  • 8/12/2019 1.MatlabReview

    33/59

    The function file x2.m

    MATLAB m-files

    Function Files

    >>r=3;

    >>d=x2(r);>>d

    d =

    9.0

    >>

    >>h=x2(4.2);

    >>hh =

    17.64

    >>

  • 8/12/2019 1.MatlabReview

    34/59

    MATLAB m-files

    Function Files

    Multiple Inputs and Outputs

    outputs in square brackets, [ ] inputs in parentheses ( )

  • 8/12/2019 1.MatlabReview

    35/59

    variables created in the function are not retainedin the workspace, except for the output variables

    the function does not have access to workspacevariables, except for the inputs

    MATLAB m-files

    Function Files

    variables passed to the function are copies of theworkspace variables. Changing their value inside the

    function has no effect on their value in the workspace.

  • 8/12/2019 1.MatlabReview

    36/59

    MATLAB Flow Control

    The while and if statements

    if expression

    statements

    end

    if expression

    statements1

    else

    statements2end

    Matlab evaluates expressionas logical true or false

    false equivalent to zero

    true equivalent to any non-zero number

    statements, any valid Matlab command

    while expression

    statements

    end

  • 8/12/2019 1.MatlabReview

    37/59

    MATLAB Flow Control

    evaluating expression

    any valid equationa=4;

    b=5;

    c=5;

    if a+bif b-c TrueFalse

    watch out for round-off

    and word length error

    if sin(0)

    if sin(pi)

    sin(pi) = 1.22e-16

    False

    True

    conditional operators== equal to

    < less than

    > greater than

    = greater than or equal to

    ~= not equal to

    logical operators

    & and

    | or

    while(3

  • 8/12/2019 1.MatlabReview

    38/59

    MATLAB Flow Control

    The for statement

    for index=start : [increment:] endstatements

    end

    index,start, increment, and enddo not need to be integer valued

    incrementis optional, if incrementis not specified

    incrementdefaults to 1

    indexcan be incremented positive (increment> 0) ornegative (increment< 0)

    loop stops when index> end (or index< end)

  • 8/12/2019 1.MatlabReview

    39/59

    MATLAB Flow Control

    example

    script file to cycle through x values

    function file to generate the y values

  • 8/12/2019 1.MatlabReview

    40/59

    MATLAB Plotting

    Basic 2D plotting functions

    plot(x1,y1[,x2,y2,x3,y3.....])

    xlabel(x axis name)

    ylabel(y axis name)

    title(graph name)

    Additional functions

    grid ongrid off

    axis([xmin,xmax,ymin,ymax])

  • 8/12/2019 1.MatlabReview

    41/59

    MATLAB Plottingexample y = sin(t)

    the plot function alone

  • 8/12/2019 1.MatlabReview

    42/59

    MATLAB Plottingexample y = sin(t)

    script file to generate

    a graph of y = sin(t)

    MATLAB Pl tti

  • 8/12/2019 1.MatlabReview

    43/59

    function file to generate

    a graph of y = sin(t)

    >>graphsin

    >>

    MATLAB Plottingexample y = sin(t)

    MATLAB Pl tti

  • 8/12/2019 1.MatlabReview

    44/59

    legend remembers

    the order the graphs

    were plotted

    MATLAB PlottingAdding a Legend for multiple graphs

    Si li k B i

  • 8/12/2019 1.MatlabReview

    45/59

    Simulink Basics

    click the Simulink button

    the Simulink window

    Simulink Basics

  • 8/12/2019 1.MatlabReview

    46/59

    click the new button

    the simulink model window

    Simulink Basics

    create a new model oropen an existing one

    Si li k E l

  • 8/12/2019 1.MatlabReview

    47/59

    Simulink Example

    Best thing to do is to go through an example

    Response to a step command

    2ndorder, constant coefficient, linear differential equation

    1 0 0y c y c y b f (t)

    Si li k E l

  • 8/12/2019 1.MatlabReview

    48/59

    Simulink Example

    Get an equivalent block diagram for the system

    use integrators to get dy/dt and y

    use mouse to drag blocks into

    the model window and to

    connect blocks with arrows

  • 8/12/2019 1.MatlabReview

    49/59

    add gain and summer blocks

    Simulink Example

    Simulink Example

  • 8/12/2019 1.MatlabReview

    50/59

    Simulink Example

    add the step input block

    Simulink Example

  • 8/12/2019 1.MatlabReview

    51/59

    Simulink Example

    add the output block

    Simulink Example

  • 8/12/2019 1.MatlabReview

    52/59

    Simulink Example

    Now, double click the blocks to open and set the blocks parameters

    set initial condition

    set gain value

    set variable name

    set output format to array

    Simulink Example

  • 8/12/2019 1.MatlabReview

    53/59

    Simulink Example

    To set the simulation parameters.

    select Simulation -> Simulation Parameters

    set Start and Stop time (in seconds)

    set numerical integration type

    Simulink Example

  • 8/12/2019 1.MatlabReview

    54/59

    Simulink Example

    Time to run the simulation

    click the run button to begin the simulation

    when the simulation is complete, Ready appears at the bottom

    Simulink Example

  • 8/12/2019 1.MatlabReview

    55/59

    Simulink Example

    Simulink will automatically save a variable named tout to

    the workspace.

    This variable contains the time values used in the simulation,important for variable time integration types

    Simulink also will create the output variable(s) you specified

    Simulink Example

  • 8/12/2019 1.MatlabReview

    56/59

    Simulink Example

    >>plot(tout,yoft)

    graph of the step response

    Simulink Example

  • 8/12/2019 1.MatlabReview

    57/59

    Simulink Example

    Another approach to solving the 2ndorder single DOF

    problem, is to cast it as a 1storder 2 DOF problem

    1

    2

    x y

    x y

    1 2

    2 o 1 2 o 1

    x x

    x b f c x c x

    In Matrix (or State Space) form. u

    y

    x Ax B

    Cx

    o 1

    0 1

    c c

    Ao

    0

    b

    B

    1 0C

    1

    2

    x

    x

    x

    u f

    Simulink Example

  • 8/12/2019 1.MatlabReview

    58/59

    Simulink Example

    1stOrder State-Space Models

    Simulink Example

  • 8/12/2019 1.MatlabReview

    59/59

    Simulink ExampleMulti Input Multi Output Systems

    use Mux and Demux blocks to combine and extract vector signals

    specify number of signals