Graphics Programs Reference
In-Depth Information
X = QUADRATIC(A,B,C) returns the two roots of the
quadratic equation
y = A*x^2 + B*x + C.
The roots are contained in X = [X1 X2].
matlab only echoes the comment lines that are contiguous; the first non-
comment line, in this case the blank line before the signature, tells mat-
lab that the help comments have ended. The first line of the help com-
ments is searched and, if successful, displayed when you type a
lookfor
command.
Comment lines can appear anywhere in the body of an m-file. Com-
ments can be put at the end of a line of code:
rootdisc = sqrt(b.^2 - delta); % Root of the discriminant
Blank lines can appear anywhere in the body of an m-file. Apart from
ending the help comment lines in a function, blank lines are ignored.
8.3 Flow Control
matlab has four kinds of statements you can use to control the flow
through your code:
if
,
else
and
elseif
execute statements based on a logical test
switch
,
case
and
otherwise
execute groups of statements based on
a logical test
while
and
end
execute statements an indefinite number of times,
based on a logical test
for
and
end
execute statements a fixed number of times
If, Else, Elseif
The basic form of an
if
statement is:
if
test
statements
end
The
test
is an expression that is either 1 (true) or 0 (false). The
statements
between the
if
and
end
statements are executed if the
test
is true. If the
test
is false the
statements
will be ignored and
execution will resume at the line after the
end
statement. The
test
expression can be a vector or matrix, in which case
all
the elements
must be equal to 1 for the
statements
to be executed. Further tests
can be made using the
elseif
and
else
statements.
Exercise 2
Write a function m-file that takes a vector input and
returns 1 if all of the elements are positive,
1 if they are all neg-
ative, and zero for all other cases. Hint: Type
help all
. (Answer
on page 183.)
−