matlab lecture 1w03

Upload: joe1915

Post on 30-May-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Matlab Lecture 1w03

    1/22

    MATLAB - Lecture # 1

    Topics Covered:

    1. Introduction.

    2. MATLAB windows.

    3. Arithmetic operations with scalars.

    4. Working in the command window.

    5. Defining variables (scalars).

    Starting with MATLAB / Chapter 1

  • 8/14/2019 Matlab Lecture 1w03

    2/22

    MATLAB is a software for numerical computations,

    visualization and programming.The name MATLAB stands forMATrix LABoratory.

    Its basic data element is an array (explained later).

    With MATLAB you can:

    Use it as a calculator.

    Define variables and use them in calculations.

    Use built-in functions (sin, cos,max,min, etc.).

    Plot graphs.

    Write and run computer programs.

    Do symbolic calculations.

    1-4

  • 8/14/2019 Matlab Lecture 1w03

    3/22

    Command Window: Main window that opens whenMATLAB is started. It has the command prompt .

    All commands can be typed in this window. Used for

    running programs written by user.

    Figure Window: Contains graphs created by graphics

    commands. This window opens automatically.

    Editor Window: Used for writing and editing

    programs. This window is opened from the File menuin the Command Window.

    Help Window: Contains help information. This

    window is opened from the Help menu in any of the

    previous windows.

    MATLAB windows:6

  • 8/14/2019 Matlab Lecture 1w03

    4/22

    Command Window(The Command Window opens

    when MATLAB is started)

    Command prompt

    6

  • 8/14/2019 Matlab Lecture 1w03

    5/22

    Figure Window (The Figure Window opensautomatically by the plot command)

    7

  • 8/14/2019 Matlab Lecture 1w03

    6/22

    (The Editor Window is opened from the

    file menu in the Command Window)Editor Window

    7

  • 8/14/2019 Matlab Lecture 1w03

    7/22

    Help Window (The Help Window can be opened from theHelp menu of any of MATLAB windows)

    8

  • 8/14/2019 Matlab Lecture 1w03

    8/22

    WORKING IN THE COMMAND WINDOW

    To type a command the cursor must be placed

    after the command prompt (>>). Once a command is typed, and the Enter key is

    pressed, the command is executed. (Only the last

    command is executed. Everything executed before

    is unchanged) It is not possible to go back to a previous line and

    make a correction.

    A previously typed command can be recalled tothe command prompt with the up-arrow key .( )

    8-9

  • 8/14/2019 Matlab Lecture 1w03

    9/22

    ARITHMETIC OPERATIONS WITH SCALARS

    NOTE: For scalars the arithmetic operations are the usual

    ones. For vectors and matrices the arithmetic operations can

    either follows matrix algebra rules, or can be performed on

    element-by-element basis (discussed in the next lectures).

    10

    Operation Symbol Example

    Addition + 5+3

    Subtraction - 5-3

    Multiplication * 5*3

    Right Division / 5/3

    Left Division \ 5\3=3/5

    Exponentiation ^ 5^3

  • 8/14/2019 Matlab Lecture 1w03

    10/22

    ORDER Of PRECEDENCE

    (The order in which operations are executed by the computer)

    Higher-precedence operations are executed before lower-precedence operations.

    If two operations have the same precedence, then the expression is

    executed from left to right.

    PRECEDENCE OPERATION

    First Parentheses, starting with the innermost pair.

    Second Exponentiation.

    Third Multiplication and division (equal precedence).

    Fourth Addition and subtraction (equal precedence).

    10

  • 8/14/2019 Matlab Lecture 1w03

    11/22

    Using numbers:

    8/2 is executed first

    7+8 is executed first

    5/3 is executed first

    USING MATLAB AS A CALCULATOR

    >> 7+8/2

    ans =

    11

    >> (7+8)/2ans =

    7.5000

    >> 4+5/3+2

    ans =

    7.6667

    Type and press Enter

    Computer response

    Type and press Enter

    Computer response

    Type and press Enter

    Computer response

    11

  • 8/14/2019 Matlab Lecture 1w03

    12/22

    5^3 is executed first,

    /2 is executed next.

    1/3 is executed first,

    ^ is executed next,

    + is executed last.

    27^1 and 32^0.2 is

    executed first,

    /3 is executed next,+ is executed last.

    5^3/2

    ans =

    62.5000>>

    >> 27^(1/3)+32^0.2

    ans =

    5

    >> 27^1/3+32^0.2

    ans =

    11

    Type and press Enter

    Computer response

    Type and press Enter

    Computer response

    Type and press Enter

    Computer response

    11

    12

  • 8/14/2019 Matlab Lecture 1w03

    13/22

    DISPLAY FORMATS

    12-13

    The format command controls how output numbers appear on the

    screen. Input numbers can be written in any format.

    format short (the default) 41.4286 Fixed-point with 4 decimal

    digits.

    format long 41.42857142857143 Fixed-point with 14 decimal

    digits.

    format short e 4.1429e+001 Scientific notation with 4

    decimal digits.

    format long e 4.142857142857143e+001 Scientific notation with

    15 decimal digits.

    Format bank 41.43 Two decimal digits.

    MATLAB has several other formats in which numbers can be displayed.

    13

  • 8/14/2019 Matlab Lecture 1w03

    14/22

    MATLAB BUILT-IN MATH FUNCTIONS

    In addition to arithmetic operations, MATLAB can be used to

    calculate elementary math functions. The most common ones are:

    Examples:

    >>

    sin(0.78539)

    ans =

    0.7071

    >> sqrt(169)

    ans =

    13

    >> log10(10000)

    ans =

    4

    MATLAB has hundreds of built-in functions (this will be discussed in

    future lectures).

    13-15

    sin(x) x in radians

    cos(x) x in radians

    tan(x) x in radians

    cot(x) x in radiansThe inverse is: asin(x),

    acos(x), etc.

    exp(x) exponential

    log(x) natural logarithm

    log10(x) base 10 logarithm

    sqrt(x) square rootabs(x) absolute value

    16

  • 8/14/2019 Matlab Lecture 1w03

    15/22

    THE ASSIGNMENT OPERATOR

    Variable = A value, or a computable value

    The left

    hand side

    can only beonevariable.

    The right hand side can be a

    specific value, or a computable

    expression (an expression thatincludes values and/or previously

    defined variables).

    16-18

    In MATLAB, the = sign is called the ASSIGNMENT

    OPERATOR.

    The ASSIGNMENT OPERATOR assigns a value to a variable.

    16

  • 8/14/2019 Matlab Lecture 1w03

    16/22

    THE ASSIGNMENT OPERATOR

    For example, if you type:

    >> x = 3

    x =

    3

    MATLAB assigns a new value to x, whichis the old value 3 plus 5.

    (In mathematics this expression has no

    meaning since it implies: 0 = 5.)

    MATLAB assigns the value of 3 to x.

    If then you type:

    >> x = x +5

    x =

    8

    For example, the statement:

    x + 4 = 30 is not valid. MATLAB does not solve for x,

    but the statement:

    x = 30 4 is valid (the number 26 is assigned to x.)

    16-18

    16

  • 8/14/2019 Matlab Lecture 1w03

    17/22

    DEFINING VARIABLES

    A variable is defined by typing a variable name followed by the

    assignment operator (equal sign) and then a value, or a

    mathematical expression.

    Once a variable is defined, the computer remembers and stores its

    value. The variable can then be used in further calculations.

    >> a=8

    a =

    8

    >> B=12

    B =

    12

    >> a+B

    ans =

    20

    >> a/B

    ans =

    0.6667

    >> B/a

    ans =

    1.5000

    >> B^a

    ans =

    429981696

    Type and press Enter

    Computer response

    Type and press Enter

    Computer response

    16-18

    16

  • 8/14/2019 Matlab Lecture 1w03

    18/22

    Variables can also be used to define new variables

    Once in existence, variables can be used in functions

    >> d=a*B

    d =

    96

    >> sqrt(d)

    ans =

    9.7980

    A previously defined variable can be redefined and reassigned

    a new value.

    16-18

  • 8/14/2019 Matlab Lecture 1w03

    19/22

    RULES ABOUT VARIABLES NAMES

    Variable names can be up to 63 characters long.

    Variable name can contain letters, digits, and

    the underscore character.

    Variable name must begin with a letter.

    MATLAB is case sensitive; it distinguishes

    between uppercase and lowercase letters. For

    example,Aand a are not the same variable.

    18

  • 8/14/2019 Matlab Lecture 1w03

    20/22

    18

  • 8/14/2019 Matlab Lecture 1w03

    21/22

    PREDEFINED VARIABLES

    >> pi

    ans =

    3.1416

    Typing these variables gives:

    >> eps

    ans =

    2.2204e-016

    >> inf

    ans =

    Inf

    >> i

    ans =

    0 + 1.0000i

    >> sin(pi/4)

    ans =

    0.7071

    18-19

    MATLAB has several variables that are predefined.

    These variables can be redefined to have any other value.It is probably better not to use the predefined variables as

    variable names.

    Some of the predefined variables are:

    pi ( ), eps (the smallest difference between two numbers)

    inf (infinity)

    i (square root of 1) j (square root of 1)

    ans (the value of the most recent calculation)

  • 8/14/2019 Matlab Lecture 1w03

    22/22