DDE 2023 MATLAB Fundamentals: MATLAB Programming and Graphics
DDE 2023 MATLAB Fundamentals: MATLAB Programming and Graphics
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
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
& and
| or
~ not.
DDE2023/03 10
Control Statements
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=
DDE2023/03 19
While
While. The general form of a while loop is
while relation
statements
end
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.
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
DDE2023/03 27
Plots of parametrically defined curves
DDE2023/03 28
Plots of parametrically defined curves
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
DDE2023/03 30
Plots of parametrically defined curves
x=0:.01:2*pi;y1=sin(x);y2=sin(2*x);y3=sin(4*x);plot(x,y1,x,y2,x,y3)
DDE2023/03 31
Plots of parametrically defined curves
DDE2023/03 32