CH6: Programming in MATLAB 1- Relational and Logical operators: Relational operators: Examples Result >> 5>8 0 >> a= 6~=2 a=1 >> b= (3>2)+(5*2==10*1)*(32>=128/4) b=2 >> c= 8/(2<5)*3+(1>9) c=?? Order of precedence: 1- Parentheses 2- Exponentiation 3- Multiplication, division 4- Addition, subtraction 5- Relational operators (>, <, >=, <=, = =, ~=) 1
Examples Result >> c=[5 12 50 21 1 4]; d=12; >> e= c>=d e=[0 1 1 1 0 0] >> f=[3 8 15 6 7 12]; >> h= f < c h=[1 1 1 1 0 0] >> a=[2 9 4; -3 5 2; 6 7-1];b=2; >> b= a<=b b= 1 0 0 1 0 1 0 0 1 >> 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) t= [8 9 4 10] w=?? 2
Logical operators: &, and(a,b) Gives 1 if A and B are nonzero, or(a,b) Gives 1 if A or B are nonzero ~, not(a) Gives 1 if A are zero xor(a,b) Gives 1 if just one is zero all(a) Gives 1 if all elements are nonzero any(a) find(a) Gives 1 any element are nonzero Gives indices of nonzero elements Examples Result >> 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) ans=1 ans=1 ans=1 m=60 t=?? x=?? 3
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- if-else- if-elseif-else- 4
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; z Result x=5,y=6 z=60 x=4,y=3 z=36 x=input('enter the first value'); y=input('enter the second value'); if x>=y z=x*3*y; else z=x*2*y; z x=5,y=5 z=75 x=5,y=6 z=60 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; z 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 Result 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') n=-1 negative one n=5 other value 5
6
7
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- 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 s, the loop index variable (k) has the value that was last assigned to it. Example: for k=1:3:10 x=3*k 3 12 21 30 In some situations the same result can be obtained by either using for- loops or using element-by-element operations. In general, element-by-element operations are faster than loops and are recommed when either method can be used. 8
while- Loops while- 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 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 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. Example: x=1; while x<12 x=x^2+1 2 5 26 the user can stop the execution of an indefinite loop by pressing the Ctrl + C 9
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 2 5 26 10
من المهم جدا مراجعة أمثلة هذا الشبتر في الكتاب Q1: i=132^2; for n=1:2*i if (rem(n,2)~=0) & (rem(n,11)==0) & (sqrt(n)>132) break 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; x 11
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); y 12
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; 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; if error disp('error current or new units are typed incorrectly.') else fprintf('v= %g %s\n',vout,voutunits) 13