Biomedical Engineering Reference
In-Depth Information
case
{case_expr1,case_expr2,case_expr3,.}
statement,.,statement
.
otherwise
statement,.,statement
end
1
0.8
0.6
0.4
0.2
0
Each alternative is called a case statement and has three
parts: the case directive, one or more case expressions,
and a sequence of one or more statements. When the
switch_expr equals a case_expr, the corre-
sponding sequence of statements is executed. If no
case_expr equals the switch_expr, then the state-
ments of the otherwise case are executed.
There is one important difference with the switch
statement: The expressions must be either scalars or
strings. MATLAB finds the right sequence of statements
to execute by attempting to find the case_expr that
matches the switch_expr.
Once the statements are executed, the control flow
resumes with the statement that follows the switch
statement, as with all other statements.
-0.2
-0.4
-0.6
-0.8
-1
00000000000
Figure 2.1b-2 sin(x/3) curve.
in radians. Since the problem specifies using 100 values
over the range, the vector
x ¼ 0: (2*pi)/100:2*pi;
specifies the values of the independent variable for this
problem.
The solution to this problem is a MATLAB script that
reads two inputs. These two inputs identify a row
(indicated by either the character 's' representing the
function sine or the character 'c' representing the func-
tion cosine) and column (indicated by one of the integer
1, 2, or 3) indices of the table above. The MATLAB script
must evaluate and plot the function in the cell identified.
It is easiest to use switch statements, rather than
a large number of if-then-else statements, to control
the flow of execution of the program. An easy strategy will
be to have one switch statement that selects between the
two rows and in each statement block, have another
switch statement that will select one of the columns. To
begin,
switch letter
case 's'
a switch statement for row s goes here
case 'c'
a different switch statement for row c
goes here
end
Example 2.1b.3 Use of the switch statement.
Write a MATLAB script that reads a character variable
(s or c) and an integer variable (1, 2, or 3) and plots
a function based on the following chart:
You may assume that there are 100 points over the
interval 0 q 2p .
Solution
Instead of beginning by immediately starting to write
MATLAB code for this problem, it is best to begin by
becoming familiar with using the plot function. The
function plot can take a number of different forms. The
function call plot (Y) will plot the vector Y against its
index (or independent variable). The function call plot
(X, Y) will plot the values of the dependent variable in
the vector Y against the values of the independent vari-
able in the vector X. For example, the MATLAB com-
mands result in Fig. 2.1b-2 .
>> x ¼ 1:100;
>> plot (sin(x/3));
With this strategy, the switch statements for each row
can be independently written and independently tested,
keeping with our principles of good programming prac-
tice. The switch statement for the row s:
switch col
case 1
plot(theta,sin(theta));
case 2
plot(theta,exp(-theta).*(sin(theta)). ^ 2);
Notice that the values of the independent variable x are
plotted on the horizontal axis and the values of the de-
pendent variable are plotted on the vertical axis. The
independent variable for the given problem is the angle q ,
Search WWH ::




Custom Search