0% found this document useful (0 votes)
47 views

Lab 3

The document discusses conditional statements and logical operators in MATLAB. It begins by introducing conditional statements and logical operators. It then provides examples of using IF statements to control program flow based on conditions. It also discusses comparison operators and logical operators that are used to evaluate conditions. Finally, it provides two tasks as examples - one for determining severity of cataract based on input, and another for diagnosing likelihood of tuberculosis based on age and reported symptoms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Lab 3

The document discusses conditional statements and logical operators in MATLAB. It begins by introducing conditional statements and logical operators. It then provides examples of using IF statements to control program flow based on conditions. It also discusses comparison operators and logical operators that are used to evaluate conditions. Finally, it provides two tasks as examples - one for determining severity of cataract based on input, and another for diagnosing likelihood of tuberculosis based on age and reported symptoms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab-3

Conditional Statements and Logical Operators in Matlab


3.1 Objective

• To give overview of conditional statements


• To give overview of logical operators
• To solve biomedical problems with conditional statements and logical operators

3.2 Introduction

In the previous labs, we introduced the fundamental concepts of computer programming, as well as
the basics of the MATLAB software package. Computer programming not only involves executing
several commands but controlling the sequence of execution (or control flow) using special
programming constructs. In this lab we introduce some of the programming constructs provided by
MATLAB.

3.3 Conditional IF Statements

The normal flow of control in a MATLAB script, and procedural programming in general, is sequential,
i.e. each program statement is executed in sequence, one after the other. This is illustrated in Fig. 3.1.

Figure 3.1 The normal sequential control flow in procedural programming

Figure 3.2 The control flow of a conditional statement

In computer programming, if all our programs featured only sequential control flow they would be
limited in their power. To write more complex and powerful programs we need to make use of
programming constructs that alter this normal control flow. One type of programming construct is the
conditional statement. The control flow of a conditional statement is illustrated in Fig. 3.2. In this
example there are two possible paths through the program, involving execution of different
statements. Which path is taken depends on the result of applying a test condition.

MATLAB if statements are one way of achieving a conditional control flow. The use of an if statement
is illustrated in the example below.
Remember if you like to put commands or make your program in .m file than just write than name of
saved file in command line to execute the program. On the other hand, if you like enter all commands
in command line before running any command use Shift+ Enter between each statements.

3.2.1 Example-1

a = input('Enter a number:');

if (a >= 0)

root = sqrt(a);

disp(['Square root = ' num2str(root)]);

else

disp(['Number is -ve, there is no square root']);

end

3.2.2 Example-2

a = input('Enter Any Number');

b = input('Enter second number');

r = rem( a , b )

if(r ==0)

disp('Number is Even')

else

disp('Number is Odd')

end

3.3 Comparison/Logical Operators

Example 3.2.1 and 3.2.2 introduced the concept of a condition. Conditions commonly involve
comparisons between expressions involving variables and values. For example, we saw the > and <
comparison operator in the example. A list of common comparison (or relational) operators such as >
is shown in Table 3.1, along with common logical operators for combining the results of different
comparisons.
Table 3.1 Common relational and logical operators

Most of these operators are intuitive. However, note the distinction between & and && (and likewise
between | and ||). The & and | operators perform AND and OR operations respectively. They will
evaluate the expressions on both sides of the operator and return a true or false value depending on
whether both (&) or (|) of them evaluated to true.

These operators will work with either scalar (i.e. single value) logical expressions or array expressions
(i.e. that evaluate to an array of true/false values). The only restriction is that MATLAB must be able
to match the expressions on either side of the operator. Either both should be scalars, both should be
arrays of the same size, and one should be an array and the other a scalar.

The && and || operators perform the same AND and OR operations but using what is known as a
short-circuiting behavior. This means that, if the result of the overall AND/OR operation can be
determined from the left-hand expression alone, then the right-hand expression will not be evaluated.
For example, if the left-hand expression of an AND operation is false then the result of the AND will
also be false, regardless of the value of the right-hand expression. Therefore, the advantage of short-
circuiting is that unnecessary operations are not performed. However, note that && and || can only
be used with scalar values, not arrays. The use of these relational and logical operators is illustrated
in the following code excerpt.

3.3.1 Example

age = input('Enter your age');

if (age>=10) && (age<=20)

disp('You are younger')

end

if(age>20) && (age<=30)

disp('You are getting old')

end

Here, in both if statements, the Boolean variable is set to true only if both comparisons evaluate to
true. If either comparison evaluates to false the flow of execution will pass to after the corresponding
end statement. We can use the short-circuiting version of the AND operator as both sides of the
operator are scalar (logical) values (i.e. not arrays).
There are two different operators in this condition: && and >. In what order would MATLAB evaluate
them? The answer to this question lies in the rules for operator precedence. Whenever MATLAB needs
to evaluate an expression containing multiple operators it will use the order of precedence shown in
Table 3.2. We have come across all these operators before with a few exceptions. The ' operator, when
placed after a matrix, is just a short way of calling the transpose function, which we introduced earlier.
The unary plus and minus operators refer to a + or - sign being placed before a value or variable to
indicate its sign

Table 3.2 MATLAB Operator Precedence

Now let’s return to our condition without brackets. Of the two operators present (&& and >), we can
see from Table 3.2 that the ones with the highest precedence are >and &&.

In this example, removing the brackets still resulted in the desired behavior. However, this will not
always be the case. Furthermore, even if the brackets are not essential it is still a good idea to include
them to make our code easier to understand and less prone to errors.

3.4 Tasks

3.4.1 Cataract is an eye disease which forms cloudy area in the eye lens. Write a program in
Matlab using conditional statements to show the severity of this disease. Input will be taken
by user in numerical form. The Output should be in the form of Low, normal, and High in
range. The ranges are given below:

Low<70 or equal to 70 Normal >70 and less or equal to 90 High>90

Code

C =input('Enter numerical form of Cataract \n');

if C<=70

fprintf('Low \n')

elseif C>70 && C<=90

fprintf('Normal \n')
elseif C>90

fprintf('High \n')

end

Output

Figure 3.3

3.4.2 Tuberculosis (TB) is a disease caused by bacteria called Mycobacterium tuberculosis.


The bacteria usually attack the lungs, but they can also damage other parts of the body.
Write a Matlab problem in which user will be shown some symptoms of tuberculosis and
asked the user for patient age. On the basis of age and symptoms the program will be able to
diagnose the chance of disease.

Age<30 No chance Age=30 or <50 slighter chances Age=50 or >50 High chances

Code

A=input('Enter your age \n');

S1=input('Press 1 if you have cough that lasts more than three weeks otherwise enter any other
number \n');

S2=input('Press 2 if you have Loss of appetite and unintentional weight loss otherwise enter any
other number \n');

S3=input('Press 3 if you have Fever,Chills,Night sweats otherwise enter any other number \n');

if A<30 && S1==1 && S2==2 && S3==3

fprintf('From age point of view you have no chance but your symptoms show you have TB \n')

elseif A<30 && S1==1 && S2~=2 && S3~=3

fprintf('From age point of view you have no chance but you have 1 symptom of TB \n')

elseif A<30 && S2==2 && S1~=1 && S3~=3


fprintf('From age point of view you have no chance but you have 1 symptom of TB \n')

elseif A<30 && S3==3 && S2~=2 && S1~=1

fprintf('From age point of view you have no chance but you have 1 symptom of TB \n')

elseif A<30 && S1==1 && S2==2 && S3~=3

fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')

elseif A<30 && S1==1 && S3==3 && S2~=2

fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')

elseif A<30 && S2==2 && S3==3 && S1~=1

fprintf('From age point of view you have no chance but you have 2 symptoms of TB \n')

elseif A<30 &&S1~=1 && S2~=2 && S3~=3

fprintf('From age point of view you have no chance and also your symptoms show you have no
TB \n')

elseif A>=30 && S1==1 && S2==2 && S3==3

fprintf('From age point of view you have slighter chances but your symptoms show you have TB
\n')

elseif A>=30 && S1==1 && S2~=2 && S3~=3

fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')

elseif A>=30 && S2==2 && S1~=1 && S3~=3

fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')

elseif A>=30 && S3==3 && S2~=2 && S1~=1

fprintf('From age point of view you have slighter chances and also you show 1 symptom of TB \n')

elseif A>=30 && S1==1 && S2==2 && S3~=3

fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')

elseif A>=30 && S1==1 && S3==3 && S2~=2

fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')

elseif A>=30 && S3==3 && S2==2 && S1~=1

fprintf('From age point of view you have slighter chances and also you show 2 symptoms of TB
\n')

elseif A>=30 && S1~=1 && S2~=2 && S3~=3

fprintf('From age point of view you have slighter chances but your symptoms show you have no
TB \n')
elseif A>=50 && S1==1 && S2==2 && S3==3

fprintf('From age point of view you have higher chances and also your symptoms show you have
TB \n')

elseif A>=50 && S1==1 && S2~=2 && S3~=3

fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')

elseif A>=50 && S2==2 && S1~=1 && S3~=3

fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')

elseif A>=50 && S3==3 && S2~=2 && S1~=1

fprintf('From age point of view you have higher chances and also you show 1 symptom of TB \n')

elseif A>=50 && S1==1 && S2==2 && S3~=3

fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')

elseif A>=50 && S1==1 && S3==3 && S2~=2

fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')

elseif A>=50 && S3==3 && S2==2 && S1~=1

fprintf('From age point of view you have higher chances and also you show 2 symptoms of TB \n')

elseif A>=50 && S1~=1 && S2~=2 && S3~=3

fprintf('From age point of view you have higher chances but your symptoms show you have no TB
\n')

end

Output

Figure 3.4
Figure 3.5

3.4.3 If a=1, b=2 and c=3, use your knowledge of the rules of operator precedence to predict
the values of the following expressions (i.e. work them out in your head and write down the
answers). Verify your predictions by evaluating them using MATLAB.

1. a+b ∗ c
2. a ^ b+c
3. 2 ∗ a == 2 && c - 1 == b
4. b + 1:6

Output

Figure 3.6
Figure 3.7

3.4.4 Body mass index (BMI) can be found, according to the formula based on a height (in
meters) and weight (in kilograms) entered using the keyboard.

𝐵𝑀𝐼 = 𝑚𝑎𝑠𝑠/ℎ𝑒𝑖𝑔ℎ𝑡 2

Write a Matlab program which will be able to display BMI with type of body of a person after
calculating it using the above stated formula.

Code

W=input('Enter weight in kgs \n');

H=input('Enter height in meters \n');

BMI=W/H^2;

if BMI<=18.5

fprintf('You are underweight \n')


elseif BMI>18.5 && BMI<=25

fprintf('You have normal weight \n')

elseif BMI>25 && BMI<=30

fprintf('You are overweight \n')

elseif BMI>30

fprintf('You have health problems')

end

Output

Figure 3.8 Output

Figure 3.9 Second ouput

You might also like