Tuesday, March 21, 2017

                                                              Chapter-4
                                       PROGRAMMING IN MATLAB
RELATIONAL OPERATORS
A relational operator compares two numbers, it gives result equal to 1(logical true) if comparison according to relational operator is true and gives result equal to 0(logical false) if comparison according to relational operator is false.
RELATIONAL OPERATOR
DESCRIPTION
EXAMPLE
                       <
Less than
>> 5<8
ans =
         1
                       >
Greater than
>> 5>8
ans =
         0
                      <=
Less than or equal to
>> 5<=8
ans =
         1
                      >=
Greater than or equal to
>> 5>=8
ans =
         0
                      ==
Equal to
>> 5==5
ans =
         1
                      ~=
Not equal to
>> 5~=8
ans =
         1

·         If two scalars are compared, the result is a scalar 1 or 0. If two arrays are compared, comparison is done element by element and result is a logical array of same size with 1’s and 0’s according to the outcome of the comparison at each address. If scalar is compared with an array, scalar is compared with every element of the array, and result is a logical array with 1 and 0 according to the outcome of the comparison of each element.

>> b = [1 3 5 7];
>> c = [-1 4 6 1];
>> d = (b>=c)

d =

     1     0     0     1

>> a = [1 2 3;4 5 6;7 8 9];
>> b = (a>=6)

b =

     0     0     0
     0     0     1
     1     1     1

The result of relational operation, which are vectors with 1’s and 0’s are called logical vectors and they can be used for addressing vectors. It extracts from that vector the elements in the position where the logical vector has 1’s.
For example:
>> a = [3 7 8 9 2 1 10 11];
>> r = (a<=5) %Check which element of vector a is smaller than or equal to 5

r =

     1     0     0     0     1     1     0     0

>> t = a(r)%extracts those elements of a which are smaller than or equal to 5

t =

     3     2     1

·         Result of the relational operator can be used as an operand in arithmetical operations.

>> 3 + (4<16)/2
ans =
          3.5000

LOGICAL OPERATORS:
·         Logical operators have numbers as operands. A non-zero number is true, and a zero number is false. Logical operators (like relational operators) are used as arithmetic operators within a mathematical expression.

·         Logical operators (like relational operators) can be used with scalars and arrays.
LOGICAL OPERATOR
DESCRIPTION
EXAMPLE
                      &(AND)
Operates on two operands (A and B). If both are true (non-zero number), result is true(1), otherwise result is false.
>> 3&7
ans =

     1
                      |(OR)
Operates on two operands (A and B). If anyone is true(non-zero number), the result is true (1), otherwise (both are false) result is false.
>> 6|0

ans =

     1
                      ~(NOT)
Operates on one operand (A). It gives the opposite of operand. True(1) if the operand is false and false(0) if the operand is true.
>> ~5

ans =

     0

The following are examples of expression that include arithmetic, relational and logical operators.
>> a = 2;b = 3;
>> x = ((a<5)&(1<a))
x =
      1
>> y = ((a>=8)|(b<=1))
y =
     0
>> ~y
ans =
     1
>> ~((a<=8)&(b>=1))
ans =
     0

SOME OTHER MATLAB LOGICAL FUNCTIONS





















PROGRAM FOR ANALYSIS OF TEMPERATURE DATA 
Q) The followings were the average temperatures in Washington DC on the days of the month of April 2002: 58 73 73 53 50 48 56 73 73 66 69 63 74 82 84 91 93 89 91. Use relational and logical operations to determine the following:
a) number of days temperature was above 75.

b) number of days the temperature was between 65 and 80.

c) The dates of the month on which the temperature was between 50 and 60.

clc
close all
clc
clear all
T = [58 73 73 53 50 48 56 73 73 66 69 63 74 82 84 91 93 89 91 80 59 69 56 64 63 66 64 74 63 69];
Tempabove75 = (T>=75);
%relational operator (>=) greater than or equal to is used. It returns an
%array consisting of 1 and 0, It returns 1 where condition is satisfied
%otherwise returns 0
NdaysTempabove75 = sum(Tempabove75);
%calculates sum of array consisting of 1 where condition(T>=75) is
%satisfied and 0 where condition is not satisfied
fprintf('The number of days on which temperature was above 75 = %f\n',NdaysTempabove75);
Tempbetween65and80 = ((T>=65)&(T<=80));
%returns a vector with 1's at address where T>=65 and T<=80
NdaysTempbetween65and80 = sum(Tempbetween65and80);
fprintf('The number of days on which temperature was between 65 and 80 = %f\n',NdaysTempbetween65and80);
Tempbetween50and60 = ((T>=50)&(T<=60));
%returns a vector with 1's at address where T>=50 and T<=60
datesTempbetween50and60 = find(Tempbetween50and60);
%returns a vector containing addresses of elements where T>=50 and T<=60
disp('dates on which temperature was between 50 and 60');
fprintf('%f\n',datesTempbetween50and60);



CONDITIONAL STATEMENTS:

A Conditional statement is a command that allows MATLAB to make a decision of whether to execute a group of commands that follow the conditional statement, or to skip these commands.
Syntax for conditional statement is following:

If conditional expression

Examples:
if a<b
if c>=4
if ((d>h)&&(a>b))
if ((x~=13)&&(a~=b))

The if statement is commonly used in three structures, if-end, if-else-end and if-elseif-else-end.

The if-end structure:

If the conditional expression in the if statement is true (1), the program continues to execute the commands that are between the if statement and the end statement. If the conditional expression is false (0), the program skips the group of MATLAB commands between the if and the end, and continues with the commands that follow the end.

 Fig. 4.1 The structure of the if-end conditional statement


 The if-else-end structure:
It provides a means for choosing one group of commands, out of a possible two groups, for execution. If the conditional expression is true, the program executes group 1 of commands between the if and the else statements and after that it skips to the end. If the conditional expression is false, the program skips to the else, and then executes group 2 of commands between the else and the end.


Q) Write a program in script file that takes two scalars and finds the largest scalar and prints it?

clc
clear all
close all
a = input('enter first number');
b = input('enter second number');
if a>=b
    largestnumber = a;
else
    largestnumber = b;  
end
fprintf('largest scalar is %f',largestnumber);


 Q) Up to 40 hours, A worker is paid according to his hourly wage and 50% more wage is paid for overtime. Write a  MATLAB program in a script file that calculates the pay to a worker. The MATLAB program asks the user to enter hourly wage and the number of hours. The program then displays the pay?

clc
close all
clear all
N = input('number of hours completed by worker');
T = input('hourly wage of worker');
if (N<=40)
    M = N*T;
else
    M = T*40 + (1.5*T)*(N-40);
    %worker is getting same salary for 40 hours and 50% more salary for more
    %than 40 hours
end
fprintf('pay of worker = %f',M);

The if-elseif-else-end structure:
It includes two conditional statements (if and elseif) which make it possible to select one out of three groups of commands for execution.
The first command is an if statement with a conditional expression. If the conditional expression is true, the program executes group 1 of matlab commands between the if and the elseif statement and then skips to the end. If the conditional expression in the if statement is false, the program skips to the elseif statement. If the conditional statement in the elseif statement is true, the program executes group 2 of matlab commands between the elseif and else and after that it skips to the end. If the conditional expression in the elseif statement is false, the program skips to the else and executes group 3 of matlab commands between the else and the end.



Q) write a program in which program asks the user to enter age of Rahul. If his age is equal to 25, display ‘He is eligible bachelor’. If his age is less than 25 display ‘He is young’. If his age is more than 25, display ‘He is old’?


clc
clear all
close all
a = input('enter age of Rahul');
if a==25
    disp('Rahul is eligible bachelor');
elseif a<25
    disp('Rahul is young');
else
    disp('Rahul is old');  
end




No comments:

Post a Comment