Extra Material Matlab

Embed Size (px)

Citation preview

  • 7/25/2019 Extra Material Matlab

    1/124

    1

    MATLAB Basics The Reference

    Durga Lal Shretha

    Yunqing Xuan

  • 7/25/2019 Extra Material Matlab

    2/124

    Part-1

    2

  • 7/25/2019 Extra Material Matlab

    3/124

    copyright Durga Lal Shrestha

    3

    Entering Matrices

    Enter an explicit list of elements

    A = [10 20 30; 1 2 3; 2 4 8];

    Load matrices from external data files

    A = load('mymagic.txt'); % you need to create the file before

    hand

    Generate matrices using built-in functions.

    A = magic(4)

    Create matrices with your own functions in M-files.

    A = myfunction(input1,input2) %

  • 7/25/2019 Extra Material Matlab

    4/124

    copyright Durga Lal Shrestha

    4

    Enter an explicit list of elements

    Separate the elements of a row with blanks or commas.

    Use a semicolon, ; , to indicate the end of each row.

    Surround the entire list of elements with square brackets,[ ].

  • 7/25/2019 Extra Material Matlab

    5/124

    copyright Durga Lal Shrestha

    5

    Subscripts

    Using parenthesis (row,column)

    e.g. the element in row I and column j of A = A(i,j)

  • 7/25/2019 Extra Material Matlab

    6/124

    copyright Durga Lal Shrestha

    6

    The colon operator :

    To generator vector

    Unit steps startvalue:endvalue

    Nonunit steps with the given increment

    startvalue:stepvalue:endvalue

  • 7/25/2019 Extra Material Matlab

    7/124

    copyright Durga Lal Shrestha

    7

    The colon operator : To select subarrays (portion ofmatrix)

  • 7/25/2019 Extra Material Matlab

    8/124

    copyright Durga Lal Shrestha

    8

    Selecting subarrays: Common errors

  • 7/25/2019 Extra Material Matlab

    9/124

    copyright Durga Lal Shrestha

    9

    Generating Matrices

    Zeros

    Ones

  • 7/25/2019 Extra Material Matlab

    10/124

    copyright Durga Lal Shrestha

    10

    Generating Matrices

    rand - uniform random number between 0 and 1

    randn normal random number(0,1)

  • 7/25/2019 Extra Material Matlab

    11/124

    copyright Durga Lal Shrestha

    11

    Concatenation

    Concatenation is the process of joining small matrices to

    make bigger ones. Using []

  • 7/25/2019 Extra Material Matlab

    12/124

    copyright Durga Lal Shrestha

    12

    Deleting Rows and Columns

  • 7/25/2019 Extra Material Matlab

    13/124

    copyright Durga Lal Shrestha

    13

    Reordering data

  • 7/25/2019 Extra Material Matlab

    14/124

    copyright Durga Lal Shrestha

    14

    Changing data

  • 7/25/2019 Extra Material Matlab

    15/124

    copyright Durga Lal Shrestha

    15

    Arrays

    Scalars: zero dimensional or 1 by 1

    S = 2.3

    Vector: one dimensional (i.e. n by 1 or 1 by n)

    V = [1 4 3 5 6]

    Matrix: two dimensional (i.e. m by n)

    M = [1 2 3

    4 5 6]

    Array: N dimensional

  • 7/25/2019 Extra Material Matlab

    16/124

    copyright Durga Lal Shrestha

    16

    Multidimensional Array

  • 7/25/2019 Extra Material Matlab

    17/124

    copyright Durga Lal Shrestha

    17

    size, length, ndims

  • 7/25/2019 Extra Material Matlab

    18/124

    copyright Durga Lal Shrestha

    18

    Matrix Algebra

    Addition

    Subtraction

    Matrix multiplication

    Array multiplication

  • 7/25/2019 Extra Material Matlab

    19/124

    copyright Durga Lal Shrestha

    19

    Addition

    +

    A and B must have the same size, unless one of them isa scalar

  • 7/25/2019 Extra Material Matlab

    20/124

    copyright Durga Lal Shrestha

    20

    Subtraction

    -

    A and B must have the same size, unless one of them isa scalar

  • 7/25/2019 Extra Material Matlab

    21/124

    copyright Durga Lal Shrestha

    21

    Matrix Multiplication

    *

    For nonscalar A and B, the number of columns of A must

    equal the number of rows of B. A scalar can multiply a matrix of any size.

  • 7/25/2019 Extra Material Matlab

    22/124

    copyright Durga Lal Shrestha

    22

    Array Multiplication

    .*

    element-by-element product of the arrays A and B

    A and B must have the same size, unless one of them isa scalar

  • 7/25/2019 Extra Material Matlab

    23/124

    23

    Part 2

  • 7/25/2019 Extra Material Matlab

    24/124

    copyright Durga Lal Shrestha

    24

    Expressions

    Variables

    Numbers

    Operators

    Functions

  • 7/25/2019 Extra Material Matlab

    25/124

    copyright Durga Lal Shrestha

    25

    Numbers

    MATLAB uses conventional decimal notation, with anoptional decimal point and leading plus or minus sign, fornumbers.

    Scientific notation uses the letter e to specify a power-of-ten scale factor.

    Imaginary numbers use either i or j as a suffix.

    Some examples of legal numbers are

    3 -99 0.0001

    9.6397238 1.60210e-20 6.02252e23

    1i -3.14159j 3e5i

  • 7/25/2019 Extra Material Matlab

    26/124

    copyright Durga Lal Shrestha

    26

    Functions

    MATLAB provides a large number of standard elementary

    mathematical functions, including abs, sqrt, exp, and sin. For a list of the mathematical functions, type

    help elfun for Elementary functions (Trigonometric, Exponential,Complex, and Rounding and remainder)

    help specfun for Specialized math functions (e.g. gamma, factor

    etc) help elmat for Elementary matrices and matrix manipulation (e.g.

    zeros, size, reshape, magic etc.)

    Built-in functions are part of the MATLAB core so they

    are very efficient, but the computational details are notreadily accessible (e.g sin, sqrt etc).

    Other functions, like gamma and sinh, are implementedin M-files and you can see the code and even modify it ifyou want.

  • 7/25/2019 Extra Material Matlab

    27/124

    copyright Durga Lal Shrestha

    27

    Data Types

  • 7/25/2019 Extra Material Matlab

    28/124

    copyright Durga Lal Shrestha

    28

    Numeric Types

    Numeric data types in MATLAB include signed andunsigned integers, and single- and double-precisionfloating-point numbers.

    Integers

  • 7/25/2019 Extra Material Matlab

    29/124

    copyright Durga Lal Shrestha

    29

    Floating-Point Numbers

    Double-Precision Floating Point

    MATLAB default data type

    64 bits, large memory and more precision

    Single-Precision Floating Point

    32 bits, less memory and less precision

  • 7/25/2019 Extra Material Matlab

    30/124

    copyright Durga Lal Shrestha

    30

    Complex Numbers

    >> x = 2 + 3i

    x =

    2.0000+3.0000i

    >> z = complex(2.1, pi)

    z =2.1000 + 3.1416i

    >> zr = real(z)

    zr =

    2.1000

    >> zi = imag(z)

    zi =

    3.1416

  • 7/25/2019 Extra Material Matlab

    31/124

    copyright Durga Lal Shrestha

    31

    Infinity and NaN

    Infinity

    x = log(0);

    >> isinf(x)

    ans =

    1

    NaN (Not a Number)

    MATLAB represents values that are not real or complex numberswith a special value called NaN

    0/0 and inf/inf

  • 7/25/2019 Extra Material Matlab

    32/124

    copyright Durga Lal Shrestha

    32

    Display Format for Numeric Values

    By default, MATLAB displays numeric output as 5-digitscaled, fixed-point values.

    You can change the way numeric values are displayed toany of the following:

    5-digit scaled fixed point, floating point, or the best of the two

    15-digit scaled fixed point, floating point, or the best of the two

    A ratio of small integers

    Hexadecimal (base 16)

    Bank notation

    Format short (formatting to 5 digit) Check the current format setting:

    get(0, 'format')

    ans =

    short

  • 7/25/2019 Extra Material Matlab

    33/124

    copyright Durga Lal Shrestha

    33

    Format

    x = [4/3 1.2345e-6]

    x =

    1.3333 0.0000

    format short e

    xx =

    1.3333e+000 1.2345e-006

    format long

    xx =

    1.33333333333333 0.00000123450000

  • 7/25/2019 Extra Material Matlab

    34/124

    copyright Durga Lal Shrestha

    34

    Logical Types

    The logical data type represents a logical true or falsestate using the numbers 1 and 0, respectively.

    (5 * 10) > 40

    ans =

    1

    Vector of logical [30 40 50 60 70] > 40

    ans =

    0 0 1 1 1

  • 7/25/2019 Extra Material Matlab

    35/124

    copyright Durga Lal Shrestha

    35

    Logical Types

    Using Logicals in Conditional Statements

    str = 'Hello';

    if ~isempty(str) && ischar(str)

    sprintf('Input string is ''%s''', str)

    end

    ans =

    Input string is 'Hello'

  • 7/25/2019 Extra Material Matlab

    36/124

    copyright Durga Lal Shrestha

    36

    Logical Indexing

  • 7/25/2019 Extra Material Matlab

    37/124

    37

    Part 3

  • 7/25/2019 Extra Material Matlab

    38/124

  • 7/25/2019 Extra Material Matlab

    39/124

    copyright Durga Lal Shrestha

    39

    Characters and Strings

    Padding.

  • 7/25/2019 Extra Material Matlab

    40/124

    copyright Durga Lal Shrestha

    40

    Structure Array

    Structures are MATLAB arrays with named data

    containers called fields.

    The fields of a structure can contain any kind of data.

  • 7/25/2019 Extra Material Matlab

    41/124

    copyright Durga Lal Shrestha

    41

    Structure Array

  • 7/25/2019 Extra Material Matlab

    42/124

    copyright Durga Lal Shrestha

    42

    Structure Array

    strArray = struct('field1',val1,'field2',val2, ...)

    ll

  • 7/25/2019 Extra Material Matlab

    43/124

    copyright Durga Lal Shrestha

    43

    Cell Arrays

    A cell array provides a storage mechanism for dissimilar

    kinds of data.

    C ll

  • 7/25/2019 Extra Material Matlab

    44/124

    copyright Durga Lal Shrestha

    44

    Cell Arrays

    Use { } for constructing, concatenating and indexing

    A = {[1 4 3; 0 5 8; 7 2 9], 'Anne Smith'; 3+7i, -pi:pi/4:pi}

    A =

    [3x3 double] 'Anne Smith'[3.0000 + 7.0000i] [1x9 double]

  • 7/25/2019 Extra Material Matlab

    45/124

    45

    Part 4

    Data Import and Export

    D t

  • 7/25/2019 Extra Material Matlab

    46/124

    copyright Durga Lal Shrestha

    46

    Supported Types of Data

    Binary data from a MATLAB session

    Text data

    Graphics files

    Audio and audio/video data

    spreadsheets Data from the system clipboard

    Information from the internet

  • 7/25/2019 Extra Material Matlab

    47/124

    Load command

  • 7/25/2019 Extra Material Matlab

    48/124

    copyright Durga Lal Shrestha

    48

    Load command

  • 7/25/2019 Extra Material Matlab

    49/124

    49

    Part 5

    MATLAB Graphics

    Matlab Graphics

  • 7/25/2019 Extra Material Matlab

    50/124

    copyright Durga Lal Shrestha

    50

    Matlab Graphics

    Plotting Process

    Graph Components

    Figure Tools

    Arranging Graphs Within a Figure

    Selecting Plot Types

    Plotting Process

  • 7/25/2019 Extra Material Matlab

    51/124

    copyright Durga Lal Shrestha

    51

    Plotting Process

    Use plotting tools to create graphs interactively.

    plottools

    Use the command interface to enter commands in theCWD or create plotting programs.

    x = 0:0.1:2*pi;

    plot(x,sin(x)

    Graph Components

  • 7/25/2019 Extra Material Matlab

    52/124

    copyright Durga Lal Shrestha

    52

    Graph Components

    Figure

    Plot

    Axis

    Data

    Figure Tools: Figure Palette Plot Browser and Property Editor

  • 7/25/2019 Extra Material Matlab

    53/124

    copyright Durga Lal Shrestha

    53

    Figure Tools: Figure Palette, Plot Browser and Property Editor

    Arranging Graphs Within a Figure

  • 7/25/2019 Extra Material Matlab

    54/124

    copyright Durga Lal Shrestha

    54

    Arranging Graphs Within a Figure

    Selecting Plot Types

  • 7/25/2019 Extra Material Matlab

    55/124

    copyright Durga Lal Shrestha

    55

    Selecting Plot Types

    Basic Plotting Functions

  • 7/25/2019 Extra Material Matlab

    56/124

    copyright Durga Lal Shrestha

    56

    Basic Plotting Functions

    Plotting Steps

  • 7/25/2019 Extra Material Matlab

    57/124

    copyright Durga Lal Shrestha

    57

    Plotting Steps

    Plotting Multiple Data Sets in One Graph

  • 7/25/2019 Extra Material Matlab

    58/124

    copyright Durga Lal Shrestha

    58

    Plotting Multiple Data Sets in One Graph

    plot(x,y)

    hold on

    plot(x,sin(x-0.5),'r')

    plot(x,sin(x-0.25),'g')

    OR

    Line Styles and Colors

  • 7/25/2019 Extra Material Matlab

    59/124

    copyright Durga Lal Shrestha

    59

    Line Styles and Colors

    plot(x,y,'--rs','LineWidth',2,...'MarkerEdgeColor','k',...'MarkerFaceColor','g',...

    'MarkerSize',10)

    Contour plots

  • 7/25/2019 Extra Material Matlab

    60/124

    copyright Durga Lal Shrestha

    60

    Contour plots

    Twenty contours of the peaks function

    -3 -2 -1 0 1 2 3-3

    -2

    -1

    0

    1

    2

    3

    Contour

  • 7/25/2019 Extra Material Matlab

    61/124

    copyright Durga Lal Shrestha

    61

    Contour

    Contour3

  • 7/25/2019 Extra Material Matlab

    62/124

    copyright Durga Lal Shrestha

    62

    Contour3

    -3-2

    -10

    12

    3

    -2

    0

    2

    -10

    -5

    0

    5

    10

    Twenty Contours of the peaks Function

  • 7/25/2019 Extra Material Matlab

    63/124

    Plotting Matrix Data

  • 7/25/2019 Extra Material Matlab

    64/124

    copyright Durga Lal Shrestha

    64

    Plotting Matrix Data

    Plotting Image Data

  • 7/25/2019 Extra Material Matlab

    65/124

    copyright Durga Lal Shrestha

    65

    Plotting Image Data

    100 200 300 400 500

    100

    200

    300

    400

    500

    600

    Surface plot

  • 7/25/2019 Extra Material Matlab

    66/124

    copyright Durga Lal Shrestha

    66

    Surface plot

    -10

    -5

    0

    5

    10

    -10

    -5

    0

    5

    10-0.5

    0

    0.5

    1

    Surface plot

  • 7/25/2019 Extra Material Matlab

    67/124

    copyright Durga Lal Shrestha67

    Surface plot

    Creating Stream Particle Animations

  • 7/25/2019 Extra Material Matlab

    68/124

    copyright Durga Lal Shrestha68

    Creating Stream Particle Animations

  • 7/25/2019 Extra Material Matlab

    69/124

    69

    Part 6

    Program components

    Basic Program Components

  • 7/25/2019 Extra Material Matlab

    70/124

    copyright Durga Lal Shrestha70

    Basic Program Components

    Variables

    Keywords

    Special Values

    Operators

    MATLAB Expressions Program Control Statements

    Symbol Reference

    MATLAB Functions

    Variables

  • 7/25/2019 Extra Material Matlab

    71/124

    copyright Durga Lal Shrestha 71

    Variables

    A MATLAB variable is essentially a tag that you assign to

    a value while that value remains in memory. Local Variables (local to functions)

    Global Variables (share a single copy of a variable)

    Persistent Variables (retained value from one function call to the

    next)

    Naming Variables

  • 7/25/2019 Extra Material Matlab

    72/124

    copyright Durga Lal Shrestha 72

    a g a ab es

    Any combination of characters, numbers and underscore

    (starting with characters) Case sensitive

    e.g. Variable1, variable1 are different

    MATLAB uses only the first N characters of the name and

    ignores the rest, where N = namelengthmax(63)

    The genvarnamefunction can be useful in creatingvariable names that are both valid and unique.

    E.g. four similar variable name strings that do not conflict witheach other: v = genvarname({'A', 'A', 'A', 'A'})

    v = 'A' 'A1' 'A2' 'A3'

    Keywords

  • 7/25/2019 Extra Material Matlab

    73/124

    copyright Durga Lal Shrestha 73

    y

    MATLAB reserves certain words for its own use as

    keywords of the language. To list the keywords, type iskeyword

    'break 'case'

    'catch 'continue'

    'else 'elseif' 'end 'for'

    'function 'global'

    'if 'otherwise'

    'persistent 'return' 'switch 'try'

    'while'

    Special Values

  • 7/25/2019 Extra Material Matlab

    74/124

    copyright Durga Lal Shrestha 74

    p

    Operators

  • 7/25/2019 Extra Material Matlab

    75/124

    copyright Durga Lal Shrestha 75

    p

    Arithmetic Operators

    Relational Operators

    Logical Operators

    Arithmetic Operators

  • 7/25/2019 Extra Material Matlab

    76/124

    copyright Durga Lal Shrestha 76

    p

    Relational Operators

  • 7/25/2019 Extra Material Matlab

    77/124

    copyright Durga Lal Shrestha 77

    p

    Logical Operators

  • 7/25/2019 Extra Material Matlab

    78/124

    copyright Durga Lal Shrestha 78

    g p

    Operator Precedence

  • 7/25/2019 Extra Material Matlab

    79/124

    copyright Durga Lal Shrestha 79

    p

    Parentheses ()

    Transpose (.'), power (.^), complex conjugate transpose (), matrixpower (^)

    Unary plus (+), unary minus (-), logical negation (~)

    Multiplication (.*), right division (./), left division (.\), matrixmultiplication (*), matrix right division (/), matrix left division (\)

    Addition (+), subtraction (-)

    Colon operator (:)

    Less than (=), equal to (==), not equal to (~=)

    Element-wise AND (&) Element-wise OR (|)

    Short-circuit AND (&&)

    Short-circuit OR (||)

  • 7/25/2019 Extra Material Matlab

    80/124

    Program Control Statements

  • 7/25/2019 Extra Material Matlab

    81/124

    copyright Durga Lal Shrestha 81

    Program control is divided into four categories:

    Conditional Control if, switch

    Loop Control for, while, continue, break

    Error Control try, catch

    Program Termination return

    Conditional Control if

  • 7/25/2019 Extra Material Matlab

    82/124

    copyright Durga Lal Shrestha 82

    if logical_expression

    statementsend

    if logical_expression1

    statements1

    elseif logical_expression2

    statements2

    else

    statements3

    end

    Conditional Control switch, case, and otherwise

  • 7/25/2019 Extra Material Matlab

    83/124

    copyright Durga Lal Shrestha 83

    Loop Control for

  • 7/25/2019 Extra Material Matlab

    84/124

    copyright Durga Lal Shrestha 84

    The for loop executes a statement or group of

    statements a predetermined number of times.

    for index = start:increment:end

    statements

    end

    Loop Control while

  • 7/25/2019 Extra Material Matlab

    85/124

    copyright Durga Lal Shrestha 85

    The while loop executes a statement or group of

    statements repeatedly as long as the controllingexpression is true.

    Loop Control continue

  • 7/25/2019 Extra Material Matlab

    86/124

    copyright Durga Lal Shrestha 86

    The continue statement passes control to the next

    iteration of the for or while loop in which it appears,skipping any remaining statements in the body of theloop.

    Loop Control break

  • 7/25/2019 Extra Material Matlab

    87/124

    copyright Durga Lal Shrestha 87

    The break statement terminates the execution of a for

    loop or while loop.

    Error Control- try and catch

  • 7/25/2019 Extra Material Matlab

    88/124

    copyright Durga Lal Shrestha 88

    Error control statements provide a way for you to take

    certain actions in the event of an error.

    Program Termination return

  • 7/25/2019 Extra Material Matlab

    89/124

    copyright Durga Lal Shrestha 89

    Program termination control enables you to exit from

    your program at some point prior to its normaltermination point.

  • 7/25/2019 Extra Material Matlab

    90/124

    90

    Part 7

    Symbol Reference

    Symbol Reference

  • 7/25/2019 Extra Material Matlab

    91/124

    copyright Durga Lal Shrestha 91

    At @ for fhandle = @myfun

    Colon :

    Comma ,

    Curly Braces { }

    Dot .

    Dot-Dot-Dot (Ellipsis) ... on page 3-103

    Exclamation Point ! for Shell Escape (e.g. !dir) Parentheses ( ) for array indexing and function input arguments

    Percent % and Percent-Brace %{ %}

    Semicolon ;

    Single Quotes for character and string

    Space Character

    Square Brackets [ ]

    Colon :

  • 7/25/2019 Extra Material Matlab

    92/124

    copyright Durga Lal Shrestha 92

    Numeric Sequence Range and Step

    Indexing Range Specifier

    Conversion to Column Vector

    Preserving Array Shape on Assignment

    Comma ,

  • 7/25/2019 Extra Material Matlab

    93/124

    copyright Durga Lal Shrestha 93

    Row Element Separator

    Array Index Separator

    Function Input and Output Separator

    Command or Statement Separator

    Curly Braces { }

  • 7/25/2019 Extra Material Matlab

    94/124

    copyright Durga Lal Shrestha 94

    Cell Array Constructor

    Cell Array Indexing

    Dot .

  • 7/25/2019 Extra Material Matlab

    95/124

    copyright Durga Lal Shrestha 95

    Structure Field Definition

    Object Method Specifier

    Dot-Dot-Dot (Ellipsis) ...

  • 7/25/2019 Extra Material Matlab

    96/124

    copyright Durga Lal Shrestha

    96

    Line Continuation

    Entering Long Strings

    You cannot use an ellipsis within single quotes to continue a

    string to the next line:

    Percent % and Percent-Brace %{ %}

  • 7/25/2019 Extra Material Matlab

    97/124

    copyright Durga Lal Shrestha

    97

    Single Line Comments

    Conversion Specifiers

    Block Comments

    Semicolon ;

  • 7/25/2019 Extra Material Matlab

    98/124

    copyright Durga Lal Shrestha

    98

    Array Row Separator

    Output Suppression

    Command or Statement Separator

    Space Character

  • 7/25/2019 Extra Material Matlab

    99/124

    copyright Durga Lal Shrestha

    99

    Row Element Separator

    Function Output Separator

    Square Brackets [ ]

  • 7/25/2019 Extra Material Matlab

    100/124

    copyright Durga Lal Shrestha

    100

    Array Constructor

    Concatenation

    Function Declarations and Calls

  • 7/25/2019 Extra Material Matlab

    101/124

    101

    Part 8

    M-File Programming

    Scripts and Functions

  • 7/25/2019 Extra Material Matlab

    102/124

    copyright Durga Lal Shrestha

    102

    There are two kinds of M-files.

    Scripts, which do not accept input arguments or returnarguments. They operates on data in the workspace

    Functions are M-files that can accept input arguments

    and return output arguments. Functions operate on variables within their own workspace,

    separate from the base workspace.

    The names of the M-file and of the function should be the same.

    Scripts: small exercise

  • 7/25/2019 Extra Material Matlab

    103/124

    copyright Durga Lal Shrestha

    103

    Create a file called magicrank.m

    Functions

  • 7/25/2019 Extra Material Matlab

    104/124

    copyright Durga Lal Shrestha

    104

    Create a file called fmagicrank.m

    help fmagicrank

    type mr = fmagicrank(10);

    Basic Parts of an M-File

  • 7/25/2019 Extra Material Matlab

    105/124

    copyright Durga Lal Shrestha

    105

  • 7/25/2019 Extra Material Matlab

    106/124

    Creating a Simple M-File

  • 7/25/2019 Extra Material Matlab

    107/124

    copyright Durga Lal Shrestha

    107

    1. Create an M-file using a text

    editor.function c = myfunc(a,b)

    c = sqrt((a.^2)+b.^2))

    x = 7.5;

    y = 3.342;

    z = mufunc(x,y)

    z =

    8.2109

    2. Call the M-file from the

    command line or from within

    another M-file

    Types of Functions

  • 7/25/2019 Extra Material Matlab

    108/124

    copyright Durga Lal Shrestha

    108

    Primary M-File Functions - the first function in an M-file andtypically contains the main program.

    Subfunctions - subroutines to the main function or to definemultiple functions within a single M-file.

    Nested Functions - functions defined within another

    function. They can help to improve the readability of the programand to give more flexible access to variables in the M-file.

    Anonymous Functions - provide a quick way of making a

    function from any MATLAB expression. We can compose anonymousfunctions either from within another function or at the MATLABcommand prompt.

    Overloaded Functions - useful when you need to create a

    function that responds to different types of inputs accordingly.

    Private Functions - a way to restrict access to a

    function. You can call them only from an M-file function in theparent directory.

    Constructing an Anonymous Function

  • 7/25/2019 Extra Material Matlab

    109/124

    copyright Durga Lal Shrestha

    109

  • 7/25/2019 Extra Material Matlab

    110/124

    Subfunctions

  • 7/25/2019 Extra Material Matlab

    111/124

    copyright Durga Lal Shrestha

    111

    Exercise Function

  • 7/25/2019 Extra Material Matlab

    112/124

    copyright Durga Lal Shrestha

    112

    function [Y] = filterdata(data,thres)

    [NRow,NCol]=size(data);j=1;

    Y=[];

    for i=1:NRow

    if data(i)>=thres

    Y(j)=data(i);

    j=j+1;

    end;end;

    Function Handles

  • 7/25/2019 Extra Material Matlab

    113/124

    copyright Durga Lal Shrestha

    113

    A function handle is a MATLAB value that provides a

    means of calling a function indirectly Construct a function handle in MATLAB using the at sign,

    @, before the function name.

    fhandle = @sin;

    You can call a function by means of its handle in thesame way that you would call the function using itsname.

    The syntax is fhandle(arg1, arg2, ...);

    fhandle(0.50)

    Function Functions

  • 7/25/2019 Extra Material Matlab

    114/124

    copyright Durga Lal Shrestha

    114

    One function works on another functions

    Zero finding Optimization

    Quadrature

    Ordinary differential equations

    Humps function

  • 7/25/2019 Extra Material Matlab

    115/124

    copyright Durga Lal Shrestha

    115

  • 7/25/2019 Extra Material Matlab

    116/124

    copyright Durga Lal Shrestha

    116

    hold on; plot(p,humps(p),'r*');

  • 7/25/2019 Extra Material Matlab

    117/124

    copyright Durga Lal Shrestha

    117

  • 7/25/2019 Extra Material Matlab

    118/124

    copyright Durga Lal Shrestha

    118

    Q = quadl(@humps,0,1)

    Q =29.8583

    Z = fzero(@humps,0.5)

    Z =-0.1315

    Z = fzero(@humps,1)

    Z =1.2955

    f = humps(Z)

    f =0

  • 7/25/2019 Extra Material Matlab

    119/124

    119

    Part 9: GUIDE

    Simple Graphical User Interface

    GUIDE

  • 7/25/2019 Extra Material Matlab

    120/124

    copyright Durga Lal Shrestha

    120

    GUIDE, the MATLAB graphical user interfacedevelopment environment, provides a set of tools for

    creating graphical user interfaces (GUIs).

    Simple gui example

  • 7/25/2019 Extra Material Matlab

    121/124

    copyright Durga Lal Shrestha

    121

    Simple gui example

  • 7/25/2019 Extra Material Matlab

    122/124

    copyright Durga Lal Shrestha

    122

    Simple gui example

  • 7/25/2019 Extra Material Matlab

    123/124

    copyright Durga Lal Shrestha

    123

    Simple gui example

  • 7/25/2019 Extra Material Matlab

    124/124