Assignment No.
04
Q-1. Using while loop adds the values in a vector to a sum until a negative value is
reached.
x = [1 3 5 -7 9];
sum = 0;
k = 1;
while x(k) >= 0 && k <= length(x) sum = sum + x(k);
k = k+1;
disp('sum');
disp(sum);
end
Result: k = 4 , sum = 9
Explanation: First of all, create a vector X with a element 13 5 - 7 and 9. Then initialize
sum=0 and k=1.using a while loop by checking the condition ( x(k)>=0 &&
k<=length(x)_)if this condition is satisfies then the loop is executed otherwise the loop is
terminated. First of all compiler is checking the condition by taking a value of k =1 . x(k)
mean element of the vector, by taking k =1, x(1) = 1 and it is greater than zero condition is
satisfied but there is a AND operator so second condition also must be satisfied which is
k<= length(x) . length(x) mean number of element in vector x and in this case it is 5. so
second condition is also satisfies ( 1<=5). After that next command is executed, which is
(sum = sum + x(k) ) and this value is store in sum. The next command is incrementation of
k . Then again condition is check if it is satisfies the loop is executed otherwise loop is
terminated. At the end disp(sum) is used to display the value of sum. The output is sum = 9.
Q-2. Determine if the following expressions are true (1) or false (0). Then check
your answers using MATLAB (simply enter the expression).
a = 5.5 b = 1.5 k = -3
1. a < 10;
2. a + b >= 6.5;
3. k ~=0;
4. b - k > a;
5. a==3*b;
6. -k <= k + 6;
7. a < 10 & a > 5;
8. abs(k) > 3 | k < b – a ;
1
RISHABH SINGH
24ME51D
1. a<10;
Result : 1
Explanation : First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (a>10) if it is true, the outcome is 1.
2. a + b >= 6.5;
Result : 1
Explanation : First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (a + b >=6.5) if it is true, the outcome is 1 otherwise zero. In this case a+b = 7
which is greater than 6.5. So the condition is true. Outcome is 1.
3. k ~=0;
Result: 1
Explanation : initially k=-3 and in next command k is initialize 0 with a compliment. If the
condition is true it is taken as false and vice versa.
4. b - k > a;
Result: 0
Explanation: First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (b-k>a) if it is true, the outcome is 1 otherwise zero. In this case b-k=4.5 which
is less than a. So the condition is false. Outcome is 0.
5. a==3*b;
Result: 0
Explanation : First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (a==3*b) if it is true, the outcome is 1 otherwise zero. In this case 3*b=4.5 which
is not equal to a. So the condition is false. Outcome is 0.
6. -k <= k + 6;
Result: 1
Explanation : First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (-k<=k+6) if it is true, the outcome is 1 otherwise zero. In this case k+6 = 3
which is equal to -k. So the condition is true. Outcome is 1.
7. a < 10 & a > 5;
Result: 1
2
RISHABH SINGH
24ME51D
Explanation : First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (a<10 & a>5) if it is true, the outcome is 1 otherwise zero. In this case 5.5<10
and 5.5>5. So the condition is true. Outcome is 1.
8. abs(k) > 3 | k < b – a ;
Result: 0
Explanation: First of all, initialize the value of a = 5.5 , b= 1.5 and k = -3. Then check the
condition (abs(k)>3 | k < b-a) if it is true, the outcome is 1 otherwise zero. In this
case ,abs(k) is positive value of k which is 3 compare it with a condition (abs(k)>3) which
is false and the next condition is (k<b-a) which is also false. So the condition is false.
Outcome is 0.
Q-3. Make m file and see the result k=input('Give me your age');
if k<0 | k>100 disp('you are a liar') end
Result: you are a liar if your is less than zero or greater than zero.
Explanation: create a new script and save it by m file. Execute the following code if one of
the condition is satisfies, (you are a liar ) is display in command window . If both the
condition is not satisfies then there is nothing in a command window.
Q-4. The cost per mile for a rented vehicle is Rs 25 for the first 100 mile,
Rs 20 for the next 200 miles and Rs 10 for all miles in excess of 300
miles. Write MATLAB statements that determine the total cost and the
average cost per mile for a given number of miles (stored in variable
distance).
Syntax :>> distance = input('Enter the distance travel in miles: ');
>>total_cost = 0; if distance <=
100 total_cost = distance
* 25; elseif distance <= 300 total_cost =
100 * 25 +
(distance - 100) * 20; else total_cost = 100 * 25 +
200 * 20 + (distance - 300) *
10; average cost = total
cost/distance; end
disp(‘total_cost’); disp(total_cost)
3
RISHABH SINGH
24ME51D
disp(‘average_cost’);
disp(average_cost);
Result: let us take an example distance =400miles
Total cost = Rs 7500
Average cost = Rs 18.75
Explanation: First of all take a input for distance and initialize the value of total cost
=0.using if else function, check a condition for (distsnce <=100) if it is satisfies then
multiply the distance with 25 if it is not satisfies then it jump to ifelse statement to check
for (distance <=300) if this condition is satisfies then total cost will be (100*25 + (distance
–100)*20) this. If this condition is also not satisfies then it jump to else statement to check
it the condition and whatever the value is it is assign in total cost. disp(total_cost) is display
the value of total cost in command window similiarly for average cost.
Q-5. Write a Matlab script to count the number of occurrences of a number n in a user
entered vector of numbers v. For example, the number of occurrences of 4 in [1, 3, 4, 5, 1,
4] is 2.
Syntax: >>vector = [1, 2, 3, 4, 2, 2, 5, 2, 6];
>>element = 2; count =
0; for i =
1:length(vector) if
vector(i) == element
count = count + 1; end
disp(‘count’);
disp(count);
Result: 4
Explanation: First of all, take a Vector v of random element. Create a variable and assign a
value which we want to count the number of occurence. Again initialize count with 0.
Now using ‘for’ loop iterate from 1 to length of the vector then in next command it is
check for a condition vector(i) == element if it is satisfies the value of count is increment.
This loop is iterate until the satisfies. If condition is not satisfies, the loop is terminated
and in command window the value of count is display.
Q-6. Use a for-end loop in a script file to calculate the sum of the first n terms of the series:
Σk=1(−1)^k / 2^k . Execute the script file for n=4 and n=20.
4
RISHABH SINGH
24ME51D
Syntax: >>n_values = [4, 20];
>>for n = n_values
sum_series = 0;
for k = 1:n term =
(-1)^k / 2^k;
sum_series =
sum_series + term;
end disp(‘the sum
of first 4 term of
the series
=’); disp(sum_series); disp(‘the sum of first
20 term of the series =’); disp(sum_series);
Result: the sum of first 4 term of the series =-0.3125 the
sum of first 20 term of the series =-0.3333
Explanation: create a vector n_values and assign a element 4 and 20 in it. By using ‘for’
loop firstly it will iterate 4 times and then 20 times. There is another loop which will iterate
n times from first loop n value is 4 and in every iteration sum_series is updated. After that
this loop is execute n times and in this case n is updated as 20 and in every iteration
sum_series is upadated .disp( sum_series) display the value for both term (n=4 & n=20).
Q-7. Following were the daily maximum temperature (in F) in India during the month of
April, 2005:
40 41 42 44 42 46 43 45 44 40 42 39 40 42 43 42 43 42 45
46 47 48 43 44 42 41 40 42 41 39 .
Use relational and logical operations to determine the following:
a) The number of days the temperature was above 40
b) the number of days the temperature was between 40 and 45
c) the days of the month that the temperature was between 42 and 45.
a) The number of days the temperature was above 40
Syntax: >> temperature = [40 41 42 44 42 46 43 45 44 40 42 39 40 42 43 42 43 42
5
RISHABH SINGH
24ME51D
45 46 47 48 43 44 42 41 40 42 41 39];
>> day_above_40 = sum(temperature>40);
>> disp(day_above_40);
Result: 24
Explanation: Create a vector temperature, having a n number of elements. In next
command, condition is check if temperature is greater than 40 it will take a sum and store
in day_above_40 . Using disp(day_above_40) display the value in command window.
b) the number of days the temperature was between 40 and 45
Syntax: >>temperature = [40 41 42 44 42 46 43 45 44 40 42 39 40 42 43 42 43 42
45 46 47 48 43 44 42 41 40 42 41 39];
>> days_between_40_and_45 = sum(temperatures >= 40 & temperatures <= 45);
>>disp(days_between_40_and_45);
Result: 24
Explanation: Create a vector temperature, having a n number of elements. In next
command, condition is check if temperature is lie between 40 and 45 . it will take a sum
and store in days_between_40_and_45 . Using disp(days_between_40_and_45) display the
value in command window.
c) the days of the month that the temperature was between 42 and 45.
Syntax: >> Temperature = [40 41 42 44 42 46 43 45 44 40 42 39 40 42 43 42 43 42
45 46 47 48 43 44 42 41 40 42 41 39];
>> days_between_42_and_45 = find(temperatures >= 42 & temperatures <= 45);
>> disp(days_between_42_and_45);
Result: 3 4 5 7 8 9 11 14 15 16 17 18 19 23 24 25 28
Explanation: Create a vector temperature, having a n number of elements. In next
command, condition is check if temperature is lie between 42 and 45 . It will take all the
index value having temperature lie between (42 to 45). Using
disp(days_between_42_and_45) display the value in command window.
6
RISHABH SINGH
24ME51D