Conditional Control in MATLAB Scripts
Click here to load reader
-
Upload
shameer-ahmed-koya -
Category
Education
-
view
667 -
download
0
Transcript of Conditional Control in MATLAB Scripts
![Page 1: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/1.jpg)
CONDITIONAL CONTROLMatlab Scripts
![Page 2: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/2.jpg)
FLOW CONTROL IN MATLAB Conditional control – if, else, switch Loop control – for, while, continue, break Program termination – return
2
if, else, and elseif for while
![Page 3: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/3.jpg)
CONDITIONAL CONTROL – IF, ELSE, ELSEIF
if test statement statementselseif test statement statementselse statementsend
3
if I == JA(I,J) = 2;
elseif abs(I-J) == 1A(I,J) = -1;
else A(I,J) = 0;end
• Conditional statements are commands that allows MATLAB to decide whether or not to execute some code that follows the statement
• Conditional statements use relational operators like ==,~=,>,< (Note that are all scalar tests)
![Page 4: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/4.jpg)
IF / ELSE / ELSEIF
If the conditional expression is true, MATLAB runs the lines of code that are between the line with if and the line with end.
If the conditional expression is false, MATLAB skips the code between if and the line with else and runs the code up to end.
![Page 5: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/5.jpg)
height=input(‘Plz enter the height:‘)if height>170 disp('tall')elseif height<150 disp('small')else disp('average')end
EXAMPLES
![Page 6: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/6.jpg)
EXAMPLESA program to make a discount of 20% if the no.
of books are greater than 5clc; books=input(‘Enter the no of Books: ');cost=books*25;if books > 5cost=(1 -20/100)*cost; % 20% discountdisp(cost)end
![Page 7: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/7.jpg)
EXAMPLESThe following program check if the input digital voltage
signal is positive or not.V=input(‘Please Enter the Voltage: ')if V < 5 % If voltage is less than 5 V out_1 = 0 %Output is zeroelseif V == 5 % If voltage is equal to 5 V out_1 = 0.5 % Output is o.5else out_1 = 1 % In other cases output is 1end
![Page 8: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/8.jpg)
EXAMPLEWrite a MATLAB program to input the values of x,y then
print 'y is greater than x‘ if y is greater and 'x is greater than y‘ if x is greater and 'x =y‘ if they are equal.
clear all,clc; x=input('enter the value x ');y=input('enter the value y ');if (x<y) disp ('y is greater than x');elseif (x>y) disp ('x is greater than y');else disp ('x is =y');end
![Page 9: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/9.jpg)
EXAMPLES Program to find grade from the mark
s=input('Enter the Mark: '); % enter the markif s>= 90 disp ('Grade: A');elseif s>=80 disp ('Grade: B');elseif s>=70 disp ('Grade: C');elseif s>=60 disp ('Grade: D');else disp ('Grade: F');end
9
![Page 10: Conditional Control in MATLAB Scripts](https://reader038.fdocuments.us/reader038/viewer/2022100504/5871ea631a28ab6a7b8b7d69/html5/thumbnails/10.jpg)
Shameer KoyaTHANK
YOU