Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MATLAB Lecture #4 EGR 110 - Engineering Graphics

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

1

MATLAB Lecture #4 EGR 110 Engineering Graphics


Reading Assignment: MATLAB handout
MATLAB Assignment: MATLAB Assignment #4
Program Flow
So far our MATLAB examples have been limited to examples of straight line
code, or a series of instructions that are executed in a single sequence. Two new
control structures will now be introduced:
Conditional control structures (or branching structures)
Iterative control structures (or looping structures)

Flowcharts are used below to illustrate the difference between straight line,
conditional, and iterative structures on the following slide.

This lecture will focus on conditional structures. Iterative structures will be
covered in the following lecture.
2
MATLAB Lecture #4 EGR 110 Engineering Graphics
Command
Command
Command
Command
Command
Straight Line Control
Command
Test
Commands
to execute
if False
Command
Commands
to execute
if True
True False
Conditional Control
(branching structure)
Command
Commands
Command
Setup
Done
Iterative Control
(looping structure)
3
MATLAB Lecture #4 EGR 110 Engineering Graphics
Since conditional structures involve branching based on the result of logical tests
(such as a program that branches if x > 2), it is important to first introduce logical
expressions.

Logical Expressions
Logical variables in MATLAB can have values of true or false.
To create a logical variable, a logical values simply needs to be assigned to it.
For example:
A = true;
B = false;

Note that logical values also have a numerical equivalent:
true = 1
false = 0




4
MATLAB Lecture #4 EGR 110 Engineering Graphics
Relational Operators
MATLAB generally uses logical expressions (resulting in true or false conditions) for
tests in conditional structures. The general form is:
A1 relational operator A2 (for example: x > 2)
where A1 and A2 are arithmetic expressions, variables, or strings.
The table below shows the relational operators available in MATLAB. Several
examples of logical expressions are also shown.
Table 4.1 Relational Operators
Operator Operation
= = Equal to
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Logical Expression
(for x = 2, y = 5)
Result
3 <= 4 true (1)
x ~= y true (1)
2*x = = y false (0)
x > y false (0)
x + 3 >= y - 1 true (1)
A < B true (1)
Examples of Logical Expressions
5
MATLAB Lecture #4 EGR 110 Engineering Graphics
Logical Operators
Logical operators are used in MATLAB between two logical operands to yield a logical
result. The general form for logical expression with the 5 available binary operators is:
L1 logical operator L2 (for example: x > 2 AND x < 8)
where L1 and L2 are logical expressions or variables.
The general form for logical expression with the one available unary operators is:
logical operator L1 (for example: ~x > 2)
Table 4.2 Logical Operators
Operator Operation Binary or unary
& Logical AND Binary
&& Logical AND with
shortcut evaluation
Binary
| Logical OR Binary
|| Logical OR with
shortcut evaluation
Binary
xor Logical Exclusive OR Binary
~ Logical NOT Unary
6
MATLAB Lecture #4 EGR 110 Engineering Graphics
Table 4.3 Truth Tables for Logical Operators
L1 L2 L1 & L2
AND
L1 | L2
OR
xor(L1,L2)
Exclusive-OR
~L1
NOT
false false false false false true
false true false true true true
true false false true true false
true true true true false false
Precedence Operator
1 Arithmetic operators (as described previously)
2 Relational operators (= =, ~=, >, >=, <, <=) from left to right
3 Unary NOT operator (~)
4 Logical AND operators (&, &&) from left to right
5 Logical OR and Exclusive OR operators (|, ||, xor) from left to right
Table 4.4 Precedence of Operations (1 = highest precedence, 5 = lowest precedence)
7
MATLAB Lecture #4 EGR 110 Engineering Graphics
Logical Expression
(for x = 2, y = 5)

Result
x < 4 & x > 1 true (1)
1 < x < 4 invalid expression
x = = 1 | x = = 3 false (0)
~x < y false (0)
xor(x > 3, y > 3) true (1)
x = = 2| y = = 3 & x < 0 true (1)
Examples of Logical Expressions
Shortcut evaluation (a minor point)
What is the difference between Logical AND (&) and Logical AND with shortcut
evaluation (&&)? The difference is illustrated below:
L1 & L2 % L1 and L2 are both evaluated, then the results are ANDed
L1 && L2 % L1 is evaluated. If it is false, the result of the entire expression is
% false, so L2 is never evaluated.
Also note that && works only with scalars, where & works with either scalar or array
values.
The differences are between Logical OR (|) and Logical OR with shortcut
evaluation (||) are similar to those described above for & and &&.
8
MATLAB Lecture #4 EGR 110 Engineering Graphics
Conditional Control Structures in MATLAB
MATLAB offers three types of conditional control structures:
IF THEN ELSE structure
SWITCH structure
TRY/CATCH structure

We will only cover the IF THEN ELSE structure as it is the most common.
9
MATLAB Lecture #4 EGR 110 Engineering Graphics
IF THEN ELSE structure
There are four versions of this
structure as shown.
if relational/logical test
<commands to execute if test is true>
end
if relational/logical test
<commands to execute if test is true>
else
<commands to execute if test is false>
end
if relational/logical test #1
<commands to execute if test #1 is true>
elseif relational/logical test #2
<commands to execute if test #2 is true>
end
if relational/logical test #1
<commands to execute if test #1 is true>
elseif relational/logical test #2
<commands to execute if test #2 is true>
else
<commands to execute if all tests above are false>
end
Note that an unlimited number
of elseif statements could be
added to the last two structures.
10
MATLAB Lecture #4 EGR 110 Engineering Graphics
% IF - THEN - ELSE structure - Example 1
% EGR 110
% filename: if1.m
% Sample program to calculate weekly pay. Overtime paid for over 40 hours.
format compact
Hours = input('Enter number of hours worked this week: ');
Rate = input('Enter amount paid per hour: $');
Pay = Hours*Rate;
if Hours > 40
disp('You earned overtime pay.')
Pay = Pay + (Hours-40)*Rate/2;
end
fprintf('Your pay is $%0.2f\n',Pay)
11
MATLAB Lecture #4 EGR 110 Engineering Graphics
% IF - THEN - ELSE structure - Example 2
% EGR 110
% filename: if2.m
% Sample program to calculate weekly pay. Overtime paid for over 40 hours.
format compact
Hours = input('Enter number of hours worked this week: ');
Rate = input('Enter amount paid per hour: $');
if Hours <= 40
disp('No overtime earned.')
Pay = Hours*Rate;
else
disp('You earned overtime pay.')
Pay = 40*Rate + (Hours-40)*Rate*1.5;
end
fprintf('Your pay is $%0.2f\n',Pay)
12
MATLAB Lecture #4 EGR 110 Engineering Graphics
% IF - THEN - ELSE structure - Example 3
% EGR 110
% filename: if3.m
% Sample program to calculate y(x) defined
% over several ranges of x.
format compact
x = input('Enter value of x: ');
y = 0; % This value will be replaced if
% x is between 0 and 20.
if 0 <= x & x < 10
y = 4*x;
elseif 10 <= x & x < 20
y = -4*x+80;
end
fprintf('y(x) = %0.2f\n',y)

s
< s +
< s
<
=
x 20 , 0
20 x 10 , 80 x 4
10 x 0 x, 4
0 x , 0
y(x)
: follows as defined is y(x) Function
0
10
20
x
y
40
13
MATLAB Lecture #4 EGR 110 Engineering Graphics
% IF - THEN - ELSE structure - Example 4
% EGR 110
% filename: if4.m
% Sample program to display letter grade for an input
% numerical grade using a standard 10 point scale.
% Display error message if grades are from 0 to 100.
format compact
Grade = input('Enter numerical grade (0 to 100): ');
if Grade < 0 | Grade > 100
disp('Invalid grade')
elseif Grade >= 90
disp('Grade = A')
elseif Grade >= 80
disp('Grade = B')
elseif Grade >= 70
disp('Grade = C')
elseif Grade >= 60
disp('Grade = D')
else
disp('Grade = F')
end

You might also like