20
Programming with MATLAB

Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

  • View
    242

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Programming with MATLAB

Page 2: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Relational Operators

Page 3: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Relational Operators

The arithmetic operators has precedence over relational operators

Page 4: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Logical Operators

Page 5: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Order of precedence for operators

Page 6: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Order of precedence for operators

Page 7: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Logical variables

Logical variables can take only two values: <> 0 (true) 0 (false)

Page 8: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The if statement

End

Start

Page 9: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The if statement

If x is greater than or equal 5, the statement y = sqrt(x-5) gets executed

Otherwise, the statement does not execute

50

55

x

xxy

Page 10: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The if statement

The value of z and w are computed only if both x and y are nonnegative.

If any of x or y is negative, both w and z retain their initial values

Page 11: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The else statement

Page 12: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The else statement

50

55

x

xxy

Page 13: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The else statement

If x is nonnegative, y = sqrt(x) is executed If x is negative, an error message is

displayed

Page 14: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

The elseif statement

Page 15: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Example: Numeric grade to letter grade Write a program to display your course letter

grade knowing your numeric grade Example:

If your numeric grade is 93, then the program displays

the letter A If your numeric grade is 70, then the program displays

the letter C

Page 16: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators
Page 17: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Nested if statement

=

Page 18: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Nested if statement

This statement gives the same result as the previous one

=

Page 19: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Examples

Write a function that accepts one variable, which is the year and returns a logical variable: true (if the year is leap) or false (if the year is not leap)

A leap year is characterized by All years evenly dividable by 400 are leap All years that are evenly dividable by 4 and are

not evenly dividable by 100 are leap

Page 20: Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators

Examples