w8 matlab programming (edited)

Upload: marian-celeste

Post on 14-Apr-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 w8 MATLAB Programming (Edited)

    1/19

    Introduction to MATLAB

    Programming

  • 7/27/2019 w8 MATLAB Programming (Edited)

    2/19

    Programming - 2Introduction to MATLAB

    Programs are contained in m-files

    - Plain files not binary files produced by word- Files must have an .m extension m-files must be in path When you call an M-file function from either the command line

    or

    from within another M-file, MATLAB parses the function andstores it

    in memory.

    This prevents MATLAB from having to reparse a function eachtime you call it during a session.

    The compiled function remains in memory until you clear itusing the

    CLEAR command, or until you quit MATLAB.

  • 7/27/2019 w8 MATLAB Programming (Edited)

    3/19

    Programming - 3Introduction to MATLAB

    TWO TYPES OF M-FILES

    1. Scripts-Automate long sequences of commands

    2. Functions

    - provide extensibility to MATLAB. They allow you to add newfunctions to the existing functions

  • 7/27/2019 w8 MATLAB Programming (Edited)

    4/19

    Programming - 4Introduction to MATLAB

    Script M-files

    - Standard ASCII text files- Contains series of MATLAB expressions stored together in apredefined sequence.

    - Saved with an .m extension and are called by simply typingthe filename without the extension in the command window.

    Downside of u sing Scr ip t f iles:

    - All variables created are added to the workspace.

    - Variables already existing in the workspace may be overwritten

  • 7/27/2019 w8 MATLAB Programming (Edited)

    5/19

    Programming - 5Introduction to MATLAB

    Functions M-files

    - Functions are subprograms- Use input and output parameters to communicate with otherfunction

    Differences between Scr ipt & Funct ion M-f i les:

    Structural Syntax - must contain keyword FUNCTION at thebeginning of the first line

    Function Workspaces, Inputs & Outputs- A function does not work with variables in the base MATLAB

    workspace.

    As a result, all information to be transferred between theMATLAB workspace and a function must bepassedas inputs &outputs.

  • 7/27/2019 w8 MATLAB Programming (Edited)

    6/19

    Programming - 6Introduction to MATLAB

    function y = mean(x)

    % MEAN Average or mean value.

    % For vectors, MEAN(x) returns the mean value.

    % For matrices, MEAN(x) is a row vector

    % containing the mean value of each column.

    [m,n] = size(x);

    if m == 1m = n;

    end

    y = sum(x)/m;

    Structure of a Function M-file

    Keyword: function Function Name (same as file name .m)

    Output Argument(s) Input Argument(s)

    Online Help

    MATLAB

    Code

    output_value = mean(input_value) Command Line Syntax

  • 7/27/2019 w8 MATLAB Programming (Edited)

    7/19

    Programming - 7Introduction to MATLAB

    Inputs to Functions : input function can be used to prompt the user for numeric or

    string input

    Input parameters to functions are preferred

    Text output from functions:

    dispfunction for simple output

    fprintffunction for formatted output.

    Text Input and Output

  • 7/27/2019 w8 MATLAB Programming (Edited)

    8/19

    Programming - 8Introduction to MATLAB

    Prompting for User Input

    The input function can be used to prompt the user for numericor string input.

    Syntax: entry = input (Text Display)

    entry = input (Text Display, s )

    Examples:

    >> x = input (Enter a value for x)

    Enter a value for x %serves as a prompt on the screen thatsignifies that MATLAB is awaiting for user

    response.

    >>yourName = input (Enter your name,s); %like the previouscommand but will return the entered string as atext variable rather than as a variable name ornumerical value.

  • 7/27/2019 w8 MATLAB Programming (Edited)

    9/19

    Programming - 9Introduction to MATLAB

    Text Output with disp

    - Output to the command window is achieved with the dispfunction

    disp Simple to use. Provides limited control over appearance ofoutput.

    Syntax: disp (variable)

    disp (text string)

    Examples:

    >>poly_roots = [ 1.2400 2.5600 5.6400 ]

    >> disp ('The roots of the cubic polynomial are')

    The roots of the cubic polynomial are

    >> disp (poly_roots)

    1.2400 2.5600 5.6400

    >> disp ('The roots of the cubic polynomial are') , disp (poly_roots)

    The roots of the cubic polynomial are

    1.2400 2.5600 5.6400

  • 7/27/2019 w8 MATLAB Programming (Edited)

    10/19

    Programming - 10Introduction to MATLAB

    disp Simple to use. Provides limited control over appearance ofoutput.

    Syntax: disp (variable)

    disp (text string)

    Examples:>>poly_roots = [ 1.2400 2.5600 5.6400 ]

    >> disp ('The roots of the cubic polynomial are')

    The roots of the cubic polynomial are

    >> disp (poly_roots)

    1.2400 2.5600 5.6400>> disp ('The roots of the cubic polynomial are') , disp (poly_roots)

    The roots of the cubic polynomial are

    1.2400 2.5600 5.6400

  • 7/27/2019 w8 MATLAB Programming (Edited)

    11/19

    Programming - 11Introduction to MATLAB

    Relational and Logical Operators

    Operators Description< Less than

    > Greater than

    = Greater than or equal to

    ~= Not equal to

    == Equal to

    & AND

    | OR

    ~ NOT

    xor Exclusive OR

    -Will output 1 for true conditions and 0 for false conditions

    - Can be used with scalars and arrays

  • 7/27/2019 w8 MATLAB Programming (Edited)

    12/19

    Programming - 12Introduction to MATLAB

    Other Logical built-in functions

    Operators Description

    all (A)

    Returns true if all elements in a vectorA aretrue.

    Returns false if one or more elements arefalse

    any (A)Returns 1 (true) if any element in a vectorAis true (nonzero).

    Returns 0 (false) if all elements are false.

    find (A)IfA is a vector, returns the indices of thenonzero elements

    find (A>d)IfA is a vector, returns the address of theelements that are larger than d (anyrelational operator can be used)

  • 7/27/2019 w8 MATLAB Programming (Edited)

    13/19

    Programming - 13Introduction to MATLAB

    SUMMARY

    Relational operators involve comparison of two values.

    The result of a relational operation is a logical (True/False)

    value.

    Logical operators combine (or negate) logical values to

    produce another logical value.

  • 7/27/2019 w8 MATLAB Programming (Edited)

    14/19

    Programming - 14Introduction to MATLAB

    Example : Analysis of temp data

    The following were the daily max temp in (F) in Washington during

    the month of April,2002.58 73 73 53 30 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80

    59 69 56 64 63 66 64 74 63 69.

    Use relational logical operators to determ ine the fol lowing:

    a. The number of days the temp was above 75

    b. the number of days the temp was between 65 and 80

  • 7/27/2019 w8 MATLAB Programming (Edited)

    15/19

    Programming - 15Introduction to MATLAB

    Examples:

    >> r = [ 8 12 9 4 23 19 10 ]r =

    8 12 9 4 23 19 10

    >>s=r>t=r(s) % use sfor addresses in vectorrto create a vector t

    t =

    8 9 4 10

    >> w=r(r

  • 7/27/2019 w8 MATLAB Programming (Edited)

    16/19

    Programming - 16Introduction to MATLAB

    fprint & sprintf

  • 7/27/2019 w8 MATLAB Programming (Edited)

    17/19

    Programming - 17Introduction to MATLAB

    Example

    function areacircle

    r=input('Give radius of circle: ');

    a=pi*r^2;

    fprintf('The area of circle with radius %.2f is %.6f\n',r,a);

    end

  • 7/27/2019 w8 MATLAB Programming (Edited)

    18/19

    Programming - 18Introduction to MATLAB

    Flow control - selection

    The if-elseif-else construction

    if elseif elseend

    if height>170disp(tall)

    elseif height

  • 7/27/2019 w8 MATLAB Programming (Edited)

    19/19

    Programming - 19Introduction to MATLAB

    Example:

    The following script file will returns one of the following messages:teenager, adult and senior given as the input of the variableage.

    function check_age

    age=input('Enter your age: ')

    if age