02_GENN004m_ArithmeticOperations

download 02_GENN004m_ArithmeticOperations

of 7

Transcript of 02_GENN004m_ArithmeticOperations

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    1/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    Arithmetic Operations

    Variables, Assignment, Expressions

    Outline

    Variables

    Assignment Statements

    Arithmetic Operators

    Built-in MATLAB Functions

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    2/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    The Command Window The Command

    History Window

    The Start Button

    Workspace

    Browser

    Path Browser

    Help Browser

    Function Browser

    MATLAB Desktop

    The prompt

    What are the usages of these items?

    Try it and take notes

    Current Directory

    User Input

    (What you typed)

    Calculation Results

    (What MATLAB computed from your statement)

    Command Window

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    3/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    Variables

    Variable: a named space for storing a value

    radius

    area

    Valid names start with a letter, can contain

    digits.

    Use meaningful variable names.

    MATLAB is case sensitive (area and Area are different)

    Assignment Statement

    Variable: a named space for storing a value

    sum

    Assignment: putting a value into a variable

    Assignment operator: =

    An assignment statement: r= 2*4.5

    Expression on Right Hand Side is evaluatedbefore the assignment operation

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    4/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    Assignment Statement

    Expression on rhs is evaluated before the

    assignment operation

    Examples:

    x= 2*3.14

    y= 1+x

    z= 4^2 cos(y)

    Any variable on the rhs must be initialized

    Scalar vs Array Scalar: indicates a variable that holds only a single

    value at a time.

    This is compared to an array, which holds many values atonce

    Array: is an indexed grouping of values, addressed withsubscripts

    B = [1.0 2.0 3.0] creates a 1 x 3 row array/vector

    length(vec): Returns length (# of elements) of vector

    length(B) returns 3

    A = [10, 20, 30, 40, 50]

    A(1)=10

    A(2)=20

    A(3)=30

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    5/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    Operators

    + Addition

    - Subtraction

    * Multiplication

    / Division (divided by e.g., 10/5 is 2)

    \ Division (divided into e.g., 5\10 is 2)

    ^ Exponentiation (e.g., 5^2 is 25)

    Operators Precedence

    ( ) parentheses

    ^ exponentiation

    - negation

    *,/,\ multiplication and division

    +, - addition and subtraction

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    6/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    Etter/Ingber

    Arithmetic Operation Examples

    What the results would be for the followingexpressions (do it by hand then test it onMATLAB):

    1. a = 3 + 5 / 10

    2. a = ( 3 + 5 ) / 10

    3. a = 3 + 5 / 10 * 3

    4. a = 3/2 5 / 10

    5. a = 3 5/26. a = ((3+5)/((3+2)*2))

    Etter/Ingber

    Arithmetic Operations Examples

    Write the MATLAB expression to evaluate the followingequations: Assume x=3, y=5, z=10 (do it by hand thentest it on MATLAB)

    ;53

    )32(

    ;543

    ;3

    2

    22

    +

    +=

    +

    ++=

    +=

    z

    yxa

    yx

    yzxa

    z

    yxa

  • 7/29/2019 02_GENN004m_ArithmeticOperations

    7/7

    GENN004: Introduction to Computers 09-Feb-

    Arithmetic Operations

    MATLAB built-in functions

    x= 25;

    y=cos(x);

    a=sqrt(x);

    z=rem(13,5);

    Function name

    Arguments (values) passed to the function

    - For more functions, check the Function Browser

    - Or type help elfun to show a list of the elementary mathfunctions- Practice: Find out what the rounding functionsfloor, ceil,

    and rounddo.

    Functions Examples

    Evaluate the following expressions (do it by handthen test it on MATLAB):

    1. floor(-2.6) ceil(-2.6)

    2. abs(-10*2.5) abs(-2^5)

    3. floor(ceil(10.8)) ceil(floor(10.8))

    4. rem(10,3) rem(3,10)5. round(10.4) round(10.6)

    6. fix(10.4) fix(10.6)