summer matlab exam solution

7
CS 111 Matlab Exam 28 June 2006 17:00-19:00 Name and Surname: Solution______________ Signature: _______ Question 1. (60 points) Let m mass of the body v velocity of the body r radius of the circle the body makes cptf centripetal force of the body p period av angular velocity Then the following formula can be used to calculate the centripetal force of a body that makes a circular motion, period of the motion, and angular velocity of the body: Write a Matlab function that calculates the centripetal force of a body that makes a circular motion, period of the motion, and angular velocity of the body. Note that the input arguments can be arrays of any size. Your function must check the sizes of the arrays, and give an error message if they are incompatible. Note that any of the input arguments can be scalar. Your function must also give an error message if any of the input argument arrays is an empty matrix. Note that all input values must be positive. If not, your function should give an error message and hence stop. Write a Matlab program that reads in a set of values for m,v, and r, uses the above function to compute cptf, p, and av, and displays the results with proper messages. Your program should also sketch a graph of v versus cptf with proper titles, labels, etc.. See the following executions of mmq1m that solves the problem and the figure as the result of the graph formed. >> mmq1m please type a set of values for the mass:3 please type a set of values for the velocity:7 please type a set of values for the radius:[0.5;1;1.5] m v r cptf p av 3.00 3.00 3.00 7.00 7.00 7.00 0.50 1.00 1.50 294.00 147.00 98.00 Page 1

Upload: grifinanuzulia

Post on 28-Nov-2014

107 views

Category:

Documents


0 download

TRANSCRIPT

CS 111 Matlab Exam28 June 2006 17:00-19:00

Name and Surname: Solution_______________ Signature: _________

Question 1. (60 points) Let

m — mass of the body

v — velocity of the body

r — radius of the circle the body makes

cptf — centripetal force of the body

p — period

av — angular velocity

Then the following formula can be used to calculate the centripetal force of a body that makes a circular motion, period of the motion, and angular velocity of the body:

Write a Matlab function that calculates the centripetal force of a body that makes a circular motion, period of the motion, and angular velocity of the body.

Note that the input arguments can be arrays of any size. Your function must check the sizes of the arrays, and give an error message if they are incompatible. Note that any of the input arguments can be scalar.

Your function must also give an error message if any of the input argument arrays is an empty matrix.

Note that all input values must be positive. If not, your function should give an error message and hence stop.

Write a Matlab program that reads in a set of values for m,v, and r, uses the above function to compute cptf, p, and av, and displays the results with proper messages. Your program should also sketch a graph of v versus cptf with proper titles, labels, etc.. See the following executions of mmq1m that solves the problem and the figure as the result of the graph formed.>> mmq1m

please type a set of values for the mass:3

please type a set of values for the velocity:7

please type a set of values for the radius:[0.5;1;1.5]

m v r cptf p av

3.00 3.00 3.00 7.00 7.00 7.00

0.50 1.00 1.50 294.00 147.00 98.00

0.45 0.90 1.35 14.00 7.00 4.67

>> mmq1m

please type a set of values for the mass:3

please type a set of values for the velocity:7

please type a set of values for the radius:[0.5;1;1.5]

m v r cptf p av

3.00 7.00 0.50 294.00 0.45 14.00

3.00 7.00 1.00 147.00 0.90 7.00

3.00 7.00 1.50 98.00 1.35 4.67

Page 1

>> mmq1m

please type a set of values for the mass:[ 2 4]

please type a set of values for the velocity:[ 1 3]

please type a set of values for the radius:[2;7]

??? Error using ==> mmq1f

The size of the input argument arrays are incompatible

Error in ==> C:\CS 111\2006SpringLQH\MM\mmq1m.m

On line 5 ==> [cptf, p, av] = mmq1f (mass, velocity, radius);

>> mmq1m

please type a set of values for the mass:[-3 4]

please type a set of values for the velocity:[2 1]

please type a set of values for the radius:[7 8]

??? Error using ==> mmq1f

m contains at least one non-positive value

Error in ==> C:\CS 111\2006SpringLQH\MM\mmq1m.m

On line 5 ==> [cptf, p, av] = mmq1f (mass, velocity, radius);

>> mmq1m

please type a set of values for the mass:[1,2,3;4,5,6]

please type a set of values for the velocity:[1,1,1;2,2,2]

please type a set of values for the radius:[0.2,0.4,0.6;0.5,1,1.1]

m v r cptf p av

1.00 1.00 0.20 5.00 1.26 5.00

4.00 2.00 0.50 32.00 1.57 4.00

2.00 1.00 0.40 5.00 2.51 2.50

5.00 2.00 1.00 20.00 3.14 2.00

3.00 1.00 0.60 5.00 3.77 1.67

6.00 2.00 1.10 21.82 3.46 1.82

Page 2

% mmq1f.mfunction [cptf, p, av] = mmq1f (m, v, r)

% MMQ1F (m,v,r) calculates [cptf, p, av] of a body that

% makes a circular motion where

% m - mass of the body; v - velocity of the body

% r - radius of the circle the body makes

% cptf - centripetal force of the body

% p – period; av - angular velocity

msize = size(m);

vsize = size(v);

rsize = size(r);

if (isempty(m) | isempty(v) | isempty(r))

error('missing information, quitting!');

elseif ( ( sum(msize==vsize) == 2 && sum(msize==rsize) == 2 ) || ...

( sum(msize==vsize) == 2 && sum(rsize==[1,1]) == 2 ) || ...

( sum(msize==rsize) == 2 && sum(vsize==[1,1]) == 2 ) || ...

( sum(rsize==vsize) == 2 && sum(msize==[1,1]) == 2 ) || ...

( sum(rsize==[1,1]) == 2 && sum(msize==[1,1]) == 2 ) || ...

( sum(rsize==[1,1]) == 2 && sum(vsize==[1,1]) == 2 ) || ...

( sum(vsize==[1,1]) == 2 && sum(msize==[1,1]) == 2 ) )

% correct size

% check for positiveness

mmin=min(m(:));

if mmin<=0

error('m contains at least one non-positive value');

else

vmin=min(v(:));

if vmin<=0

error('v contains at least one non-positive value');

else

rmin=min(r(:));

if rmin<=0

error('r contains at least one non-positive value');

else

%calculate the output arguments

cptf = m .* v .^2 ./ r;

p = 2 * pi * r ./ v;

av = v ./ r;

end

end

end

else % incorrect array sizes

error('The size of the input argument arrays are incompatible');

end

Page 3

%mmq1m.m% A program that tests the above mmq1f functionmass = input ('please type a set of values for the mass:');

velocity = input ('please type a set of values for the velocity:');

radius = input ('please type a set of values for the radius:');

[cptf, p, av] = mmq1f (mass, velocity, radius);

sp = size(p,1) * size(p,2);

if size(mass,1) == 1 && size(mass,2) ==1

mass = mass * ones(sp,1);

end

if size(velocity,1) == 1 && size(velocity,2) ==1

velocity = velocity * ones(sp,1);

end

if size(radius,1) == 1 && size(radius,2) ==1

radius = radius * ones(sp,1);

end

disp(' m v r cptf p av');

fprintf('%5.2f\t%5.2f\t%5.2f\t%6.2f\t%5.2f\t%5.2f\n',...

[mass(:), velocity(:), radius(:), cptf(:), p(:), av(:)]');

mass = 4;

velocity=0.01:0.01:3;

radius=1;

cptf = mmq1f (mass, velocity, radius);

plot(velocity,cptf);

axis ([0 3 0 36]);

title('\bfVelocity versus centripetal force');

grid on

xlabel('velocity');

ylabel('cptf');

Page 4

Question 2. (40 points)Let

What are the values of the arrays after execution of each assignment statement?B = a < 0C (~B) = 5D = a ( [4 3], 2:-1:1)E = [ eye(3,4) ; a(:,3)']

B =

0 1 0 0

0 0 0 1

0 0 0 0

0 1 1 0

C =

Columns 1 through 9

5 5 5 5 0 5 5 0 5

Columns 10 through 16

5 5 0 5 0 5 5

D =

-1 0

1 2

E =

1 0 0 0

0 1 0 0

0 0 1 0

3 1 4 -3

Page 5