matlab programming for engineers

Post on 13-Feb-2016

179 Views

Category:

Documents

25 Downloads

Preview:

Click to see full reader

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

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)

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

3

THE LOGICAL DATA TYPE (true, false)

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

4

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

Some relational operations and their results

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

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

7

LOGIC OPERATORS

EXAMPLE

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

8

LOGICAL FUNCTIONS

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

9

QUIZ!

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

10

Branches:

a) if Statement

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

11

BRANCHES (if-STATEMENT)

Optional

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:

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:

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

15

Branches:

b) switch Statement

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

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.

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

18

BRANCHES (switch-STATEMENT)

Example:

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

19

BRANCHES (switch-STATEMENT)

Example:

Example

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

20

BRANCHES (switch-STATEMENT)

Example

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

21

Branches:

b) try/catch Statement

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.

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

23

BRANCHES (try/catch)

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

24

BRANCHES (try/catch)

Example

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

25

fprintf command

fprintf(format,data)

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

27

Additional plotting features

Plotting Multiple Plots on the Same Axes

OR

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

28

Additional plotting features

Creating Multiple Figures

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

30

Additional plotting features

Enhanced Control of Plotted Lines

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

31

Additional plotting features

Enhanced Control of Plotted Lines Example,

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

32

Additional plotting features

Enhanced Control of Text Strings

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

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

34

Additional plotting features

Enhanced Control of Text StringsExamples:

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

35

Additional plotting features

Polar Plotspolar(theta,r)

Example,

CH3: BRANCHING STATEMENTS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

36

Additional plotting features

Polar Plotspolar(theta,r)

Solution:

top related