4.cece matlab programming

Upload: rashad

Post on 06-Apr-2018

240 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/3/2019 4.Cece Matlab Programming

    1/25

    MATLABCECE ACADEMIC TEAM

    Prepared by: Mohamed Farag

  • 8/3/2019 4.Cece Matlab Programming

    2/25

    Contents

    Intro to Programming

    Flow Control Conditional Statements.

    Loops.

    M-Files. Scripts Vs M-Functions

    New Data Types Multidimensional Arrays

    Structures Cell Arrays

    Error Handling

    19/7/2011Matlab CECE Academic Team

  • 8/3/2019 4.Cece Matlab Programming

    3/25

    Intro to Programming

    19/7/2011Matlab CECE Academic Team

    Design of computer programs to solve complexproblems needs to be done in a systematic mannerfrom the start to avoid time-consuming and

    frustrating difficulties later in the process. Programming Types

    Algorithms and Control Structures

    Structured ProgrammingOOP Programming

  • 8/3/2019 4.Cece Matlab Programming

    4/25

    Intro to Programming

    19/7/2011Matlab CECE Academic Team

    Algorithms and Control Structures

    an ordered sequence of precisely defined instructions thatperforms some task in a finite amount of time

    There are three main categories of algorithmic operations: Sequential Operation

    Conditional Operation

    Iterative Operation

  • 8/3/2019 4.Cece Matlab Programming

    5/25

    Intro to Programming

    19/7/2011Matlab CECE Academic Team

    Structured Programming

    A technique for designing programs in which a hierarchy ofmodules is used, each having a single entry and a single exitpoint, and in which control is passed downward through thestructure without unconditional branches to higher levels of thestructure.

    In MATLAB these modules can be built-inor user-definedfunctions.

  • 8/3/2019 4.Cece Matlab Programming

    6/25

    Intro to Programming

    19/7/2011Matlab CECE Academic Team

    OOP Programming

    Four main concepts

    Encapsulation

    Data Hiding Inheritance

    Polymorphism

  • 8/3/2019 4.Cece Matlab Programming

    7/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Logical Operation

    ~ : not

    & : And

    | : Or

    && : Short circuit And ( Scalar operation)

    || : Short Circuit Or ( Scalar Operation)

    Note:- ~:not operation is an element-wiseoperation(it affects each element in the array )

  • 8/3/2019 4.Cece Matlab Programming

    8/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Rational Operation

    ==:equal

    < : less than

    > : greater than

    = : greater than or equal

    ~= : not equal

  • 8/3/2019 4.Cece Matlab Programming

    9/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Masking

    If we want to do a specific operation on specific elementsunder certain conditions.

    We have two ways: Using loops and loop over the array and check the condition on

    each element then do your operation

    Using Masks

    Ex:

    A=[1 2 3;-1 -2 -3;4 5 6]; A(A

  • 8/3/2019 4.Cece Matlab Programming

    10/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Conditional Statements

    IF Conditionally execute statements.

    The general form of the IF statement is

    IF expression

    statements

    ELSEIF expression

    statementsELSE

    statements

    END

    Example

    if I == J

    A(I,J) = 2;

    elseif abs(I-J) == 1

    A(I,J) = -1;

    else

    A(I,J) = 0;

    end

  • 8/3/2019 4.Cece Matlab Programming

    11/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Conditional Statements

    SWITCH Switch among several cases based on expression.

    The general form of the SWITCH statement is:

    SWITCH switch_expr

    CASE case_expr,

    statement, ..., statement

    CASE {case_expr1, case_expr2, case_expr3,...}

    statement, ..., statement

    OTHERWISE,

    statement, ..., statement

    END

  • 8/3/2019 4.Cece Matlab Programming

    12/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Example

    t = [0 : 100]; x = exp(-t) . *sin(t);

    response = input( 'Type min, max , or sum .',' s ' )

    response = lower ( response ) ;

    switch response

    case min

    minimum = min (xl

    Case max

    maximum = max (x)

    case sum

    total = sum(x)

    otherwise

    disp ( ' You have not entered a proper choice . )

    End

  • 8/3/2019 4.Cece Matlab Programming

    13/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    Loops For Repeat statements a specific number of times.

    The general form of a FOR statement is:

    FOR variable = expr, statement, ..., statement END

    Example

    for R = 1:N

    for C = 1:N

    A(R,C) = 1/(R+C-1);

    end

    end

  • 8/3/2019 4.Cece Matlab Programming

    14/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    WHILE Repeat statements an indefinite number of times.

    The general form of a WHILE statement is:

    WHILE expression

    statements

    END

    Ex:- x = 1

    while x

  • 8/3/2019 4.Cece Matlab Programming

    15/25

    Flow Control

    19/7/2011Matlab CECE Academic Team

    The break statement. A while loop can be terminated with thebreak statement, which passes control to the first statementafter the corresponding end. The break statement can also beused to exit a for loop.

    The continue statement can also be used to exit a for loop to

    pass immediately to the next iteration of the loop, skipping theremaining statements in the loop.

  • 8/3/2019 4.Cece Matlab Programming

    16/25

    M-Files.

    19/7/2011Matlab CECE Academic Team

    M-Files can be used to write Scripts & Functions

    Functions

    need input & output arguments

    Variables are local Scripts

    Variables are global

    Can be called by writing its name in the command window

  • 8/3/2019 4.Cece Matlab Programming

    17/25

    M-Files.

    19/7/2011Matlab CECE Academic Team

    Functions:-

    Style

    Function [Outputs]=function_name(inputs)

    % discription of the Function it will be displayed when youuse help function_name

    Function body

  • 8/3/2019 4.Cece Matlab Programming

    18/25

    M-Files.

    19/7/2011Matlab CECE Academic Team

    Functions Types:-

    Primary Function

    Ex:-

    function [mean,stdev] = stat(x) %STAT Interesting statistics.

    n = length(x);

    mean = sum(x) / n;

    stdev = sqrt(sum((x - mean).^2)/n);

  • 8/3/2019 4.Cece Matlab Programming

    19/25

    M-Files.

    19/7/2011Matlab CECE Academic Team

    Functions Types:- Subfunctions:-

    Function defined in the same primary function file and used inside it. itis not visible outside the file.

    Ex:- function [mean,stdev] = stat(x) %STAT Interesting statistics. n = length(x); mean = avg(x,n); stdev = sqrt(sum((x-avg(x,n)).^2)/n);

    %------------------------- function mean = avg(x,n) %AVG subfunction mean = sum(x)/n

  • 8/3/2019 4.Cece Matlab Programming

    20/25

    New Data Types

    19/7/2011Matlab CECE Academic Team

    Multidimensional Array

    It is an array but with more than two subscripts

    Ex:- A=ones(3,5,2);

    Used for 3D data like:

    Temperature in a room

    A sequence of Matrix

  • 8/3/2019 4.Cece Matlab Programming

    21/25

    New Data Types

    19/7/2011Matlab CECE Academic Team

    Cell Array

    A cell array is a collection of containers called cells inwhich you can store different types of dataSome

    functions Cell:create empty cell array

    Celldisp:display cell array

    Cellplot:display grahpical representation of cell array

  • 8/3/2019 4.Cece Matlab Programming

    22/25

    New Data Types

    19/7/2011Matlab CECE Academic Team

    Structures

    Multidimensional arrays accessed by textual fielddesignators

    Ex:- S.name=mohamed;

    S.score=83;

    S.grade=A+;

    Or :- s=struct(name,mohamed,score,83,grade,A+);

  • 8/3/2019 4.Cece Matlab Programming

    23/25

    Error Handling

    19/7/2011Matlab CECE Academic Team

    Try- Catch Ex:-

    function matrixMultiply(A, B)

    try

    A * B

    catch err = lasterror;

    if(strfind(err.message, 'Inner matrix dimensions'))

    disp('** Wrong dimensions for matrix multiply')

    Else

    if(strfind(err.message, 'not defined for values of class'))

    disp('** Both arguments must be double matrices') end

    end

    end

  • 8/3/2019 4.Cece Matlab Programming

    24/25

    References

    [1] Using Matlab version 6 , Mathworks

    [2] Introduction to Matlab 7 for engineers, William J

    [2] David F. Griffiths , An Introduction to Matlab.

    [3] Matlab Help.

    19/7/2011Matlab CECE Academic Team

  • 8/3/2019 4.Cece Matlab Programming

    25/25

    THANK YOU!Prepared by: Mohamed Farag

    Email: [email protected]