Help Commands - Name I.E Gives Help On How To Use A Command
Help Commands - Name I.E Gives Help On How To Use A Command
Help Commands - Name I.E Gives Help On How To Use A Command
Exercises-2
(A) Useful Commands
>>help commands_name i.e gives help on how to use a command
>> lookfor for while
>> help while
>>help if
>>whos
EX 1: Create a new m file and save as m file and execute and see the value of y .
a=10;
while a>0
y(a)=a*10;
a=a-1;
end;
y
Ex 2: Examine the following for loops and determine how many times each loop will be
executed:
i) for index = 7:10
ii) for jj = 7:-1:10
iii) for ii = -10:3:-7
iv) for kk = -10:3:10
v) for ii = 0:0.25:5
vi) for jj = 5:-0.5:-5
3. Write a loop to construct the following vectors:
(a) v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
(b) v = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1];
(c) v = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
(d) v = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
(e) v = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
(f) v = [ 1/2 , 1, 1, 1, 1, 1, 1, 1, 1, 1/ 2 ];
(g) v = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
(h) v = [1, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 1];
ramlal@cc.iitd.ac.in 1
y = (-sze : sze)' * one;
r = (x.^2 + y.^2).^0.5;
p=r<4
EX 8 Type the following lines in matlab editor and save it as test.m and execute it.
m = [1 2 3; 4 5 6]
size(m) % Returns the size of a matrix
size(m, 1) % Number of rows
size(m, 2) % Number of columns
m1 = zeros(size(m)) % Create a new matrix with the size of m
who % List variables in workspace
whos % List variables w/ info about size, type, etc.
EX 9: Type the following lines in matlab editor and save it as test.m and execute it.
a=input('enter a ');
sum = 0.0;
if a < 50
sum = sum + a;
end
disp('sum ');
disp(sum);
EX 10 : Give MATLAB statements that perform the following:
EX 11 This 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;
end
disp('sum');
disp(sum);
EX 12: 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
2
end;
12 7 35
1 22 14
42 3 13
16 26 8
11 5 33
10 9
8 7
6 5
EX 14: Create an m X n matrix A, create a matrix B of the same size containing all zeros,
and then copy into B the elements of A that are greater than zero.
EX 15 Write a Matlab script file to find the left-most (first) digit of a number n. For
example, the left-most digit of 5428 is
Ex 16: 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).
Ex 17: Write a MATLAB program to evaluate a function f ( x, y ) for any two user specified
values of x and y. The function f ( x, y ) is defined as follows:
3
⎧ x + y, x ≥ 0 and y ≥ 0
⎪ x + y 2 , x > 0 and y < 0
⎪
f ( x, y ) = ⎨ 2
⎪ x + y, x < 0 and y > 0
⎪⎩ x 2 + y 2 , x < 0 and y < 0
Ex 18:Write a Matlab script file to count the number of occurrences of a number n in a
vector of numbers v. For example, the number of occurrences of 4 in [1, 3, 4, 5, 1, 4] is 2.
EX 19:Write a Matlab script file to calculate the factorial of a number n. Make sure you
handle the case of 0! Report an error if n is negative.
EX 21. 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
EX 23: 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
EX 24: An election is contested by the five candidates. The candidates are numbered 1 to 5 and the
voting is done by marking the candidate number on the ballot paper. Write a script to read the
ballots and count the votes cast for each candidate. In case, a number read is outside the range 1 to 5,
the ballot should be considered as a ‘spoilt ballot’, and the program should also count the number of
spoilt ballots.