Matlab Programming for Engineers

36
CH3: BRANCHING STATEMENTS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Introduction to Matlab Matlab Basics Matlab Basics Branching Statements Branching Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

description

Matlab Programming for Engineers . Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) . Dr. Nidal Farhat. OBJECTIVES . - PowerPoint PPT Presentation

Transcript of Matlab Programming for Engineers

Page 1: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

1

Matlab Programming for Engineers Matlab Programming for Engineers

Dr. Nidal Farhat

Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching StatementsBranching Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Page 2: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

2

OBJECTIVES

The Logical Data Type Relational Operators (==, ~=, >, < , >=, <=) Logic Operators (&, &&, |, ||, xor, ~) Logical Functions (ischar, isempty, ….) Branches

(if-else) Statement switch Statement try-catch Statement

Additional Plotting Features

Page 3: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

3

THE LOGICAL DATA TYPE (true, false)

Page 4: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

4

RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)

Some relational operations and their results

Page 5: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

5

RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)

Compare array by a value

Compare array by another one (of the

same size)

Be careful because of roundoff errors

Page 6: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

6

LOGIC OPERATORS

Operator Operation& Logical AND (scalar/array, both sides evaluation)

&& Logical AND (scalar, left then right side evaluation)

| Logical inclusive OR (scalar/array both sides evaluation)

|| Logical inclusive OR (scalar, left then right side evaluation)

xor Logical exclusive OR~ Logical NOT

Page 7: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

7

LOGIC OPERATORS

EXAMPLE

Page 8: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

8

LOGICAL FUNCTIONS

Page 9: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

9

QUIZ!

Page 10: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

10

Branches:

a) if Statement

Page 11: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

11

BRANCHES (if-STATEMENT)

Optional

Page 12: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

12

BRANCHES (if-STATEMENT)

Example: Write a program that determine whether a quadratic equation:

has real (identical/different) or complex roots. (Use input command for entering the coefficients (a,b,c) and disp commands to display the output).

Write a program that calculates the roots in each case. (Use input command for entering the coefficients of the equation and disp commands to display the output).

Test the program on the following equations:

Page 13: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

13

BRANCHES (if-STATEMENT)

Example: evaluating a Function of Two Variables

Test it using the following (x,y) values:

Page 14: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

14

BRANCHES (if-STATEMENT)

Nested if-Statements

It is preferable to use if-else-end statement whenever is possible

Page 15: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

15

Branches:

b) switch Statement

Page 16: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

16

BRANCHES (switch-STATEMENT)

The switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives.

The switch_expr and case_expr may be a scalar or a string

Optional

Page 17: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

17

BRANCHES (switch-STATEMENT)

Remarks: The case expression could be more than one enclosed by {}

At most one code block can be executed. If the switch_exp matches more than case_exp the first one is executed.

Page 18: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

18

BRANCHES (switch-STATEMENT)

Example:

Page 19: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

19

BRANCHES (switch-STATEMENT)

Example:

Example

Page 20: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

20

BRANCHES (switch-STATEMENT)

Example

Page 21: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

21

Branches:

b) try/catch Statement

Page 22: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

22

BRANCHES (try/catch)

The try/catch statement is designed to trap errors. In MATLAB when an error is encountered while running the program

the program execution is aborted. Use try/catch statement to run a part of the program susceptible to

produce error when executed. MATLAB will try to execute this part. If an error is produced, then instead of aborting, it executes the part of the program found in the catch statement.

If no error is produced in the try block, the catch block will not be executed.

The programmer can handle errors in the program without causing the program to stop.

Page 23: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

23

BRANCHES (try/catch)

Page 24: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

24

BRANCHES (try/catch)

Example

Page 25: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

25

fprintf command

fprintf(format,data)

Page 26: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

26

Additional plotting features

Example f(x) = sin(x), from -2π to 2π

Then restrict the axes to the region 0 ≤ x ≤ π, and 0 ≤ y ≤ 1

Page 27: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

27

Additional plotting features

Plotting Multiple Plots on the Same Axes

OR

Page 28: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

28

Additional plotting features

Creating Multiple Figures

Page 29: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

29

Additional plotting features

Subplots Subplot(m,n,p) ≡ creates mxn subplots in the current figure (array of

subplots), m rows and n columns. p is the current subplot.

p=1 p=2

p=4 p=[5 6]

p=3

Page 30: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

30

Additional plotting features

Enhanced Control of Plotted Lines

plot(x,y,'PropertyName',value,…)

Page 31: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

31

Additional plotting features

Enhanced Control of Plotted Lines Example,

Page 32: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

32

Additional plotting features

Enhanced Control of Text Strings

Page 33: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

33

Additional plotting featuresEn

hanc

ed C

ontr

ol o

f Tex

t Str

ings

Page 34: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

34

Additional plotting features

Enhanced Control of Text StringsExamples:

Page 35: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

35

Additional plotting features

Polar Plotspolar(theta,r)

Example,

Page 36: Matlab Programming for Engineers

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

36

Additional plotting features

Polar Plotspolar(theta,r)

Solution: