You are on page 1of 6

1 Chapter 2: Introduction to MATLAB 2

MAS162 – Foundations of Discrete Mathematics


Semester 1, 2016

Dr Amy Glen
Chapter 2: Introduction to MATLAB
School of Engineering & Information Technology
Murdoch University, Perth, Australia (continued)
A.Glen@murdoch.edu.au
http://amyglen.wordpress.com

LECTURE 9

Selection statements (if, else, elseif, and find) in MATLAB

Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 1 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 2

Chapter 2: Introduction to MATLAB 3 Chapter 2: Introduction to MATLAB Selection statements 4

Selection statements
The basic selection statement is the if statement, which in MATLAB has
the form:
I Last time, we were looking at using for loops in MATLAB
(Section 2.5). if (condition)
... % some statement(s)
I You should now go about working through the remaining exercises end
and examples in Section 2.5 of the Unit Notes by yourself (Tutorial 3).
which means that the statement(s) are executed if and only if the stated
I Today we’ll start looking at selection statements (Section 2.6); condition holds. For example:
in particular, the if construct and find command. if y > 0.5
I Then we’ll move onto user-defined functions and their uses ... % some statement(s)
(Section 2.7). end
will execute the statements if and only if the value of y is > 0.5.
Example
Consider the next MATLAB program. What do you think the final value of
count represents?
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 3 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 4
Chapter 2: Introduction to MATLAB Selection statements 5 Chapter 2: Introduction to MATLAB Selection statements 6

Example . . .
2
Plot of y=x for x=−1,−0.9,...,1
1

clear
0.9
count=0;
for x=-1:0.1:1 0.8
y=x^2;
if y > 0.5 0.7
count=count+1;
disp([count y]) 0.6
end
end 0.5

y
Executing the program gives: 0.4
1 1
2.0000 0.8100 0.3

3.0000 0.6400
4.0000 0.6400 0.2

5.0000 0.8100
6 1 0.1

So there are 6 y values satisfying y > 0.5. Is this what you expected? 0
Let’s have a look at the plot of y = x 2 for the specified x values.
−1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 5 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 6

Chapter 2: Introduction to MATLAB Selection statements 7 Chapter 2: Introduction to MATLAB Selection statements 8

Instead of > in y > 0.5 above, we could have any one of the following:
relational operator meaning
< less than
<= less than or equal Similarly, logical expressions involving arrays are treated
> greater than element-by-element.
>= greater than or equal For example:
== equal
~= not equal >> a=[2 4 6];
>> b=[3 5 1];
I A statement of the form y > 0.5 is called a logical expression. >> a < b
I Depending on the value of the variable y, the expression y > 0.5 has ans =
value 0 (false) or 1 (true). 1 1 0
For example:
>> y = 0.8;
>> y > 0.5
ans =
1
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 7 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 8
Chapter 2: Introduction to MATLAB Selection statements 9 Chapter 2: Introduction to MATLAB Selection statements 10

Combining logical expressions Nested if statements


Logical expressions can be combined in MATLAB using the following: If statements can be nested.

logical operator symbol Example:


not ~
and & clear
or | count=0;
for x=-1:0.1:1
For example, the if statement in the previous program could be replaced y=x^2;
by if y > 0.5
count=count+1;
disp([count y])
if y > 0.5 & y < 0.8
if x > 0.5
disp(’Both x and y are > 0.5’)
or by end
end
if y < 0.2 | y > 0.5 end

Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 9 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 10

Chapter 2: Introduction to MATLAB Selection statements 11 Chapter 2: Introduction to MATLAB Selection statements 12

else and elseif


Output: When there are two cases to consider, we use else.

1 1 For example, we can use else to define the absolute value of a given
2.0000 0.8100 number.
3.0000 0.6400
% Define the absolute value of x
4.0000 0.6400
clear
Both x and y are > 0.5
x=input(’Enter value of x: ’);
5.0000 0.8100
disp(’absolute value is’)
Both x and y are > 0.5
if x >= 0
6 1
y=x
Both x and y are > 0.5
else
y=-x
end

Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 11 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 12
Chapter 2: Introduction to MATLAB Selection statements 13 Chapter 2: Introduction to MATLAB Selection statements 14

Entering some values of x:


Enter value of x: 1 When there are more than two cases to consider in levels, we use elseif
absolute value is as follows.
y =
1 % Classify Goldilocks’ porridge
clear
Enter value of x: 5 temp=input(’Enter temperature: ’);
absolute value is if temp > 60
y =
5
disp(’porridge is too hot’)
elseif temp > 30
Enter value of x: -3 disp(’porridge is just right’)
absolute value is elseif temp > 0
y = disp(’porridge is too cold’)
3 else
disp(’porridge is frozen solid’)
Enter value of x: -sqrt(2)
end
absolute value is
y =
1.4142
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 13 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 14

Chapter 2: Introduction to MATLAB Selection statements 15 Chapter 2: Introduction to MATLAB Selection statements 16

Logical function find


Entering a few different values of temp:

Enter temperature: 67 I The function find is very useful to find the elements of a vector that
porridge is too hot satisfy some condition.

Enter temperature: 28 I Note that find returns the index set of these elements, i.e., the
porridge is too cold positions of the elements that satisfy the given condition.

Enter temperature: 35 Example


porridge is just right Write a MATLAB program using the find command to find the values of
y = x 2 for x = −1, −0.9, −0.8, . . . , 1 that are greater than 0.5, and the
Enter temperature: 30 number of these values.
porridge is too cold
Then redefine these values to equal 0.5 and plot the resulting points.

Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 15 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 16
Chapter 2: Introduction to MATLAB Selection statements 17 Chapter 2: Introduction to MATLAB Selection statements 18

2
% Find elements of a vector that satisfy a condition 0.5
Plot of y=x for x=−1,−0.9,...,1 with y values > 0.5 redefined to 0.5

clear
x=-1:0.1:1; 0.45
y=x.^2;
indset=find(y > 0.5) % index set of elements of vector y that are > 0.5 0.4
number=length(indset) % number of elements of y that are > 0.5
disp(y(indset)) % display elements of y that are > 0.5 0.35
y(indset)=0.5; % redefine elements
plot(x,y,’o’) 0.3
xlabel(’x’)
ylabel(’y’) 0.25

y
title(’Plot of y=x^2 for x=-1,-0.9,...,1 with y values > 0.5 reset to 0.5’)
0.2

Note that the length command returns the length of a vector.


0.15
indset =
1 2 3 19 20 21 0.1

number = 0.05
6
0
1.0000 0.8100 0.6400 0.6400 0.8100 1.0000 −1 −0.8 −0.6 −0.4 −0.2 0 0.2 0.4 0.6 0.8 1
x
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 17 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 18

Chapter 2: Introduction to MATLAB Selection statements 19 Chapter 2: Introduction to MATLAB Selection statements 20
% Compute cost of bicycle orders

Example clear
format bank
orders=[25,5,12,80,3,7,56,21,30,17];
I An importer of bicycles charges retailers $250 each for bicycles of a certain tot_cost=0;
type, but is prepared to offer discounts for large orders. disp(’ order cost’)
for n=1:length(orders)
I There is a discount of 5% for orders of 20 or more but less than 50, and a if orders(n) >= 50
cost=0.90*250*orders(n);
discount of 10% for orders of 50 or more. elseif orders(n) >= 20
cost=0.95*250*orders(n);
I There is no discount for orders of less than 20. else
cost=250*orders(n);
I On a particular day there were ten orders of sizes shown in the following end
vector: orders=[25,5,12,80,3,7,56,21,30,17] disp([n cost])
tot_cost=tot_cost+cost;
end
Exercise disp(’ ’)
Write a MATLAB program using the given vector orders, a for loop and an if disp(’Total cost of all orders:’)
disp(tot_cost)
construct that will, for each order, display the order number (1, . . . , 10) and the small_discount=find(orders>=20 & orders<50);
cost of the order, as well as the total cost of all bicycles ordered. disp(’ ’)
disp(’Orders that received a 5% discount:’)
Extend the program using the find command to identify and display the orders disp(small_discount)
(numbered 1 to 10) that obtained a 5% discount, and use the length command disp(’ ’)
to compute and display the number of such orders. disp(’Number of orders that received a 5% discount:’)
disp(length(small_discount))
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 19 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 20
Chapter 2: Introduction to MATLAB Selection statements 21 Chapter 2: Introduction to MATLAB Selection statements 22

% Compute cost of bicycle orders


clear
Output format bank
order cost orders=[25,5,12,80,3,7,56,21,30,17];
1.00 5937.50 tot_cost=0;
2.00 1250.00 for n=1:length(orders)
3.00 3000.00 if orders(n) >= 50
4.00 18000.00 cost=0.90*250*orders(n);
5.00 750.00 elseif orders(n) >= 20
6.00 1750.00 cost=0.95*250*orders(n);
7.00 12600.00 else
8.00 4987.50 cost=250*orders(n);
9.00 7125.00 end
10.00 4250.00 fprintf(’Order %2.0f cost $%8.2f.\n’,n,cost)
tot_cost=tot_cost+cost;
Total cost of all orders: end
59650.00
fprintf(’Total cost of all orders is $%8.2f.\n\n’,tot_cost)
Orders that received a 5% discount:
1.00 8.00 9.00 small_discount=find(orders>=20 & orders<50);
disp(’Orders that received a 5% discount:’)
Number of orders that received a 5% discount: disp(small_discount)
3.00
disp(’ ’)
disp(’Number of orders that received a 5% discount:’)
disp(length(small_discount))
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 21 Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 22

Chapter 2: Introduction to MATLAB Selection statements 23

Output

Order 1 cost $ 5937.50.


Order 2 cost $ 1250.00.
Order 3 cost $ 3000.00.
Order 4 cost $18000.00.
Order 5 cost $ 750.00.
Order 6 cost $ 1750.00.
Order 7 cost $12600.00.
Order 8 cost $ 4987.50.
Order 9 cost $ 7125.00.
Order 10 cost $ 4250.00.
Total cost of all orders is $59650.00.

Orders that received a 5% discount:


1.00 8.00 9.00

Number of orders that received a 5% discount:


3.00

Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 23

You might also like