MATLAB Fundamentals
MATLAB
PROGRAMMING
And Graphics
DDE2023/03 1
MATLAB PROGRAMMING
Relational and Logical Operator
Conditional Statements
Loop Structures
Break
Switch and Case
DDE2023/03 2
Relational Operators
The relational operators in MATLAB are
< less than
> greater than
<= less than or equal
>= greater than or equal
== equal
~= not equal.
Note that "=" is used in an assignment statement while "=="
is used in a relation. Relations may be connected or
quantified by the logical operators
DDE2023/03 3
Relational Operators
When applied to scalars, a relation is actually the scalar
1 or 0 depending on whether the relation is true or false.
Try 3 < 5, 3 > 5, 3 == 5, and 3 == 3
>> 3 < 5, 3 > 5, 3 == 5, 3 == 3
ans =
1
ans =
0
ans =
0
ans =
DDE2023/03 4
Relational Operators
When applied to matrices of the same size, a relation is
a matrix of 0's and 1's giving the value of the relation
between corresponding entries.
Try a = rand(5), b = triu(a), a == b.
DDE2023/03 5
Relational Operators
>> a = rand(3), b = triu(a), a == b
a=
0.2028 0.2722 0.7468
0.1987 0.1988 0.4451
0.6038 0.0153 0.9318
b=
0.2028 0.2722 0.7468
0 0.1988 0.4451
0 0 0.9318
ans =
1 1 1
0 1 1
0 0 1
DDE2023/03 6
Relational Operators
A relation between matrices is interpreted by
while and if to be true if each entry of the relation
matrix is nonzero. Hence, if you wish to execute
statement when matrices A and B are equal you
could type
if A == B
statement
end
DDE2023/03 7
Relational Operators
but if you wish to execute statement when A and
B are not equal, you would type:
if any(any(A ~= B))
statement
end
or, more simply,
if A == B
else
statement
end
DDE2023/03 8
Relational Operators
Note that the seemingly obvious
if A ~= B, statement, end
will not give what is intended since statement
would execute only if each of the corresponding
entries of A and B differ.
DDE2023/03 9
logical operators
the logical operators
& and
| or
~ not.
DDE2023/03 10
Control Statements
In their basic forms, these MATLAB flow
control statements operate like those in
most computer languages.
DDE2023/03 11
If and if else if
If. The general form of a simple if statement is
if relation
statements
end
The statements will be executed only if the relation is
true. Multiple branching is also possible, as is
illustrated by
DDE2023/03 12
if else if
if (logical expression)
matlab command
elseif (other logical expression)
another matlab command
else a matlab command
end
if n < 0
parity = 0;
elseif rem(n,2) == 0
parity = 2;
else
parity = 1;
end
In two-way branching the elseif portion would, of course, be omitted.
DDE2023/03 13
for
for a given n, the statement
x = []; for i = 1:n, x=[x,i^2], end
or
x = [];
for i = 1:n
x = [x,i^2]
end
will produce a certain n-vector
DDE2023/03 14
For
>> x = []; for i = 1:n, x=[x,i^2], end
x=
1
x=
1 4
x=
1 4 9
x=
1 4 9 16
DDE2023/03 15
For
the statement
x = []; for i = n:-1:1, x=[x,i^2], end
will produce the same vector in reverse
order.
DDE2023/03 16
For
>> x = []; for i = n:-1:1, x=[x,i^2], end
x=
16
x=
16 9
x=
16 9 4
x=
16 9 4 1
DDE2023/03 17
For example
The statements
for i = 1:m
for j = 1:n
H(i, j) = 1/(i+j-1);
end
end
H
will produce and print to the screen the m-by-n
hilbert matrix
DDE2023/03 18
For
>> for i = 1:m
for j = 1:n
H(i, j) = 1/(i+j-1);
end
end
H
H=
1.0000 0.5000 0.3333 0.2500
0.5000 0.3333 0.2500 0.2000
0.3333 0.2500 0.2000 0.1667
0.2500 0.2000 0.1667 0.1429
0.2000 0.1667 0.1429 0.1250
DDE2023/03 19
While
While. The general form of a while loop is
while relation
statements
end
• The statements will be repeatedly executed
as long as the relation remains true.
DDE2023/03 20
While
for a given number a, the following will compute and
display the smallest nonnegative integer n such that
2^n>= a:
n = 0;
while 2^n < a
n = n + 1;
end
n
DDE2023/03 21
While
>> a=10;
>> n = 0;
while 2^n < a
n = n + 1;
end
n
n=
DDE2023/03 22
break
break
The break command breaks you out of the
innermost for or while loop you are in.
DDE2023/03 23
Graphics
Graphics
MATLAB can produce both planar plots and 3
- D
mesh surface plots.
Planar plots. The plot command creates linear x- y
plots; if x and y are vectors of the same length, the
command plot(x,y) opens a graphics window and
draws an x- yplot of the elements of x versus the
elements of y.
DDE2023/03 24
Planar plots or 2D plots
for example, draw the graph of the sine function over the
interval -4 to 4 with the following commands
x = -4:.01:4; y = sin(x); plot(x,y)
DDE2023/03 25
Graphics
y = e^(-x^2) over the interval -1.5 to 1.5 as follows:
>>x =- 1.5:.01:1.5; y = exp(- x.^2); plot(x,y)
DDE2023/03 26
Plots of parametrically defined curves
>> t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plot(x,y)
DDE2023/03 27
Plots of parametrically defined curves
The command grid will place grid lines on the current
graph.
The graphs can be given titles, axes labeled, and text
placed within the graph with the following commands
which take a string as an argument.
title graph title
xlabel x- axis label
ylabel y- axis label
gtext interactively
- positioned text
text position text at specified coordinates
DDE2023/03 28
Plots of parametrically defined curves
For example, the command
title('Best Least Squares Fit')
gives a graph a title.
The command
gtext('The Spot')
allows a mouse or the arrow keys to position a
crosshair on the graph, at which the text will be
placed when any key is pressed.
DDE2023/03 29
Plots of parametrically defined curves
By default, the axes are auto - scaled.
This can be overridden by the command axis.
If c = [xmin,xmax,ymin,ymax] is a 4- element vector,
then axis(c) sets the axis scaling to the precribed
limits.
By itself, axis freezes the current scaling for
subsequent graphs; entering axis again returns to
auto - scaling.
The command axis('square') ensures that the same
scale is used on both axes.
DDE2023/03 30
Plots of parametrically defined curves
Two ways to make multiple plots on a single graph are
illustrated by
x=0:.01:2*pi;y1=sin(x);y2=sin(2*x);y3=sin(4*x);plot(x,y1,x,y2,x,y3)
and by forming a matrix Y containing the functional
values as columns
x=0:.01:2*pi; Y=[sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y)
DDE2023/03 31
Plots of parametrically defined curves
One can override the default linetypes and pointtypes.
For example,
x=0:.01:2*pi; y1=sin(x); y2=sin(2*x); y3=sin(4*x);
plot(x,y1,'',x,y2,':',x,y3,'+')
renders a dashed line and dotted line for the first two
graphs while for the third the symbol + is placed at each
node. The line
- and mark- types are
Linetypes: solid (-), dashed (\tt--). dotted (:), dashdot (-.)
Marktypes: point (.), plus (+), star (*), circle (o), x-mark (x)
See help plot for line and mark colors.
DDE2023/03 32