Professional Documents
Culture Documents
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
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
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
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
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
Chapter 2: Introduction to MATLAB Selection statements 15 Chapter 2: Introduction to MATLAB Selection statements 16
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.
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
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
Output
Dr Amy Glen (Murdoch University) MAS162 – Foundations of Discrete Maths Lecture 9 – S1 2016 23