Matlab Ch3 - An

advertisement
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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
Mechanical Engineering Department
1
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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
Mechanical Engineering Department
2
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
THE LOGICAL DATA TYPE (true, false)
Mechanical Engineering Department
3
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)
Some relational operations
and their results
Mechanical Engineering Department
4
CH3: BRANCHING STATEMENTS
MATLAB PROGRAMMING FOR ENGINEERS
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)
Compare array
by a value
Compare array by
another one (of the
same size)
Mechanical Engineering Department
Be careful because of
roundoff errors
5
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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
Mechanical Engineering Department
6
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
LOGIC OPERATORS
EXAMPLE
Mechanical Engineering Department
7
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
LOGICAL FUNCTIONS
Mechanical Engineering Department
8
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
QUIZ!
Mechanical Engineering Department
9
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Branches:
a) if Statement
Mechanical Engineering Department
10
CH3: BRANCHING STATEMENTS
MATLAB PROGRAMMING FOR ENGINEERS
BRANCHES (if-STATEMENT)
Optional
Mechanical Engineering Department
11
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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:
Mechanical Engineering Department
12
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (if-STATEMENT)
Example: evaluating a Function of Two Variables
Test it using the following (x,y) values:
Mechanical Engineering Department
13
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (if-STATEMENT)
Nested if-Statements
It is preferable to use if-else-end statement whenever is possible
Mechanical Engineering Department
14
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Branches:
b) switch Statement
Mechanical Engineering Department
15
CH3: BRANCHING STATEMENTS
MATLAB PROGRAMMING FOR ENGINEERS
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
Mechanical Engineering Department
16
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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.
Mechanical Engineering Department
17
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (switch-STATEMENT)
Example:
Mechanical Engineering Department
18
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (switch-STATEMENT)
Example:
Example
Mechanical Engineering Department
19
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (switch-STATEMENT)
Example
Mechanical Engineering Department
20
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Branches:
b) try/catch Statement
Mechanical Engineering Department
21
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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.
Mechanical Engineering Department
22
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (try/catch)
Mechanical Engineering Department
23
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
BRANCHES (try/catch)
Example
Mechanical Engineering Department
24
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
fprintf command
fprintf(format,data)
Mechanical Engineering Department
25
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
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
Mechanical Engineering Department
26
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Plotting Multiple Plots on the Same Axes
OR
Mechanical Engineering Department
27
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Creating Multiple Figures
Mechanical Engineering Department
28
CH3: BRANCHING STATEMENTS
MATLAB PROGRAMMING FOR ENGINEERS
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=4
Mechanical Engineering Department
p=2
p=3
p=[5 6]
29
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Enhanced Control of Plotted Lines
plot(x,y,'PropertyName',value,…)
Mechanical Engineering Department
30
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Enhanced Control of Plotted Lines
Example,
Mechanical Engineering Department
31
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Enhanced Control of Text Strings
Mechanical Engineering Department
32
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Enhanced Control of Text Strings
Additional plotting features
Mechanical Engineering Department
33
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Enhanced Control of Text Strings
Examples:
Mechanical Engineering Department
34
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Polar Plots
polar(theta,r)
Example,
Mechanical Engineering Department
35
MATLAB PROGRAMMING FOR ENGINEERS
CH3: BRANCHING STATEMENTS
Additional plotting features
Polar Plots
polar(theta,r)
Solution:
Mechanical Engineering Department
36
Download