6.6: Solutions to Exercises
Solution to Exercise 1
The code
0:50
creates a vector of integers starting at and going to ; this vector has elements. "Hello World" will be printed once for each element in the vector (51 times).
Solution to Exercise 2
The code
1:1:50
creates a vector of integers starting at and going backward to ; this vector has elements. "Guten Tag Welt" will be printed once for each element in the vector (50 times).
Solution to Exercise 3
The code
1:1:50
creates an empty vector with no elements. "Bonjour Monde" would be printed once for each element in the vector, but since the vector is empty, it is never printed.
Solution to Exercise 4
The outer loop (the loop with
a
) will be executed five times. Each time the outer loop is executed, the inner loop (the loop with
b
) will be executed eleven times, since
0:0.1:1
creates a vector with elements. "Hola Mundo" will be printed 55 times.
Solution to Exercise 5
In the first line, the value of
n
is set to . The code
1:n
creates a vector of integers from to . Each iteration through the loop sets
j
to the next element of this vector, so
j
will be sent to each value through in succession, and this sequence of values will be printed. Note that each time through the loop, the value of
n
is decreased by ; the final value of will be . Even though the value of is changed in the loop, the number of iterations through the loop is not affected, because the vector of integers is computed once before the loop is executed and does not depend on subsequent values of
n
.
Solution to Exercise 6
The
d
loop will be executed seven times. In each iteration of the
d
loop, the
h
loop will be executed times. In each iteration of the
h
loop, the
m
loop will be executed times. In each iteration of the
m
loop, the
s
loop will be executed times. So the variable count will be incremented .
A simpler way to achieve the same results is the command


7 * 24 * 60 * 60

Solution to Exercise 8
The following for loop is designed to solve this problem:


t=0:.01:4;

hold on

for f=[0.7 1 1.5 2]

y=cos(2 * pi * f * t);

plot(t,y);


end

When this code is run, it plots all of the cosine waveforms using the same line style and color, as shown in Figure 7.
Plot of cosines at different frequencies.
The following code changes the line style of each of the cosine plots.


fs = ['r';'b.';'go';'y*']; % Create an array of line style strings

x=1; % Initialize the counter variable x

t=0:.01:4;

hold on

for f=[0.7 1 1.5 2]

y=cos(2*pi*f*t);

plot(t,y,fs(x,1:end)); % Plot t vs y with the line style string indexed by x

x=x+1; %Increment x by one


end

xlabel('t');

ylabel('cos(2 pi f t)')

title('plots of cos(t)')

legend('f=0.7','f=1','f=1.5','f=2')

This code produces the plot in Figure 8. Note that this plot follows appropriate engineering graphics conventionsaxes are labeled, there is a title, and there is a legend to identify each plot.
Plot of cosines at different frequencies.
Solution to Exercise 10
This solution was created by Heidi Zipperian:


a = [1 1 2 4 2]

b = [1 2 3 1 2]

for j = 1:5

c=sqrt(a(j)^2 + b(j)^2)


end

A solution that does not use a for loop was also created by Heidi:


a = [1 1 2 4 2]

b = [1 2 3 1 2]

c = sqrt( a. ^2 + b. ^ 2)

Solution to Exercise 11
The following shows the commands typed at the
>>
prompt and the output produced:


>> d = 1.54

d =

1.5400

>> b = 8

b =

8

>> h = 14

h =

14

>> vcyl = h * pi * (b/2) ^ 2

vcyl =

703.7168

>> vcube = d ^ 3

vcube =

::3.6523

>> nl = vcyl / vcube

nl =

::192.6796

Solution to Exercise 12
We created the following file named
PingPong.m
:


% PingPong.m  computes a lower bound on the number of

% ping pong balls that fit into a cylinder

% Note that most lines end with a ";", so they don't print

% intermediate results

d = 1.54;

h = 14;

b = 8;

vcyl = h * pi * (b / 2) ^ 2;

vcube = d ^ 3;

nl = vcyl / vcube

When run from the command line, this program produces the following output:


>> PingPong

nl =

192.6796

Solution to Exercise 13
This solution is by BrieAnne Davis.


for d = 1.0:.05:2.0

b=8;

h=14;

vcyl = h * pi * (d / 2) ^ 2

vcube = d ^ 3

nl = vcyl / vcube


end

Solution to Exercise 14
This solution is by Wade Stevens. Note that it uses the command hold on to plot each point individually in the for loop.


clear all

hold on

for d=1.0:0.1:2.0;

b=8;

h=14;

C = h * pi * (b / 2) ^ 2; %volume of cylinder

c = d ^ 3; %volume of cube

N = C / c; %Lower bound

floor(N)

plot (d, N,'g*')


end

This solution creates the plot in Figure 9.
Plot of as a function of ; each point plotted individually.
This different solution is by Christopher Embrey. It uses the index variable
j
to step through the
dv
array to compute elements of the
nlv
array; the complete
nlv
array is computed, and then plotted outside the for loop.


clear

dv = 1.0:.05:2.0;

[junk, dvsize] = size(dv)

for j = 1:dvsize

d = dv(j)

b = 8; %in

h = 14; %in

vcyl = h * pi * (b/2) ^ 2;

vcube = d ^ 3;

nl = vcyl / vcube;

nlv(j) = nl;


end

plot (dv,nlv)

This solution creates the plot in Figure 10.
Plot of as a function of ; points plotted as vectors.
And Finally, this solution by Travis Venson uses vector computations to perform the computation without a for loop.


%creates a vector for diameter

dv = 1:.02:2;

b = 5.5;

h = 12;



%computes volume of cylinder

vcyl = h * pi * (b/2) ^ 2;



%computes volume of cube

vcube = dv. ^ 3;



%computes lower bound

lowerboundv = vcyl./vcube;



%plots results

plot(dv,lowerboundv)

Solution to Exercise 15
This solution is by AJ Smith. The height,
h
, ranges from to and the base,
b
, ranges from to .


for h=12:15; %ranges of height

for b=8:12; %ranges of the base

d=1.54; %diameter of ping pong ball.

Vcyl=h * pi * (b/2) ^ 2; %Volume of cylinder

Vcube=d ^ 3; %volume of a cube that encloses a single ball

Nl=Vcyl/Vcube %lower bound on the number of balls that fit in the cylinder


end


end

Solution to Exercise 17
This solution is by Scott Jenne; it computes and plots the velocity:


load Altitude.txt ascii

t = Altitude(:,1);

s = Altitude(:,2);

for n = 1:180;

v(n) = ((s(n+1))s(n))/((t(n+1))t(n))

hold on

plot(t(n),v,'o')


end

The plot produced by this code is shown in Figure 11.
Plot of velocity computed with the forward difference method versus time.
Solution to Exercise 18:
This solution by Bryson Hinton:


load altitude.txt ascii

t = altitude(:,1);

s = altitude(:,2);

hold on

for x = 2:181

v(x) = (s(x)s(x1))/(t(x)t(x1));

plot(t(x),v(x),'b.')


end

The plot produced by this code is shown in Figure 12.
Plot of velocity computed with the backward difference method versus time.
Solution to Exercise 19:
This code computes the velocity using the central difference formula.


load Altitude.txt ascii

t=Altitude(:,1);

s = Altitude(:,2);

for n = 2:180

v(n1)=(s(n+1)s(n1))/(t(n+1)t(n1));


end

plot(t(2:180), v)

The plot produced by this code is shown in Figure 13.
Plot of velocity computed with the central difference method versus time.
Solution to Exercise 20:
This code uses the
diff
function to compute the difference between adjacent elements of
t
and
s
,and the
./
function to divide each element of the altitude differences with the corresponding element of the time differences:


load Altitude.txt ascii

t = Altitude(:,1);

s = Altitude(:,2);

v = diff(s)./diff(t);

plot(t(1:180),v)

The plot produced by this code is shown in Figure 14.
Plot of velocity computed with the forward difference method versus time. The values in this plot are the same as in Figure 11.
Solution to Exercise 21:
This solution is by Jonathan Selby:


load Acceleration.txt ascii

t=Acceleration (:,1);

a=Acceleration (:,2);

v(1)=0;

for n = 1:181

v(n+1)=(t(n+1)t(n))*(a(n+1)+a(n))/2+v(n);


end

plot(t,v)

This code creates the plot in Figure 15.
Plot of velocity versus time. The velocity is computed by numerically integrating the measured acceleration.
This code can be easily extended to also compute altitude while it is computing velocity:


load Acceleration.txt ascii

t=Acceleration (:,1);

a=Acceleration (:,2);

v(1) = 0; % Initial velocity

s(1) = 0; % Initial altitude

for n = 1:181

v(n+1)=(t(n+1)t(n)) * (a(n+1)+a(n))/2+v(n);

s(n+1)=(t(n+1)t(n)) * (v(n+1)+v(n))/2+s(n);


end

plot(t,s)

This code creates the plot in Figure 16.
Plot of altitude versus time.
Solution to Exercise 22:
This solution by Nicholas Gruman uses the
cumtrapz
function to compute velocity with the trapezoidal rule:


load Acceleration.txt ascii

t=Acceleration(:,1);

A=Acceleration(:,2);

v = cumtrapz(t,A);

Altitude could also be computed by adding the following line to the end of the previous code:


s = cumtrapz(t,v);
