ch6: programming in matlab -...

13
Matlab : CH6 Eng. Mohammed H. Nazly 1 CH6: Programming in MATLAB 1- Relational and Logical operators: Relational operators: Examples >> 5>8 >> a= 6~=2 >> b= (3>2)+(5*2==10*1)*(32>=128/4) >> c= 8/(2<5)*3+(1>9) Result 0 a=1 b=2 c=?? Order of precedence: 1- Parentheses 2- Exponentiation 3- Multiplication, division 4- Addition, subtraction 5- Relational operators (>, <, >=, <=, = =, ~=)

Upload: buituyen

Post on 16-Apr-2018

236 views

Category:

Documents


9 download

TRANSCRIPT

Matlab : CH6 Eng. Mohammed H. Nazly

1

CH6: Programming in MATLAB

1- Relational and Logical operators:

Relational operators:

Examples

>> 5>8

>> a= 6~=2

>> b= (3>2)+(5*2==10*1)*(32>=128/4)

>> c= 8/(2<5)*3+(1>9)

Result

0

a=1

b=2

c=??

Order of precedence:

1- Parentheses

2- Exponentiation

3- Multiplication, division

4- Addition, subtraction

5- Relational operators (>, <, >=, <=, = =, ~=)

Matlab : CH6 Eng. Mohammed H. Nazly

2

Examples

>> c=[5 12 50 21 1 4]; d=12;

>> e= c>=d

>> f=[3 8 15 6 7 12];

>> h= f < c

>> a=[2 9 4; -3 5 2; 6 7 -1];b=2;

>> b= a<=b

>> r=[8 12 9 4 23 19 10];

>> s=r<=11

s=[1 0 1 1 0 0 1]

>> t=r(s)

>> w=r(r<=9)

Result

e=[0 1 1 1 0 0]

h=[1 1 1 1 0 0]

b=

1 0 0

1 0 1

0 0 1

t= [8 9 4 10]

w= ??

Matlab : CH6 Eng. Mohammed H. Nazly

3

Logical operators:

& , and(A,B)

| , or(A,B)

~ , not(A)

xor(A,B)

all(A)

any(A)

find(A)

Gives 1 if A and B are nonzero Gives 1 if A or B are nonzero Gives 1 if A are zero

Gives 1 if just one is zero Gives 1 if all elements are nonzero Gives 1 any element are nonzero Gives indices of nonzero elements

Examples

>> 5&8

>> 3|0

>> ~50

>> m = 30*(2*(5*6|3*0)+(5&0*~0))

>> t = (-5<2<-1)*(~(5>7))

>> x= ~(5>=8)|(2<-1)

Result

ans=1

ans=1

ans=1

m=60

t=??

x=??

Matlab : CH6 Eng. Mohammed H. Nazly

4

Examples

>> k=[5 8 0 3 12];l=[6 7 8 0 3];

>> x= k&l

>> y= or(k,l)

>> z= all(k)

>> h= any(l)

>> m= xor(k,l)

>> find(k)

>> or(k>7,l)

>> 5 & (k<6)

Result

x=[1 1 0 0 1]

Y=[1 1 1 1 1]

Z=0

h=1

m=[0 0 1 1 0]

ans=[1 2 4 5]

ans=??

ans=??

2- Conditional Statements:

It has 3 structures

If-end if-else-end if-elseif-else-end

Matlab : CH6 Eng. Mohammed H. Nazly

5

Examples in the script file

x=input('enter the first value'); y=input('enter the second value'); z=x*2*y; if x>y z=x*3*y; end z

x=input('enter the first value'); y=input('enter the second value'); if x>=y z=x*3*y; else z=x*2*y; end z

x=input('enter the first value'); y=input('enter the second value'); if x>y z=x*3*y; elseif x<y z=x*2*y; else z=x*y; end z

Result

x=5,y=6

z=60

x=4,y=3

z=36

x=5,y=5

z=75

x=5,y=6

z=60

x=5,y=5

z=25

x=5,y=6

z=60

x=6,y=5

z=90

THE switch-case STATEMENT

Examples in the script file

n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end

Result

n=-1

negative one

n=5

other value

Matlab : CH6 Eng. Mohammed H. Nazly

6

Matlab : CH6 Eng. Mohammed H. Nazly

7

Matlab : CH6 Eng. Mohammed H. Nazly

8

Loops:

-In a loop, the execution of a command, or a group of commands, is repeated several times consecutively. Each round of execution is called a pass. for-end Loops

the number of passes is specified when the loop starts.

The increment s can be negative,

If the increment value s is omitted, the value is 1 (default)

If f = t, the loop is executed once.

The value of k should not be redefined within the loop.

When the loop ends, the loop index variable (k) has the value that was last assigned to it. Example: for k=1:3:10

x=3*k

end

x =

3

x =

12

x =

21

x =

30

In some situations the same end result can be obtained by either using for-end loops or using element-by-element operations. In general, element-by-element operations are faster than loops and are recommended when either method can be used.

Matlab : CH6 Eng. Mohammed H. Nazly

9

while-end Loops while-end loops are used in situations when looping is needed but the number of passes is not known in advance.

-The first line is a while statement that includes a conditional expression. -When the program reaches this line the conditional expression is checked. -If it is false (0), MATLAB skips to the end statement and continues with the program. -If the conditional expression is true (1), MATLAB executes the group of commands that follow between the while and end commands. -The conditional expression in the while command must include at least one variable. -The variables in the conditional expression must have assigned values when MATLAB executes the while command for the first time. At least one of the variables in the conditional expression must be assigned a new value in the commands that are between the while and the end.

Example: x=1; while x<12 x=x^2+1 end

x = 2 x = 5 x = 26

the user can stop the execution of an indefinite loop by pressing the Ctrl + C

Matlab : CH6 Eng. Mohammed H. Nazly

10

NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS There is no limit to the number of loops and conditional statements that can be nested.

The break command: When inside a loop (for or while), the break command terminates the execution of the loop (the whole loop, not just the last pass). The break command is usually used within a conditional statement. The continue command: The continue command can be used inside a loop (for or while) to stop the present pass and start the next pass in the looping process. The continue command is usually a part of a conditional statement.

Example:

x=1; while x>0.5 if x<15 x=x^2+1 else break end

end

x = 2 x = 5 x = 26

Matlab : CH6 Eng. Mohammed H. Nazly

11

من المهم جدا مراجعة أمثلة هذا الشبتر في الكتاب

Q1:

i=132^2; for n=1:2*i if (rem(n,2)~=0) & (rem(n,11)==0) & (sqrt(n)>132) break end end fprintf('The required number is: %i\n',n)

Q2:

x=[-3.5 5 -6.2 11.1 0 7 -9.5 2 15 -1 3 2.5]; n=length(x); for i=1:n-1 A=x(i); for j=i+1:n if x(j)<A A=x(j); x(j)=x(i); x(i)=A; end end end x

Matlab : CH6 Eng. Mohammed H. Nazly

12

Q3: write a program that eliminates the elements that devisable to 3 , 4 , 5 from a vector that inserted by the user.

x=input('enter the vector : '); n=length(x) m=0; for k=1:n if rem(x(k),3)~=0 & rem(x(k),4)~=0 & rem(x(k),5)~=0 m=m+1; y(m)=x(k); end end y

Matlab : CH6 Eng. Mohammed H. Nazly

13

Q4:

Vin=input('Enter the value of the volume to be converted:');

VinUnits=input('Enter the current units (m3, Liter, ft3, or USGallon): ','s');

VoutUnits=input('Enter the new units (m3, Liter, ft3, or USGallon): ','s');

error=0;

switch VinUnits

case 'm3'

VL=Vin*1000;

case 'Liter'

VL=Vin;

case 'ft3'

VL=Vin*(12*0.254)^3;

case 'USGallon'

VL=Vin*3.78541178;

otherwise

error=1;

end

switch VoutUnits

case 'm3'

Vout=VL/1000;

case 'Liter'

Vout=VL;

case 'ft3'

Vout=VL/(12*0.254)^3;

case 'USGallon'

Vout=VL/3.78541178;

otherwise

error=1;

end

if error

disp('ERROR current or new units are typed incorrectly.') else

fprintf('V= %g %s\n',Vout,VoutUnits)

end