Description - Cornell Computer Science

CS1112 Fall 2016 Project 2
due Thursday 9/15 at 11pm
You must work either on your own or with one partner. If you work with a partner you must first register
as a group in CMS and then submit your work as a group. Adhere to the Code of Academic Integrity. For
a group, “you” below refers to “your group.” You may discuss background issues and general strategies
with others and seek help from the course staff, but the work that you submit must be your own. In
particular, you may discuss general ideas with others but you may not work out the detailed solutions
with others. It is not OK for you to see or hear another student’s code and it is certainly not OK to
copy code from another person or from published/Internet sources. If you feel that you cannot complete
the assignment on you own, seek help from the course staff.
Objectives
Completing this project will solidify your understanding of Chapters 2 and 3 in Insight (for-loops, whileloops, nested loops). You will also further explore Matlab graphics and call user-provided (not built-in)
functions.
Ground Rule
Do not use arrays or the break command in this project. In fact, do not use the break command in this course.
1
What time is it?
Write a Matlab script clockFace to display a time (e.g., 23:15:31) on a clock with hour and minute hands.
(Make a still display, not a running clock.)
The clock is drawn in a figure window. Our clock’s hands have a “continuous pivot,” i.e., they are not
restricted to the 60 individual minute marks. The hour value may be any whole number in [0..23] and the
minute and second values are whole numbers in [0..59]. There are quite a few things that the solution will
need, so practice problem decomposition. Here are some “subproblems” that you need to solve in order to
complete the solution; work on them one at a time: convert numeric time values to angles, convert angles
to x-y coordinates for plotting, draw the clock with tick marks, and draw the hands to indicate the time
correctly. First, structure your solution by writing a comment for each task in the provided skeleton script
clockFace in a logical order. Then fill in the detail (add code) under each comment. Test each newly added
section of code simply by running the (partially completed) program. (Later in the course we will discuss
a better strategy for testing and program development.) Keep in mind that some tasks may appear in
multiple parts of the script (e.g., converting an angle to x-y coordinates for plotting). Below are additional
specifications:
1
• You must call the provided functions DrawDisk, DrawStar, or DrawRect to draw the face of the clock,
including the hour and minute marks. Download the files DrawDisk.m, DrawStar.m, and DrawRect.m
from the course website and put them in your current folder along with clockFace.m. See your notes
from Lecture 6 to see how to call the DrawDisk, DrawStar, and DrawRect functions.
• Use the plot function to draw the hands. For example, plot([x1 x2],[y1 y2],’r-’,’Linewidth’,8)
draws a red line of “width 8” from (x1,y1) to (x2,y2). The hour hand in the figure above has a width
of 8; the default line width is 0.5.
• The hour marks must be different from the minute marks.
• The hour marks for 3, 6, 9, and 12 o’clock must be different from the other hour marks.
• The hour hand must be easily distinguishable from the minute hand.
Choose colors and shapes as you wish. Here’s a hint: hours, minutes, and seconds can be converted readily
to an angle, so think in polar coordinates. A polar coordinate has two components: radial and angle. The
radial component is like the radius of a circle; the angle is measured counter-clockwise from 0◦ , which is
the positive x-axis in the Cartesian system. Remember that Matlab uses radians, not degrees, for angles.
In order to use Matlab’s plot function, you need to convert from polar coordinates (r, θ) to Cartesian
coordinates (x, y):
x = r cos(θ) y = r sin(θ)
Submit your file clockFace.m on CMS.
2
Trajectory of a golf ball
In your physics course you might have studied the motion of a projectile. Now
you will write a simulation to plot the trajectory of a golf ball in flight, subject
to air resistance (drag). Without air resistance, one expects the trajectory to be a
parabola due to Earth’s gravity, with equal time for ascending and descending, as
shown in the diagram on the right. What is the effect of air resistance? You will
find out!
We consider the golf ball to be a unit mass and air resistance to act in the opposite direction of motion. Further we assume that air resistance is proportional to
the square of the velocity of the projectile. With these assumptions, the relevant
equations are
vx
vy
=
=
dvx /dt =
dvy /dt =
dx/dt
dy/dt
q
vx2 + vy2
q
−k · vy vx2 + vy2 − g
−k · vx
where vx and vy are the components of the velocity in the x- and y-directions, k is the coefficient of air drag,
and g is the gravitational constant. The golf ball is initially at x = 0 and y = 0 and is launched with some
initial velocity at an angle φ measured from the x-axis.
Download the file golfBall.m from the Projects page. Read and run it. Since the simulation code is missing
the output includes only a figure window with axis labels and “dummy values” printed to the Command
Window. You will complete the simulation and replace the dummy values with the actual values calculated
in the simulation.
Here are the details of our simulation:
• The constants, parameter values, and initial conditions are given in the provided code. Develop your
simulation with the given values but you can (and will) experiment with different values later.
2
• The simulation begins with time t = 0 and position x = 0 and y = 0 and ends when the golf ball lands
(y gets back to, or passes, zero) or the maximum allowed simulation time has been reached, whichever
happens first.
• Given the initial velocity v and launch angle φ, the initial velocities in the x- and y-directions are
vx = v cos φ and vy = v sin φ
• We use differencing to represent the (continuous) derivatives. For example, instead of
dx
dt
we consider
xnew − xcurrent
4t
where 4t is a discrete time step. With this discrete representation, at each time step, i.e., each step
of the simulation, we can compute the new velocities and positions as follows:
q
vxnew = vx − 4t · k · vx vx2 + vy2
q
vynew = vy − 4t · (k · vy vx2 + vy2 + g)
xnew
=
x + vx · 4t
ynew
=
y + vy · 4t
• Starting at t = 0 and after each time step, plot the location of the golf ball. Recall that the command
plot(a,b,’ro’) draws a marker (red circle) at position (a,b). Choose any marker and color you like.
Add a pause of 0.01 seconds (pause(.01)) after the plot command so that the plotting is animated
when the simulation is executed.
Run the code you have so far to make sure that the simulation works! At this point you should have
removed all the dummy values except perhaps those for variables ascendTime and descendTime. Does
the trajectory make sense (a curve that opens down, starting and ending at around y = 0)? Do the
displayed values in the Command Window look correct?
• Add more code to the simulation to compute the durations of ascent and descent. Do this using code
based on the simulation here—do not look up equations from physics textbooks! Make sure all the
dummy variable values are removed.
• Finally, modify the parameter values phi, v, k, and maxTime to see how the trajectory changes. Once
you change the parameter values, the complete trajectory may not be shown since the axes are set to
120m wide and 100m tall and maxTime may be “too short,” but you can use the values displayed in
the Command Window to deduce the trajectory. Add a comment at the end of the script to answer
these questions:
1. Which of these launch angle results in the longest horizontal range, π/3, π/4, π/5, π/6 (without
changing the other parameter values)?
2. How does the flight time change with the launch angle? Answer in one sentence.
3. What is the shape of the trajectory when k = 0?
Answer the above questions simply by running the simulation with different parameter values. Do not
write more code.
Before submitting your file golfBall.m on CMS, change all the parameter values back to the original values
given: k=0.02, maxTime=10, phi=pi/4, and v=100.
3
3
Pythagoras rules (!)
A Pythagorean triple is a set of three positive numbers, a, b, and c such that
a 2 + b 2 = c2
Write a script triples to display and count all the integer Pythagorean triples with a ≤ M , b ≤ M , and
c2 = a2 + b2 , where M is a user-input value in the range of 4 to 1000, inclusive. Your script should keep
prompting the user for M until a value in the correct range is entered; it does not matter whether M is an
integer or has fractional parts.
Note: Check all possible combinations of a and b: we consider, e.g., a = 3, b = 4, c = 5 and a = 4, b = 3,
c = 5 to be two different triples. You may find the functions sqrt and floor useful. Below is an example
run of the program where the user eventually set M to 15.4. Your output should be in a similar format. To
get the numbers to line up nicely in columns as shown, use fprintf with appropriate substitution sequences
(format strings).
Enter a number in the range of 4 to 1000: -6.1
Enter a number in the range of 4 to 1000: 10002
Enter a number in the range of 4 to 1000: 15.4
a
b
c
--------------------3
4
5
4
3
5
5
12
13
6
8
10
8
6
10
8
15
17
9
12
15
12
5
13
12
9
15
15
8
17
There are 10 integer Pythagorean triples
with a and b less than or equal to 15.40.
Submit your file triples.m on CMS.
4