matab no3

14

Click here to load reader

Upload: moeen-khan-afridi

Post on 08-Jul-2015

32 views

Category:

Education


1 download

TRANSCRIPT

Page 1: matab no3

Structures, logical and relational operators

Designed by : Dawar [email protected]

March – July 2012CECOS College of Engineering and IT

Lab No.03

Page 2: matab no3

March – July 2012CECOS College of Engineering and IT

Relational and logical operators

< less than> greater than<= less than or equal>= greater than or equal== equal~= not equal

Relational operators

Their result will be either true (1) or false (0)

>> 3 < 5 returns 1

>> a = 3 == 5 returns 0

These operators when applied on matrices, returns 1s and 0s in

a resultant matrix

Page 3: matab no3

March – July 2012CECOS College of Engineering and IT

Relational and logical operators

>> A = [ 1 2; 3 4 ];

>> B = [ 6 7; 8 9 ];

>> A == B

ans =

0 0

0 0

>> A < B

ans =

1 1

1 1

Page 4: matab no3

Try the following commands

>> ~A >> A | ~A

>> A&B >> A | B

>> A & ~B

March – July 2012CECOS College of Engineering and IT

Relational and logical operators

& and

| or

~ not

Logical operators

Page 5: matab no3

March – July 2012CECOS College of Engineering and IT

Control structures

Structures are used to control the sequence of

execution of code. This is achieved by using

Conditional control structures

Looping control structures

Logical and relational operators are used in

implementing the control structures.

Page 6: matab no3

March – July 2012CECOS College of Engineering and IT

Conditional Control structures

Following are some conditional/branching control

structures

i- if-end Construct : The most basic construct is

if <condition>

<program>

end

Example

a = 1;

b = 2;

if a < b

c = 3;

end;

Page 7: matab no3

March – July 2012CECOS College of Engineering and IT

Conditional Control structures

ii- If-else-end Construct

if <condition1>

<program1>

else

<program2>

end

Example

a = 1;

b = 2;

if a < b

c = 3;

else

c=10;

end;

Page 8: matab no3

March – July 2012CECOS College of Engineering and IT

Conditional Control structures

iii- If-elseif-end Construct: Another variation is

if <condition1>

<program1>

elseif <condition2>

<program2>

end

Examplea = 1;b = 2;if a < bdisp(‘b is greater’);elseif (a>b)disp(‘a is greater’);elsedisp(‘ a and b are equal’);end;

Page 9: matab no3

March – July 2012CECOS College of Engineering and IT

Looping Control structures

i- For loop

for i= 1 : n

<program>

end

For each value of ‘i’ , “program” executes one time i.e the program

will execute n-times

>> for i = 1 : 5

c = 2*i

end

c = 2 c = 4 …. … …. …. c = 10

Page 10: matab no3

March – July 2012CECOS College of Engineering and IT

Looping Control structures

Any vector can be used for the value of i

for i = [2,4,5,6,10]

<program>

end

Page 11: matab no3

March – July 2012CECOS College of Engineering and IT

Looping Control structuresii- Nested For loop

for i=1:3

for j=1:3

A(i,j) = i+j;

end

end

For each value of ‘i’ , “program” executes three times i.e the

program will execute 9 times.

A(1,1)=1+1 A(1,2)=1+2 A(1,3)=1+3 A(2,1)=2+1

…… ….. …. ….. … …. A(3,3)=3+3

Program

Page 12: matab no3

March – July 2012CECOS College of Engineering and IT

While loops

while <condition>

<program>

end

The ‘program’ executes till the ‘condition’ remains true,

and stops when the ‘condition’ becomes false

Page 13: matab no3

March – July 2012CECOS College of Engineering and IT

Task

1. Create an m-file that finds the factorial of a number using for

loop (don’t use the built in function ‘factorial’).

2. Repeat the above question for while loop.

3. Generate square of first ten integers using for loop.

4. Add the following two matrices using nested for loop.

Page 14: matab no3

March – July 2012CECOS College of Engineering and IT

Task

5. Using MATLAB, Find the value of C, where C is defined as

follows:

C= 10a , 0<a<=5

C= 20a , 5<a<=10

The value of ‘a’ shall be provided by the user.

6. Using MATLAB, List all the values of y, for 0 ≤ n ≤ 10. if

y=n2 , n is even

y=n , n is odd