CHAPTER 1: INTRODUCTION INTO MATLAB COMPUTING

Lecture Notes 1.2: Branch statements and loops

Comparison operators: (1 if true, 0 if not)

·         equal: ==

a = 4; b = 3; c = 4;

e1 = (a == b) % it is false

e2 = (a == c) % it is true

e1 =     0

e2 =     1

·         not equal: ~=

e3 = (a~=b) % it is true

e4 = (a~=c) % it is false

e3 =     1

e4 =     0

·         less than: <

·         equal or less than: <=

x = 0.1; y = x^1.1, z = x^0.9

e5 = (y <= x)

e6 = (z < x)

y =    0.0794

z =    0.1259

e5 =     1

e6 =     0

·         greater than: >

·         equal or greater than: >=

e7 = (y >= x)

e8 = (z > x)

e7 =     0

e8 =     1

·         comparison operators can be applied to vectors, they return vectors of ones and zeros

x = [ 1 2 3 4 5 6 ]

y = [ 2 1 4 3 6 5 ]

e = ( x < y)

x =

1     2     3     4     5     6

y =

2     1     4     3     6     5

e =

1     0     1     0     1     0

Logical operators and composite comparisons:

 a b & | ~a ~b 1 1 1 1 0 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 0 0 1 1

·         logical AND: &

n = 25; m = 5; l = 60;

e1 = ( (n >= 10) & (n <= 50) ) % it is equivalent to: 10 <= n <= 50

e2 = ( (m >= 10) & (m <= 50) )

e3 = ( (l >= 10) & (l <= 50) )

e1 =     1

e2 =     0

e3 =     0

·         logical OR: |

e4 = ( (n < 10) | (n > 50) ) % it is equivalent to: n < 10 or n > 50

e5 = ( (m < 10) | (m > 50) )

e6 = ( (l < 10) | (l > 50) )

e4 =     0

e5 =     1

e6 =     1

·         logical NOT: ~ (the use of this operator can be avoided)

e7 = ( ~(n <= 50) ) % it is equivalent to: n > 50

e8 = ( ~(m == 10) ) % it is equivalent to: m ~= 10

e9 = ( ~(l > 10) ) % it is equivalent to: l <= 10

e7 =     0

e8 =     1

e9 =     0

·         Typical errors in using logical operators

( ( n > 10 )  |  ( n < 15 ) ) % it is always true: n > 10 or n < 15

( ( m > 10 ) & ( m < 5 ) ) % it is always false: m < 5 and m > 10

ans =     1

ans =     0

·         any: returns 1 if any of the elements of a vector are non-zero, returns 0 otherwise

·         all: returns 1 if all of the elements of a vector are non-zero, returns 0 otherwise

·         find: returns the indices of the elements of a vector that are non-zero.

x =      0     1     1     1     1     0    -1    -1    -1    -1     0

r1 =     1

r2 =     0

r3 =     2     3     4     5     7     8     9    10

Branch statements:

·         if, elseif, else, end: (the parts with elseif and else are optional)

if <comparison>

<statements>

elseif <comparison>

<statements>

elseif <comparison>

<statements>

else

<statements>

end

a = 10; b = 100;

if ( a == b ) % NB: useful identation of the MATLAB codes

c = a*b

elseif ( b ~= 0 )

c = a/b % this branch is executed for the example

else

c = 0

end % c is assigned dynamically depending on a and b

Loops:

·         for, end

for <counts>

<statements>

end

for x = 1:0.5:3

y = x^2-5*x-3

end

y =    -7

y =   -8.2500

y =    -9

y =   -9.2500

y =    -9

x = [ 1 1.5 2 2.5 3 ];

for k = 1 : length(x)

y(k) = x(k)^2-5*x(k)-3 % NB: dynamical creation of the vector y

end

y =    -7

y =   -7.0000   -8.2500

y =   -7.0000   -8.2500   -9.0000

y =   -7.0000   -8.2500   -9.0000   -9.2500

y =   -7.0000   -8.2500   -9.0000   -9.2500   -9.0000

·         while, end

while < condition >

<statements>

end

x = [ 1 1.5 2 2.5 3 ]; n = 1;

while (n <= length(x))

y(n) = x(n)^2-5*x(n)-3 % NB: MATLAB vectors are created as row-vectors

n = n + 1;

end

y =    -7

y =   -7.0000   -8.2500

y =   -7.0000   -8.2500   -9.0000

y =   -7.0000   -8.2500   -9.0000   -9.2500

y =   -7.0000   -8.2500   -9.0000   -9.2500   -9.0000

·         break: terminate execution of a (for/end or while/end) loop

r = 10;

while (1) % infinite loop

r = 2*r;

if ( r > 100 )

break; % termination of the infinite loop

end

V = 4*pi*r^3/3

end

V =  3.3510e+004

V =  2.6808e+005

V =  2.1447e+006

When break is used in nested loops, only the immediate loop, where break is located, is terminated.