You are on page 1of 5

Spring 2014 Prelim 1 Solutions

Question 1: (10 points)


(a) What is the output from executing the following script? If the program doesnt terminate or if
there will be an error during execution, write the word error instead of the output.
n = 1;
while n<=10
n = 3*n;
disp(n)
if n==27
n = n-17;
disp(n)
end
end
Solution:
3
9
27
10
30

(b) What will be printed when the following script is executed? Use the specified print format.
Script
a = 3; b = 4;
c = foo(b,a);
fprintf(a is %d\n, a);
fprintf(b is %d\n, b);
fprintf(c is %d\n, c);

Function
function c = foo(d,a)
b = a*2;
c = b+d;
a = d;
fprintf(b is %d\n, b);

Solution:
b
a
b
c

is
is
is
is

6
3
4
10

Spring 2014 Prelim 1 Solutions

Question 2: (15 points)


(a) Consider the code fragment below. Assuming that u stores a positive value less than 1000, rewrite
the fragment using a while-loop such that the same output is produced. If the fragment doesnt
terminate or if there is not enough information for you to translate the for-loop into a while-loop,
then write the words no while in the box below.
% u is positive and <1000
for j = 1000:-u:u
disp(j)
end
% Example solution:
j = 1000;
while j >= u
disp(j)
j = j - u;
end

(b) Let x and n be variables that each stores a positive integer value. Without using the ^ operator
or the equivalent built-in function power, write a fragment below to build a vector z such that z(1)
has the value x, z(2) has the value x2 , z(3) has the value x3 , and so forth. Vector z has length n.
% Assume that scalar variables x and n are initialized to positive integer values.
% Write code to build a vector z as described above.

% Example solution 1
z= ones(1,n);

% preallocation not necessary

z(1)= x;
for k= 2:n
z(k)= z(k-1)*x;
end

% Example solution 2
xpow= 1;
for k= 1:n
xpow= xpow*x;
z(k)= xpow;
end

Question 3: (25 points)


A certain script, when executed, produces the figure shown on the right.
Write the code fragment that you
would add to such a script in order
to estimate the area of overlap between the yellow disk and the blue
rectangle using Monte Carlo approximation. The fragment should generate N random dart throws such that
the x-coordinate is equally likely to be
anywhere from -3 to 3 while the ycoordinate is equally likely to be anywhere from -2 to 3 for each dart. Plot
each dart location using an asterisk in
one of four colors:

Spring 2014 Prelim 1 Solutions

2.5

1.5

0.5

(0,0)
0.5

1.5

2
3

green if the dart is outside both the disk and the blue rectangle
red if the dart is in the part of the disk that does not overlap with the blue rectangle
white if the dart is in the part of the blue rectangle that does not overlap with the disk
black if the dart is in the area where the disk and the blue rectangle overlap

The disk has radius 2 and is centered about (0,0) as marked. You can assume that a dart is never
exactly at the edges of the disk and rectangle.
Reminders: The distance between two points (x1 , y1 ) and (x2 , y2 ) is
command to plot a red asterisk at (3,2) is plot(3,2,r*).
N= 10000;

% Simulate N dart throws.

hit= 0;
for k= 1:N
% Generate pt
x= rand*6-3;
y= rand*5-2;
d= sqrt(x^2 + y^2);

(x1 x2 )2 + (y1 y2 )2 . The

Write your code below.

% #darts in overlap so far

% dist from pt to center of disk

if d<2
if x<1
plot(x,y,r*)
else
plot(x,y,k*)
hit= hit + 1;
end
elseif x>1
plot(x,y,w*)
else
plot(x,y,g*)
end

% in disk
% not in overlap

% in overlap

% not in disk, in blue rect


% not in disk, not in blue rect

end
fprintf(Area of overlap is %f \n, ____ hit/N*30 ____ )
4

Spring 2014 Prelim 1 Solutions

Question 4: (25 points)


(a) Implement the following function as specified:
function [xnew, ynew] = update(x, y, d)
% Update the position after walking one step. x, y, d, xnew, and ynew are
% scalars. (x,y) is the original position; (xnew,ynew) is the position
% after one step. d represents the direction of the step: d=1 means go
% north one unit; any other value of d means go east one unit.
% Example Solution:
if d==1
xnew=
ynew=
else
xnew=
ynew=
end

x;
y + 1;
x + 1;
y;

(b) Implement the following function as specified. For full credit, make effective use of function update
(assume it has been implemented correctly and is accessible).
function [xvec, yvec] = myRandomWalk(x0,y0)
% Simulate a random walk in which only two moves are possible: go north one
% unit or go east one unit. The walk starts at (x0,y0) and in each step, it is
% twice as likely to go east as to go north. The simulation stops after 20
% steps or if the origin (0,0) is reached, whichever happens first.
% Vectors xvec and yvec store the path such that xvec(k) and yvec(k) are the x% and y-coordinates AFTER the kth step. The starting point is NOT stored
% in xvec and yvec, and you can assume that the starting point is not (0,0).
% Example Solution
k= 0; % number of steps so far
x= x0;
y= y0;
while

k<20

&&

(x~=0 || y~=0)

% generate random direction


dir= ceil(rand*3);
% update position
[x, y] = update(x, y, dir);
k= k + 1;
xvec(k)= x;
yvec(k)= y;
end

Spring 2014 Prelim 1 Solutions

Question 5: (25 points)


Complete the function below as specified. Do not use
any built-in functions other than length and zeros.
The diagram on the right shows an example graphic
produced by the following statements:
blu=[0 .1 .9]; yel=[1 .8 .1];
diskTri(6, .5, blu, yel)
Assume the availability of the function DrawDisk. For
example, the command
DrawDisk(3, 2, .5, [1 1 0])
draws a yellow disk of radius 0.5 centered at (3,2).
Your code draws only the disks. The grid lines and
the rgb values are shown for your convenience; do not
draw them.

14

[0.0 0.10 0.90]


12

[0.2 0.24 0.74]

10

[0.4 0.38 0.58]


6

[0.6 0.52 0.42]


4

[0.8 0.66 0.26]


2

[1.0 0.80 0.10]

0
0

10

12

function diskTri(n, s, cTop, cBot)


% Draw a triangle of disks; there are n disks on each side of the triangle
% where n is an integer greater than 1.
% The disks have unit radius and are spaced s units apart, s>0.
% cTop and cBot are vectors of rgb values. The disk at the top has the
% color cTop; the disks in the bottom row have the color cBot; the rows of
% disks in between vary uniformly in color (linearly interpolated).
% The center of the lower left disk is at (0,0) and the disks on the far
% right line up vertically.
close all; figure; axis equal; hold on
Example solution:
d= 2+s;

% distance from center to center

for r= n:-1:1

% Go from bottom row (row n) to top row (row 1)

% y-coord of rth row


y= (n-r)*d;
% Color for rth row
f= (r-1)/(n-1);
colr= f*cBot + (1-f)*cTop;
% At rth row, draw r disks starting from the right
for c= 1:r
x= (n-c)*d;
DrawDisk(x,y,1,colr)
end
end

hold off
6

14

16

You might also like