Help Commands - Name I.E Gives Help On How To Use A Command

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MATLAB WORKSHOP

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];

4. Construct the above vectors without using a loop.

EX 6: A While Loop executes a set of commands repeatedly, until a controlling expression is


no longer true. Try running this simple program from an M-File:
clear ;
b=0;t=0; %Enter the initial values of variables b and t
while 2^b<200 %Enter the controlling expression
b=b+1;
t=t+2^b; %These two lines are the commands to be executed
end %End the While Loop

EX 7: What does the following code do?


sze = 5;
one = [ ]; % This specifies an empty matrix.
for ii = 1 : 2*sze+1
one = [one 1];
end
x = one' * (-sze : sze);

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:

• If time is greater than 50.0, increment time by 1.0.


• If the difference between volt_1 and volt_2 is larger than 2.0, print the values of
volt_1 and volt_2.
• If the value of den is less than 0.003, set result to zero; otherwise, set result equal
to num divided by den (hint: use the else statement )

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

EX 13: Make m file and see the result


k=input('Give me your age ');
if k<0 | k>100
disp('you are a liar')

2
end;

EX-13(a) Write a script called ex1Q1. In the script, do the following

1. Create the matrix dataQ1:

12 7 35
1 22 14
42 3 13
16 26 8
11 5 33

2. Retrieve the following:


a. firstScalar = the element in the 3rd row and 2nd column.
b. firstVector = elements from columns 1 and 3 from the 2nd row.
c. firstMatrix = elements from columns 2 and 3 from rows 3 to 5.

Change dataQ1 as followed:


3. Replace elements from row 2 from columns 1 and 2 with the vector (17 18) .
After replacing, assign dataQ1 to a new matrix called dataQ1S3.
4. Delete the 2nd row from dataQ1. After deleting, assign dataQ1 to dataQ1S4.
5. Replace elements from rows 2 to 4, from columns 1 and 2 with the matrix

10 9
8 7
6 5

Assign dataQ1 to dataQ1S5.


Create dataQ1S6: concatenation of dataQ1 and the following vector:
[ 11 12 13 ] .

Use ‘size’ command to get rowNum = the number of rows of dataQ1.

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 20: Find x, y and z for the following system of equations :


x-y+2z = -2
3x-3y+4z = -5
y+2z = 2

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-22: A vector is given by V=[ 5 17 -3 8 0 -1 12 15 20 -6 6 4 -7 16]. Write a program as a script


file that doubles the elements that are positive and are divisible by 3 or 5 raise to the power of 3 the
elements that are negative but greater than -5.

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.

You might also like