Professional Documents
Culture Documents
Lecture 02: Matlab Programming
Lecture 02: Matlab Programming
University
>> q= 2 ~= pi
q= 1
>> a= 1; b= 2; c= 3; d= b^2-4*a*c;
>> q= d >= 0 % q= 1 if roots are real
q=
0
>> roots([a b c]) % find roots
ans =
-1.0000 + 1.4142i
-1.0000 - 1.4142i
2. Logical operator
Logical Operation
p q P AND q P OR q NOT p
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
2. Logical operator
Logical Operators
Compare two operands and returns 1 or 0
expr1 op expr2
op expr
Operator Description
& Logical AND
| Logical OR
~ Logical NOT
&& Short-circuit logical AND
|| Short-circuit logical OR
2. Logical operator
expr1 && expr2 : expr2 is not evaluated if expr1= 0
expr1 || expr2 : expr2 is not evaluated if expr1= 1
>> x= 0; y= 1;
>> q= (x~=0) && (y>2)
q=
0
>> p= ~q % Negation of q
p=
1
>> r= (x~=1) || (y>2)
r=
1
2. Logical operator
>> A= [1 2 3 4;5 6 7 8]; A=
1 2 3 4
>> p= (A==5) 5 6 7 8
ans =
0 0 0 0
1 0 0 0
>> (A>3)&(A<=6)
ans =
0 0 0 1
1 1 0 0
3. Operator priority
Operator Priority
Priority Operators
1 ( ) (if nested parentheses exist, inner ones are first)
2 ^, ^, '
3 ~
4 *, /, \, *, /, \
5 +, -
6 :
7 >, <, >=, <=, ==, ~=
8 &
9 | Within each level,
10 operators are evaluated
&&
from left to right.
11 ||
3. Operator priority
>>x= (3/2+2+9*2)^2/20*2
x=
46.2250
Evaluation order
3/2= 1.5 21.5^2= 462.25
9*2= 18
1.5+2= 2.5 462.25/20= 23.1125
>> x= -3*pi:pi/20:3*pi;
>> x(find(x == 0))= eps; % Finds the zero elements of x
and replaces them with eps.
>> y= sin(x)./x;
>> plot(x,y)
5. For-end statement
For-end Statement
% Syntax
for var= s:step:f
statements;
end
var: variable
s: starting value
step: increment
f: upper limit
5. For-end statement
% For example
s= 0; % Initialize s with 0
for i=1:2:6
s= s+i
end
s= 1
s= 4
s= 9
5. For-end statement
Example 3: Calculate 10!= 123…10 using the
for-end statement
s= 0; % Initialize s n= 1:10;
for n=1:10 s= sum(n./(n+1)./(n+2));
s= s+n/(n+1)/(n+2); %s= sum(n./((n+1).*(n+2)))
end s
s
s= s=
1.1865 1.1865
5. For-end statement
Example 5: Write a script that sums all the elements
of a 33 magic matrix.
s= 0; % Initialize s with 0
A= magic(3); [n,m]= size(A);
for i= 1: n
for j= 1: m
s= s+A(i,j);
end
end
s
s= 45
6. While-end statement
While-end Statement
while expression
statements;
end
statements are repeated while expression is true
count= 0;
while count < 2
count= count+1
end
count= 1
count= 2
6. While-end statement
Example 6: Find the minimum number n such that
1+2+ +n is greater than 50 using the while-end
statement.
n= 1; % Initialize n with 1
while sum(1:n) <= 50
n= n+1;
end
[n sum(1:n)]
ans= 10 55
6. While-end statement
Example 7: Repeat the previous problem using the
while-end and break statements.
count= 1;
while (1)
if count > 10
break;
end
disp(count);
count= count+1;
end
ans= 9 45
7. If-end statement
If-end Statement
if expression
statements;
end
statements are executed if
expression is true
x= pi/4;
if sin(x) >= 0
disp('Yes, true');
end
Yes, true
7. If-end statement
If-else-end Statement
% Syntax
if expression
statement1;
else
statement2;
end
false
7. If-end statement
Example 8: Find two roots of ax2+bx+c= 0 (a= 1, b=
2, c= 3)
a= 1; b= 2; c= 3; d= b*b-4*a*c;
if d >= 0
x= [-b+sqrt(d); -b-sqrt(d)]/(2*a);
else
x= [-b+j*sqrt(-d); -b-j*sqrt(-d)]/(2*a);
end
x
x= -1.0000 + 1.4142i
-1.0000 - 1.4142i
7. If-end statement
If-elseif-else-end Statement
% Syntax
if expression1
statement1;
elseif expression2
statement2;
else
statement3;
end
% For example
for x= 0:0.5:2
fx= x*exp(-x)
pause
end
9. Break, Continue, Return, Pause
Example 13: Write an animation program that draws
the sin(x) graph.
clf
x= 0; h= 0.1; loop= 200; buf= [];
for i= 1:loop
buf= [buf;x sin(x)];
plot(buf(:,1), buf(:,2),'k.-'),
axis([0 h*loop -2 2])
x= x+0.1;
pause(0.01);
end
Q&A