Our simulations, which are only preliminary investigations, will not assume any air resistance - just the influence of gravity. However, we will take into account that we cannot perfectly control the angle and initial velocity of our pumpkin launch - there will always be error. In particular, we will assume that the errors for both angle and initial velocity follow a normal distribution, which for you will simply mean that the error follows the "bell curve" - most likely to be close to zero, with probability tapering off as you get further away from zero. Don't Panic! I'll take care of the code that generates this kind of random error! At any rate, random errors in angle and velocity will result in over or undershooting are target, and this error in accuracy is what are simulations are measuring. How can we minimize it?
The final version of this program will read a target distance and an angle from the user and 1) compute the velocity required to reach the target distance, 2) simulate 10,000 pumpkin launches with random error in angle and velocity, 3) produce a file that, when plotted with Excel, gives a histogram of the errors in landing position for the 10,000 launches, and 4) print out the "90% error radius", i.e. distance r such that 90% of the launches fall within r feet of the target.
| ||||
Example Run of Project |
This is a Programming Project, and it is very important that you understand and abide by the Department's policy concerning programming projects. Please view:
http://www.cs.usna.edu/academics/ProgrammingPolicy.pdfHere we will highlight a few of the particularly relevant points:
4. a. For assignments designated as Programming Projects in computer science or information technology courses, midshipmen may give no assistance whatsoever to any person and may receive no assistance whatsoever from any person other than the assigning instructor.
4. c. For Programming Projects [...], midshipmen may not use information in any format, electronic or printed, other than their own [...] class notes, the course textbook, or materials distributed by the instructor. If midshipmen wish to use additional sources, they must receive explicit permission from their instructor.
4. d. Working with an instructor on a programming project [...] does not grant permission for midshipmen to collaborate with others [...].
4. e. Midshipmen are responsible for resolving all questions, uncertainties, or perceived ambiguities concerning collaboration by direct consultation with their course instructor.Remember, your personal honor and integrity are far more important than your grade on a project.
NOTE: Since several of the SI204 instructors may be on travel or unavailable from time to time, you are allowed to go to other SI204 instructors for assistance. The SI204 instructors are: Prof. Schulze, Prof. Needham, Prof. Brown, Prof. Stahl, and CAPT Young. Of course, any such assistance needs to be documented on your cover sheet.
where g is the accelearation of gravity, roughly 32 ft/sec2. Now, this also requires you to turn the problem around and compute the velocity required to achieve a certain distance.
d = v cos
θ( v sin
θ +sqrt
(v2sin
2θ + 2 g h)
g)
That should be all the physics you need. Of course you could all have derived that for yourselves, right?
v = d sqrt
( g
2 dsin
θcos
θ + 2 hcos
2θ)
RAND_MAX
, each value occuring with the
same probability. Thus, you're as likely to get 1000 as
you are to get 0. Many physical phenomena aren't like
this - including things like error in aiming. You're more
likely to be off by a little bit than by a lot. Normally
distributed random numbers behave like this. When you
generate normally distributed random numbers you provide a
desired mean and standard deviation.
The numbers generated will be centered at mean,
and will be more likely to occur near mean than
far away - how much more likely is controlled by
standard deviation. Approximately 68% of the
numbers will fall within +/- standard deviation.
Approximately 98% will fall within +/-
twice the standard deviation.
The errors in angle of pumpkin trajectory and initial
velocity will be modelled using normal distributions, so
that small errors are more likley than large errors.
The assignment will tell you what mean and standard
deviation to user, and you will use the function
randNorm(mean,sdv)
provided in
drbrown.h to get your random
numbers. Remember, however, that it is still up to you
to seed the random number generator with a call to
srand(time(0))
at the beginning of
main
. To use this file, place
drbrown.h in your project
directory and add #include "drbrown.h"
to your list of include's. Notice, you use ""s instead
of < >s.
Note: You may not copy&paste
the contents of drbrown.h into your code, you may only
save drbrown.h in your project directory and #include it.
You might want to look at the hitogram explorer, which shows histogram for a variety of random number distributions, including the normal distribution.
The purpose of this project is primarily to give you an
opportunity to use functions and arrays in a large program.
To that end, part of what you will be graded on is that you
use functions and arrays in an appropriate way. If your
entire program is stuck in main
rather than
being broken out into several logical functions, your grade
will suffer. Good programs are broken up into functions,
each of which encapsulate some logical piece of
functionality. Thus, a single function that both computes
the number of pennies required to fill my office and prints
out my optimum bodyweight would, for example, not be one
that encapsulates some logical piece of
functionality. Clearly there should be two functions:
numPennies
and optWeight
.
Run of Part 1 Output File Enter target distance in feet and outfile name: 2300 data.txt
view data.txt
Example Run of Part 1
35 + randNorm(0,1.25)which would sometimes give you an angle less than 35, sometimes greater but, since the distribution is normal, usually pretty close. Just how "close" is controled by the standard deviation.
Enter distance (in ft) and angle (in deg): 2300 42 Required velocity v = 270.735 ft/sec. Enter standard deviations for angle (in deg) and velocity (in ft/sec): 1.25 1.0 Off by -3.10558 ft. angle was 41.1009, velocity was 271.023 Off by 21.3003 ft. angle was 44.0515, velocity was 271.415 Off by -45.1087 ft. angle was 39.0506, velocity was 270.08 Off by 11.5817 ft. angle was 41.5306, velocity was 271.653 Off by 11.098 ft. angle was 43.7264, velocity was 270.859 Off by 11.2973 ft. angle was 41.8454, velocity was 271.478 Off by 9.70372 ft. angle was 41.0412, velocity was 271.822 Off by 34.3673 ft. angle was 42.6043, velocity was 272.525 Off by 10.6812 ft. angle was 42.3391, velocity was 271.225 Off by -24.6966 ft. angle was 41.7777, velocity was 269.367Example Run of Part 2
Note: Because this is random, your results won't match mine! However, if the standard deviations of the errors are made very small, your shots shouldn't be off by much. That's one kind of test. Play around with this for different distances, angles, and standard deviations. Try shooting at 45 degrees and setting the stadard deviation for the velocity error to zero. Is there a wierd pattern in the result? Can you explain it?
Clearly the first is least accurate, the second is more accurate, and the third is most accurate. The concept here ought to be clear: You set up "bins"
-100' to -99', -99' to -98 ft, ... , -1' to 0', 0' to 1', 1' to 2', ... , 99' to 100'and each time you simulate a launch you record which "bin" the error fell into (i.e. you incremement some counter for that bin. When you're done, you print out the count for each bin to some file, import that file into Excel and plot the data as a bar graph. If you look carefully, you'll see that this requires 200 bins. Most sensibly one would create an array of 200 integers to represent the counts for the 200 bins. The only difficulty is taking an error like "-16.37 feet" and determining the index associated with bin [-17' to -16']. Let's do an example with range -3' to +3' rather than -100' to +100'. So, R = 3 and your histogram should cover a range of +/-R feet with 1-foot buckets. The array we'd use to provide our counters for these 2*R bins would look like this:
Now, suppose your simulation resulted in a shot with error -1.7 feet. Which array cell should we increment? Well, -1.7 is in bin [-2' to -1'], of course, and H[1] is the counter for that bin. We can, in fact map an error x to an index in H as follows: index =
array H =
-3' to -2'
-2' to -1'
-1' to 0'
0' to 1'
1' to 2'
2' to 3'
0 0 0 0 0 0
0
1
2
3
4
5
floor(
x + R)
, where
"floor(
z)
" means the greatest integer
less than or equal to z. Notice that for
negative numbers "floor" and "truncation" are different!
BTW: cmath has a "floor" function.
Write a program that reads in a target distance (in ft), a launch angle (in deg), an output filename, a standard deviation for the angle error (in deg), and a standard deviation for the velocity (in ft/sec) and runs 10,000 simulations of the launch in which the angle and velocity are subject to normally distributed random errors with mean 0 and the standard deviations as given by the user. The program should then write an output file that can be imported into Excel to plot a histogram of errors in accuracy for the 10,000 simulations. Line 1 of the file is the number of pumpkin launches that fell in the range -100' to -99', line 2 of the file is the number of pumpkin launches that fell in the range -99' to -98', and so on until the 200th line of the file, which should be the number of pumpkin launches that fell in the range 99' to 100'. If you open this file in Excel, you'll have a 1-column spreadsheet. Select the whole column and plot as a column-bar chart, and you'll get your histogram. Note: some pumpkins may fall outside of the +/-100' range and simply won'tbe recorded in your histogram.
Example run for Part 3 Enter distance (in ft), angle (in deg) and outfile name: 2300 60 h60.txt Required velocity v = 290.794 ft/sec. Enter standard deviations for angle (in deg) and velocity (in ft/sec): 1.0 3.0view screen short of Excel histogram produced from file h60.txt
There will be a paper and electronic part to your submissions. The paper submission can be handed to your instructor, slid under their office door, or put in their mailbox. Each individual instructor will tell their class how the electronic portion is to be submitted. Here's what to submit: