W6 MATLAB Fundamentals

download W6 MATLAB Fundamentals

of 62

Transcript of W6 MATLAB Fundamentals

  • 8/2/2019 W6 MATLAB Fundamentals

    1/62

    Centre for Computer Technology

    ICT114Mathematics for

    Computing

    Week 6

    Fundamentals of MATLAB

  • 8/2/2019 W6 MATLAB Fundamentals

    2/62

    March 20, 2012 Copyright Box Hill Institute

    Objectives

    Review Week5

    MATLAB workspace

    Basic Operations Matrix and Array Operations

    Control statements

    Script files

    M-files

  • 8/2/2019 W6 MATLAB Fundamentals

    3/62

    March 20, 2012 Copyright Box Hill Institute

    Queue Characteristics

    Queueing systems are characterized byfive components

    Arrival pattern of customers

    Service pattern

    Number of servers

    The capacity of the facility to hold customers The order in which customers are served

  • 8/2/2019 W6 MATLAB Fundamentals

    4/62

    March 20, 2012 Copyright Box Hill Institute

    Kendalls notation

    Kendalls notation for specifying a queuecharacteristics is v/ w/ x/ y/ z

    v indicates arrival patternw denotes service pattern

    x denotes the number of servers

    y represents system capacity (number ofcustomers)

    z designates queue discipline

  • 8/2/2019 W6 MATLAB Fundamentals

    5/62

    March 20, 2012 Copyright Box Hill Institute

    Some notations for v/ w/ z

    Queue characteristic Symbol Meaning

    Interarrival timeor

    Service time

    D

    M

    Ek (k = 1, 2, 3..)

    G

    Deterministic

    Exponentially distributed

    Erlang type distributed

    Any other distribution

    Queue discipline

    FIFO

    LIFOSIRO

    PRI

    GD

    First in first out

    Last in first outService in random order

    Priority ordering

    Any other ordering

  • 8/2/2019 W6 MATLAB Fundamentals

    6/62

    March 20, 2012 Copyright Box Hill Institute

    Example

    Write an algorithm to determine astudents final grade and indicate whether

    it is passing or failing. The final grade iscalculated as the average of four marks.

    (Algorithms and Flowcharts)

  • 8/2/2019 W6 MATLAB Fundamentals

    7/62

    March 20, 2012 Copyright Box Hill Institute

    Example - Pseudocode

    Input a set of 4 marks

    Calculate their average by summing and dividingby 4

    if average is below 50

    Print FAIL

    else

    Print PASS

    (Algorithms and Flowcharts)

  • 8/2/2019 W6 MATLAB Fundamentals

    8/62

    March 20, 2012 Copyright Box Hill Institute

    Example - Algorithm

    Step 1: Input M1,M2,M3,M4

    Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE < 50) then

    Print FAILelse

    Print PASS

    endif

    (Algorithms and Flowcharts)

  • 8/2/2019 W6 MATLAB Fundamentals

    9/62

    March 20, 2012 Copyright Box Hill Institute

    Flowchart - Example

    PRINTPASS

    Step 1: Input M1,M2,M3,M4Step 2: GRADE (M1+M2+M3+M4)/4Step 3: if (GRADE

  • 8/2/2019 W6 MATLAB Fundamentals

    10/62

    Centre for Computer Technology

    Fundamentals of MATLAB

  • 8/2/2019 W6 MATLAB Fundamentals

    11/62

    March 20, 2012 Copyright Box Hill Institute

    Introduction

    MATLAB stands for Matrix Laboratory It is a Numerical Computation software for

    Engineering and Scientific calculations Developed by John Little and Cleve Moler

    of MathWorks, Inc. The basic Data Type is a Matrix, which

    doesnt require dimensioning MATLAB can be used to generate a

    variety of Graphs

  • 8/2/2019 W6 MATLAB Fundamentals

    12/62

  • 8/2/2019 W6 MATLAB Fundamentals

    13/62

    March 20, 2012 Copyright Box Hill Institute

    MATLAB Variables

    Legal variable names

    Begin with one of az or AZ

    Have remaining characters chosen fromaz, AZ, 09, or _

    Maximum length of 63 characters (ver 6.5)

    Should not be the name of a built-invariable, built-in-function, or user-definedfunction

  • 8/2/2019 W6 MATLAB Fundamentals

    14/62

    March 20, 2012 Copyright Box Hill Institute

    Basic Operations

    The basic data type is a numerical rectangularmatrix with real or complex numbers

    MATLAB statements are of the form

    variable =expression

    Expressions typed manually (typed by user) areinterpreted immediately and evaluated

    If the statements ends with a semicolon ; , it isevaluated, but the display is suppressed

  • 8/2/2019 W6 MATLAB Fundamentals

    15/62

    March 20, 2012Copyright Box Hill Institute

    Basic Operations The variables are case sensitive.

    casesen off turns this off

    exit or quit, are the commands to exit

    MATLAB help, is for online help

    helpfunction name, is to obtain informationon a specific function

    lookfor, searches the summary informationfor each function

    clc clears the command window

  • 8/2/2019 W6 MATLAB Fundamentals

    16/62

    March 20, 2012Copyright Box Hill Institute

    Matrices and Vectors

    All Matlab variables are matrices

    A Matlab vector is a matrix with one

    row orone column

    A Matlab scalar is a matrix with onerow andone column

  • 8/2/2019 W6 MATLAB Fundamentals

    17/62

    March 20, 2012Copyright Box Hill Institute

    Basic Operations

    A matrix1 2 3

    A= 2 3 4 is represented by

    3 4 5

    A = [1 2 3; 2 3 4; 3 4 5];or

    A = [ 1 2 32 3 43 4 5 ];

  • 8/2/2019 W6 MATLAB Fundamentals

    18/62

    March 20, 2012Copyright Box Hill Institute

    Basic Operations A row vector

    B = [ 6 9 12 15 18] is represented byB = [ 6 9 12 15 18] or B = [6, 9, 12, 15, 18]

    A column vectorC = 6 C = [ 69 912 is represented by 1215 1518 18]

    orC = [6;9;12;15;18 ]

  • 8/2/2019 W6 MATLAB Fundamentals

    19/62

    March 20, 2012Copyright Box Hill Institute

    Matrix Operations

    The basic matrix operations are addition(+),subtraction(-), multiplication(*) and conjugatetranspose ()

    There are two forms of matrix division, the leftinverse operator (\) and the right inverseoperator (/)

    size(A) is used to obtain the size of a matrix

    A, the result will be a row matrix with twovalues. The is the number of rows and thesecond the number of columns of A

  • 8/2/2019 W6 MATLAB Fundamentals

    20/62

    March 20, 2012Copyright Box Hill Institute

    Matrix Operations The sum and difference of matrices with same

    dimensions can be determined For two matrices E and F with same dimensions

    E = [7 2 3; 4 3 6; 8 1 5];

    F = [1 4 2; 6 7 5; 1 9 1];

    The sum H = E + F The matrix H will appear as

    H =

    8 6 510 10 119 10 6

  • 8/2/2019 W6 MATLAB Fundamentals

    21/62

    March 20, 2012Copyright Box Hill Institute

    Matrix Operations

    For the matrices E and F the differenceG = E F

    The difference will appear as

    G =6 -2 1-2 -4 17 -8 4

    A scalar matrix may also be added or subtractedfrom a matrix in a similar manner

  • 8/2/2019 W6 MATLAB Fundamentals

    22/62

    March 20, 2012Copyright Box Hill Institute

    Matrix Operations

    For two matrices X (n x m) and Y (i x j) Matrix Multiplication is defined if the innermost operands

    of the two operands are the same, i.e. in the above casem=i

    For the matrices E and F (both 3x3), the productQ = E*F will appear asQ =

    22 69 27

    28 91 2919 84 26 The matrix can be multiplied by a scalar in a similar

    fashion

  • 8/2/2019 W6 MATLAB Fundamentals

    23/62

    March 20, 2012Copyright Box Hill Institute

    Matrix Operations Matrix division can be either left inverse operator (\) or

    right inverse operator (/) For two matrices x and y

    x/y is equivalent to x div yx\y is equivalent to y div x

    If a*b = c and a is a nonsingular matrix, thena\c is equivalent to b= inv(a)*c

    a/c is equivalent to b = c*inv(a)

    Where inv is the MATLAB function to obtain the inverseof a matrix

  • 8/2/2019 W6 MATLAB Fundamentals

    24/62

    March 20, 2012Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    25/62

    March 20, 2012Copyright Box Hill Institute

    Syntax

    A = ones(nrows,ncols)

    A = zeros(nrows,ncols)

    A = eye(n)

    A = eye(nrows,ncols)

    A = diag(v) v= diag(A)

  • 8/2/2019 W6 MATLAB Fundamentals

    26/62

    March 20, 2012

    Copyright Box Hill Institute

    Example

  • 8/2/2019 W6 MATLAB Fundamentals

    27/62

    March 20, 2012

    Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    28/62

    March 20, 2012 Copyright Box Hill Institute

    Subscript Notation

    If A is a matrix, A(i,j)selects the element in the ith rowand jth column.

    Subscript notation can be used on the right hand side ofan expression to refer to a matrix element.

    >> A = [1 2 3; 4 5 6; 7 8 9];>> b = A(3,2)

    b = 8

    >> c = A(1,1)c = 1

  • 8/2/2019 W6 MATLAB Fundamentals

    29/62

    March 20, 2012 Copyright Box Hill Institute

    Subscript Notation

    Subscript notation is also used to assignmatrix elements

    >> A(1,1) = c/bA =0.2500 2.0000 3.0000

    4.0000 5.0000 6.00007.0000 8.0000 9.0000

  • 8/2/2019 W6 MATLAB Fundamentals

    30/62

    March 20, 2012 Copyright Box Hill Institute

    Array Operations

    Array operations are the element byelement arithmetic operation

    The normal matrix operations(*,\,/,^)preceded by (.) represents an arrayoperation

    ^ represents raising to the power Addition and Subtraction operations are

    the same for a matrix and an array

  • 8/2/2019 W6 MATLAB Fundamentals

    31/62

    March 20, 2012 Copyright Box Hill Institute

    Array Operations For two matrices with the same dimensions

    A1=[2 7 6;8 9 10]; and B1=[6 4 3;2 3 4]; C1=A1.*B1 will result in C1 =

    12 28 18

    16 27 40 For two matrices with the same dimensions

    R1=[7 3 5]; and S1=[2 3 4]; Q1=R1.^S1 will result in Q1=

    49 9 625

    Q2=R1.^2 will result in Q2=49 9 25

    Q3=2.^S1 will result in Q3=4 8 16

  • 8/2/2019 W6 MATLAB Fundamentals

    32/62

    March 20, 2012 Copyright Box Hill Institute

    Array Operations

    For two matrices with the same dimensionsA1=[2 7 6;8 9 10]; and B1=[6 4 3;2 3 4];

    D1=A1./B1 will result in

    D1= 0.3333 1.7500 2.00004.0000 3.0000 2.5000

    E1=A1.\B1 will result inE1=

    3.0000 0.5714 0.50000.2500 0.3333 0.4000

  • 8/2/2019 W6 MATLAB Fundamentals

    33/62

    March 20, 2012 Copyright Box Hill Institute

    Complex Numbers

    Numbers of the form z=3+4i are calledcomplex numbers

    In MATLAB the above number isrepresented as z=3+4*I

    There are no spaces around the + sign.

    This is because, if there is a spaceMATLAB considers it as two differentnumbers

  • 8/2/2019 W6 MATLAB Fundamentals

    34/62

    March 20, 2012 Copyright Box Hill Institute

    Complex Numbers

    produces the conjugate transpose

    For a matrix x=1+2*i 2+1*i

    3-1*i 4+5*i xt=x will result in xt= 1-2*i 3+1*i

    2-1*i 4-5*i

    . produces the unconjugate transpose xut=x. will result in xt= 1+2*i 3-1*i

    2+1*i 4+5*i

  • 8/2/2019 W6 MATLAB Fundamentals

    35/62

    March 20, 2012 Copyright Box Hill Institute

    Generating Vectors, Iterations

    A very important operator in MATLAB is thecolon symbol (:)

    It can be used- to create vectors and matrices

    -to specify submatrices and vectors- to perform iterations

    Syntaxv_variable = i_val: inc/dec: f_val

    v_variable is the vector variablei_val is the initial valuef_val is the final valueinc/dec is the increment or decrement

  • 8/2/2019 W6 MATLAB Fundamentals

    36/62

    March 20, 2012 Copyright Box Hill Institute

    Generating Vectors - Examples

    v1= 1:8 will generatev1=

    1 2 3 4 5 6 7 8 v2= 4:-0.5:1 will generate

    v2=4.0 3.5 3.0 2.5 2.0 1.5 1.0

    v3= [(5:-1:0);(8:0.2:9)] will generatev3=

    5.0 4.0 3.0 2.0 1.0 0.08.0 8.2 8.4 8.6 8.8 9.0

  • 8/2/2019 W6 MATLAB Fundamentals

    37/62

    March 20, 2012 Copyright Box Hill Institute

    Generating Vectors

    Vectors can be generated by the MATLABfucntions linspaceand logspace

    linspacegenerates linearly evenly spacedvectors

    logspacegenerates logarithmically evenlyspaced vectors Syntax

    linspace(i_val,f_val,num)

    logspace(i_val,f_val,num)i_val is the initial valuef_val is the final valuenum is the number of elements in the vector

  • 8/2/2019 W6 MATLAB Fundamentals

    38/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statementsfor Loops

    The for loop allows a statement or a group ofstatements to repeat a fixed number of times

    Syntax forindex=i_val:inc:f_valstatement group y

    endi_valis initial value, incis increment, f_valis final value

    eg. sum = 0;for i = 1 : 100sum = sum + i^2

    endsum

  • 8/2/2019 W6 MATLAB Fundamentals

    39/62

    March 20, 2012 Copyright Box Hill Institute

    Nested for loops

    General formforindex1=i_val1 : inc1 : f_val1

    forindex2=i_val2 : inc2 : f_val2

    statement group yend

    end

    i_val1, i_val2 areinitial valuesf_val1,f_val2 arefinal values

    inc1, inc2 are respectiveincrements

  • 8/2/2019 W6 MATLAB Fundamentals

    40/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statementsifstatements

    if statements use logical operations to performsteps

    Syntaxiflogical expression1

    statement group1

    endthe statement group1 will execute if logical expression1 is true(1)else execute the statement following the end statement

    eg. if x

  • 8/2/2019 W6 MATLAB Fundamentals

    41/62

    March 20, 2012 Copyright Box Hill Institute

    Nested if statements

    General formiflogical expression1

    statement group1

    iflogical expression2statement group2

    endstatement group3

    endstatement group4

  • 8/2/2019 W6 MATLAB Fundamentals

    42/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statements Relational Operators

    When a relational operator is used to compare betweenpairs of corresponding elements, the resulting matrixconsists of 1s(true) and 0s(false)

    Operator Function

    =

    = =

    ~ =

    Less than

    Less than or equal

    Greater than

    Greater than or equal

    Equal

    Not equal

  • 8/2/2019 W6 MATLAB Fundamentals

    43/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statementsif-else

    statements if-else statement executes one set of statements

    if a logical expression is true and a different setof statements if the logical statement is false

    Syntax

    iflogical expression1

    statement group1

    elsestatement group2

    end

  • 8/2/2019 W6 MATLAB Fundamentals

    44/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statements - if-elseif-else

    statement With if-elseif-else,group of statements are executed if

    other logical expressions are false Syntax

    iflogical expression1statement group1

    elseiflogical expression2statement group2

    elseiflogical expression3statement group3else statement group3

    end

  • 8/2/2019 W6 MATLAB Fundamentals

    45/62

    March 20, 2012 Copyright Box Hill Institute

    Control Statements - while loop

    while loop allows to execute a group ofstatements as long as a specified

    condition is satisfied Syntax

    whileexpression1

    statement group1end

    statement group2

  • 8/2/2019 W6 MATLAB Fundamentals

    46/62

    March 20, 2012 Copyright Box Hill Institute

    Plotting

    Two dimensional plots are created with theplot function

    Syntax:plot(x,y)plot(xdata,ydata,symbol)

    plot(x1,y1,x2,y2,...)plot(x1,y1,symbol1,x2,y2,symbol2,...)

  • 8/2/2019 W6 MATLAB Fundamentals

    47/62

    March 20, 2012 Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    48/62

    March 20, 2012 Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    49/62

    March 20, 2012 Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    50/62

    Displayfprintf

    fprintf writes both to the screen (and to a file)eg:

    x = 0 : 5 : 10;

    fprintf( The value of x is %2.2f\n , x)

    MATLAB displays

    The value of x is 0.00The value of x is 5.00

    The value of x is 10.00

    March 20, 2012 Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    51/62

    Displaydisplay

    display(x) prints the value of a variable or

    expression x

    eg:

    x = 0 : 5 : 10;

    display(x)

    MATLAB displays 0.00 5.00 10.00

    March 20, 2012 Copyright Box Hill Institute

  • 8/2/2019 W6 MATLAB Fundamentals

    52/62

    March 20, 2012 Copyright Box Hill Institute

    MATLAB Programming

    Two different kinds of MATLAB programs

    -- MATLAB Scripts-- MATLAB Functions

    Both are basically MATLAB files with .m

    extension. Also called as m-files

  • 8/2/2019 W6 MATLAB Fundamentals

    53/62

    March 20, 2012 Copyright Box Hill Institute

    m-files

    To create an m-file

    click on FileNew select M-File from the menu

    The MATLAB Editor/Debugger screen is presented.The code is typed / edited here

    After the code is entered in the Editor/Debugger screen

    click on FileSave As (e.g., firstgraph.m ) Save

    Make sure that the file is saved in the directory that is inMATLAB's search path

  • 8/2/2019 W6 MATLAB Fundamentals

    54/62

    March 20, 2012 Copyright Box Hill Institute

    Script m-files

    Script Files

    Not really programs

    No input/output parameters

    Script variables are part of workspace

    Useful for tasks that never change Useful as a tool for documenting homework

  • 8/2/2019 W6 MATLAB Fundamentals

    55/62

    March 20, 2012 Copyright Box Hill Institute

    Script files - Example

    Enter statements for a script file sineplot.m1. Choose New. . . from File menu2. Enter lines listed below

    x=linspace(0,2*pi);y=sin(x);plot(x,y)

    3. Choose Save. . . from File menu

    Save as sineplot.m 4. Run it

    >> sineplot

  • 8/2/2019 W6 MATLAB Fundamentals

    56/62

    March 20, 2012 Copyright Box Hill Institute

    Script files - disadvantages

    Create and change variables in theworkspace

    Give no warning that workspace variableshave changed

    Can cause bugs that are hard to track

    down The algorithm cannot be used for different

    set of data

  • 8/2/2019 W6 MATLAB Fundamentals

    57/62

    March 20, 2012 Copyright Box Hill Institute

    Function m-files

    Use input and output parameters tocommunicate with other functions and thecommand window

    Use local variables that exist only while thefunction is executing. These are distinct fromvariables of the same name in the workspace orin other functions

    Input parameters allow the same calculationprocedure to be applied to different data

    Functions can call other functions

  • 8/2/2019 W6 MATLAB Fundamentals

    58/62

    March 20, 2012 Copyright Box Hill Institute

    Syntax

    function [outArgs] = funcName(inArgs)

    outArgsare enclosed in [ ]

    outArgsis a comma-separated list of variable names

    [ ] is optional if there is only one parameter functions with no outArgsare legal

    inArgsare enclosed in ( )

    inArgsis a comma-separated list of variable names functions with no inArgsare legal

  • 8/2/2019 W6 MATLAB Fundamentals

    59/62

    March 20, 2012 Copyright Box Hill Institute

    Script files - example

    twosum.m - two inputs, no output

    function twosum(x,y)% twosum Add two matrices -- comment% and print the result -- commentx+y

    >> twosum(2,2)

    ans =4

    Please note that % is used to write comments

  • 8/2/2019 W6 MATLAB Fundamentals

    60/62

    March 20, 2012 Copyright Box Hill Institute

    Script files - example

    addmult.m two inputs, two outputs

    function [s,p] = addmult(x,y)

    % Compute sum and product of two matrices

    s = x+y

    p = x*y

    >> addmult(3,2)

  • 8/2/2019 W6 MATLAB Fundamentals

    61/62

    March 20, 2012 Copyright Box Hill Institute

    Summary

    MATLAB statements are of the form

    variable = expression

    All Matlab variables are matrices Matrix and Array operations

    Control Statements

    Script files Function m-files

  • 8/2/2019 W6 MATLAB Fundamentals

    62/62

    Reference

    Gerald W. Recktenwald, NumericalMethods with MATLAB, Implementationand Application, Prentice Hall

    John O. Attia, Electronics and CircuitAnalysis using MATLAB, 2nd ed, BocaRaton, FL, CRC Press 2004

    Stephen J. Chapman, Essentials ofMATLAB Programming, Thomson Nelson