Programming Project 2, SI204, Fall 2004: Punkin Chunkin & Accuracy


Executive Summary
Every year, Sussex County DE hosts PUNKIN CHUNKIN, in which contestants use machines to hurl pumpkins as far as possible; longest throw wins. In this project, we'll consider a slightly different problem, namely hurling pumpkins with accuracy. After all, what's the point of lanching a pumpkin 5000 feet if you can't hit what you're aiming at? Therefore, this project will investigate - via computer simulation - the ballistics of pumpkins.

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.

Run of Program Histogram from file a47.txt
Enter distance (in ft), angle (in deg) and outfile name: 2000 47 tmp.txt
Required velocity v = 252.118 ft/sec.
Enter standard deviations for angle (in deg) and velocity (in ft/sec): 1.15 0.5
90% of shots fall within 17 feet of target!
Example Run of Project

Due Dates and Honor
This project will be due by close of business on Monday, 1 November. Submission instructions will be provided by your individual instructors. Please see the course policy for details about due dates and late penalties.

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.pdf
Here 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.

Extra Information
Details

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.

Step 1: How to hit the target
Write a program that reads from the user a target distance (in feet) and for launch angles 9 deg, 12 deg, 15 deg, ... , 81 deg computes the initial velocity (in feet per second) that would be required to hit the target assuming that the launch occurs 20 ft. above ground level and assuming the ground is completely flat (which, in Sussex County, DE is true!). As output the program should write to both a file and the screen each angle followed by a tab followed by the velocity, one line per angle-velocity pair. This file can then be opened in Excel and ploted to give a nice graph. Hopefully you can pick out the angle that requires the least initial velocity! Note: remember that cmath's sin and cos functions take their angles in radians, not degrees! Be sure to break things into nice logical functions!

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

Step 2: The best laid plans ...
Write a program that reads in a target distance (in ft), a launch angle (in deg), a standard deviation for the angle error (in deg), and a standard deviation for the velocity (in ft/sec) and runs 10 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. For each simulation, the program should output how far off the shot was. To make clear what this error stuff means: Suppose angle θ = 35 deg. If we subject this to a normally distributed error with standard deviation 1.25 degrees, the actual launch angle for a particular simulation would be
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.367			      
Example 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?

Step 3: Getting a handle on our inaccuracy - plotting a histogram
From your part 2 solution you can get some feeling for how random errors in angle and velocity affect your accuracy. However, it's hard to see the whole picture for two reasons: 1) for a real statistical picture to emerge you need many more trials (10,000 instead of 10, perhaps), and 2) we have difficulty spotting patterns in large collections of numbers. A visual presentation of what's landing where is what we really need. What we really need is a histogram of the error in accuracy! The following three histgrams of errors in accuracy mean very different things!



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:

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
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 = 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.0
view screen short of Excel histogram produced from file h60.txt

Step 4: Adding the 90% error radius
Finally, for this last part, you should add on to your part 3 solution so that the program also reports the "90% error radius" statistic, i.e. the value r such that 90% of your 10,000 pumpkin launch simulations fall within +/-r feet of the target distance. There are various ways to do this. The cleverest way involves nothing more than the same array H you used for your histogram! Note: If there is enough error, 90% may not fall within the +/-100' range. You can choose to either not worry about it, or report the radius as simply greater than 100'.

What to submit and how it will be graded
You will only submit a solution to one of the four Steps above. You will submit your solution for the highest numbered step that actually works! For example, if you had a working solution to Step 3, but only a partially completed solution to Step 4, you would submit your Step 3 solution. The maximum points for a Step 1 solution is 30, for a Step 2 solution 60, for a Step 3 solution 90, and for a Step 4 solution 100. How points are assigned for a particular solution is at your instructor's discretion, however, program structure, documentation, variable names, etc. will be considered along with program correctness. Important points:

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:

Step 1 submission:
Paper: A printout of your .cpp file. A screen capture of your program running on input distance 3500 feet and output file name "data1.txt". A printout of the file "data1.txt" produced by your program. You will also submit a signed copy of this cover sheet. Electronic: The .cpp file containing the source code for your solution.

Step 2 submission:
Paper: A printout of your .cpp file. A screen capture of your program running on input distance 3500 feet, angle 45 degrees, angle error standard deviation 1.75 and velocity error standard deviation 0.0. You will also submit a signed copy of this cover sheet. Electronic: The .cpp file containing the source code for your solution.

Step 3 submission:
Paper:
  1. A printout of your .cpp file.
  2. A screen capture of your program running on input distance 2500 feet, angle 55 degrees, output file name "data3.txt", angle error standard deviation 0.75 and velocity error standard deviation 1.0.
  3. A printout of the Excel chart produced from the file "data3.txt" your program created.
  4. A writeup answering the following question: Given target distance 2500 feet, if your current pumpkin throwing machine has angle error standard deviation 0.75 and velocity error standard deviation 1.0, what angle gives you the most accuracy? Your writeup must include experimental data using your program that justify your conclusion. This should be like a Chem Lab writeup!
  5. You will also submit a signed copy of this cover sheet.
Electronic: The .cpp file containing the source code for your solution.

Step 4 submission:
Paper:
  1. A printout of your .cpp file.
  2. A screen capture of your program running on input distance 2500 feet, angle 55 degrees, output file name "data3.txt", angle error standard deviation 0.75 and velocity error standard deviation 1.0.
  3. A printout of the Excel chart produced from the file "data3.txt" your program created.
  4. A writeup answering the following two questions: Given target distance 2500 feet, if your current pumpkin throwing machine has angle error standard deviation 0.75 and velocity error standard deviation 1.0:
    • What angle gives you the most accuracy?
    • Assuming you use your optimum angle, which would improve your accuracy more, reducing angle error standard deviation by 50% or reducing velocity error standard deviation by 50%?
    Your writeup must include experimental data using your program that justify your conclusion. This should be like a Chem Lab writeup!
  5. You will also submit a signed copy of this cover sheet.
Electronic: The .cpp file containing the source code for your solution.


Christopher W Brown
Last modified: Tue Nov 16 23:13:04 EST 2004