MATLAB 06 Control Structures
MATLAB 06 Control Structures
Computer programs (not just MATLAB) can be categorized into one of three
structures: sequences, selection structures, and repetition structures (see Fig 6.1).
Up to this point, all commands have been written in sequences.
Logical Operators
Selection and repetition structures used in MATLAB depend on relational
operators, used to compare two matrices of equal size. Comparisons are either
true or false. MATLAB (along with most other computer programs) use the
number 1 for true and 0 for false. Consider the expressions
>> x = 5;
>> y = 1;
>> x<y returns ans = 0 meaning false
6-1
The six relational operators within MATLAB include:
Operator Interpretation
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
~= not equal to
MATLAB also allows for the combination of comparisons wityh the logical
operators:
Operator Interpretation
& and
~ not
| or
As an example,
>> x = 5;
>> y = 1;
>> z = 7;
>> x<y & x<z returns ans = 0 meaning false
>> x<y | x<z returns ans = 1 meaning true
Selection Structures
A simple if statement has the following form:
if comparison
statements
end
If the comparison is true, the statements between the if statement and end
statement are executed. It is good practice to indent the statements within the if
structure for readability.
6-2
Practice Problem 6.1
Create a function accepts 𝑎, 𝑏, 𝑐, and calculates the real roots of a quadratic
equation
𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0
Recall that the roots are
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥1 , 𝑥2 =
2𝑎
2
Use the function for 𝑥 − 6𝑥 + 8 = 0, where we all can factor to get 𝑥 = 2 and
𝑥 = 4 as solutions
6-3
Practice Problem 6.2
Create a function that accepts a test score and determines a letter grade:
Grade Score
A ≥ 90
B ≥80 and <90
C ≥70 and <80
D ≥60 and <70
F <60
6-4
Practice Problem 6.3
Create script that quires the user for coordinates of a point
(x1, y1) and determines the quadrant (1, 2, 3, or 4) of a line
extending from the origin to that point.
6-5
Practice Problem 6.5
Modify the previous example to use the menu command.
Find Command
The find command searches a matrix and identifies which elements in that matrix
meet a given criteria. The find command can often be used more efficiently in
place of traditional selection structures. Consider the following matrix containing
the heights of applicants for a Military Academy,
height = [63, 67, 65, 72, 69, 78, 75]
The find command will identify the index number of the matrix that meets the
specified criteria. Since applicants are required to be at least 5’6” (66”),
acceptable applicants are
accept = find (height >= 66)
which returns
accept = 2 4 5 6 7
Further,
height(accept)
returns
accept = 67 72 69 78 75
Consider a larger matrix, where each row contains the applicant height and age.
applicants = [63, 18; 67, 19; 65, 18; 72, 20; 69, 36; 78, 43; 75, 12]
Since applicants are required to be at least 5’6” (66”), acceptable applicants are
pass = find (applicants(: ,1) >= 66 && applicants(: ,2)>=18 …
&& applicants(: ,2)< 35)
which returns
pass = 2
4
5
6-6
Practice Problem 6.6
The height ℎ of a rocket (in meters) as a function of time 𝑡 (in seconds) can be
represented by the following equation:
ℎ = 2.13𝑡 2 − 0.0013𝑡 4 + 0.000034𝑡 4.751
Create a vector of time from 0 to 70 at 0.5-second intervals. Use the find function
to determine when the rocket hits the ground to within 0.5 seconds.
6-7
Practice Problem 6.7
Although it would be better to use MATLAB’s vector capability, use a for loop to
create a degrees-to-radians table.
6-8
Practice Problem 6.10
A cantilever beam is shown below. Beam width, w
x General Location Force, F
Length, L
6-9
Note that the variable used to control the while loop must be updated every time
through the loop. If not, an endless loop results. To manually exit a calculation,
type ctrl-c.
Consider another example
scores = [76, 92, 45, 86, 90, 82, 98, 97];
count = 0;
k = 0;
while k < length(scores)
k = k + 1;
if scores(k)>90
count = count + 1;
end
end
disp(count)
6-10
Break and Continue
The break command can be used to terminate a loop prematurely, while the
comparison criterion is still true. Here’s an example
n = 0;
while n < 10
n = n + 1;
a = input(‘Enter a value greater than 0: ’)
if a < 0
disp(‘You should’ve entered a positive number ’)
disp(‘This program will not terminate ’)
break
end
disp(‘The natural log of that number is ’)
disp(log(a))
end
6-11
Animations
MATLAB can be used to create animated graphics by continually erasing and then
redrawing objects on the screen, making incremental changes with each redraw.
Here’s an example:
close all; clear all; clc
n = 20;
s = .02;
x = rand(n,1)-0.5;
y = rand(n,1)-0.5;
figure
axis([-1 1 -1 1])
axis square
grid off
hold on
while min(abs([x;y]))<1
x = x+s*rand(n,1);
y = y+s*rand(n,1);
plot(x,y,'.','MarkerSize', 15, 'tag', 'update') %tags objects as “update”
pause(.05)
delete(findobj('tag', 'update')) %erases all objects tagged as “update”
end
6-12
6-13