Trajectory Modeling Intro
12.36 Trajectory Modeling Intro
One of the earliest uses for computers was to model the trajectories of artillery shells at a target. In this project you are going to produce some code to find the best angle and best velocity to fire a cannon to hit a target. You can view a simulator that shows you what we’re trying to do here – click the Intro page to run the simulator.
The simulator allow you to play with the height of the cannon – lower it to 0m and try to hit the target. You can move the target to between 3.0m and 30.0m (actually you can move it more, but these are the distances we’re going to care about). You can move the angle of the cannon between 25.0 degrees and 90.0 degrees in increments of 5 degrees each. And you can change the initial speed of the cannonball coming out of the cannon to between 1 and 30 m/s. For this lab we want you to write a program that, given a speed, will compute the distance traveled for every possible angle in the simulator.
As you can see from the simulator, the important elements we need to care about are the angle that the cannon is pointed in and the initial speed that it applies to the cannonball that it shoots. (Interestingly the mass of the object you’re shooting doesn’t matter outside of getting it up to the initial speed – we’ll leave that explanation to your physics professors).
The formula for how long a projectile will stay in the air once launched is:
td=2vsin(θ)g
Where:
v=initial velocity in meters/second
θ=angle projectile is shot at
g=9.8m/s2
The distance d that the projectile travels is then:
d=vtdcos(θ)
Your job is to write a program that prompts the user for an initial speed, and then lists the distance travelled for all angles between 25.0 and 90.0 in increments of 5.0 (25.0, 30.0, 35.0, 4.0, etc. up to 90.0) to find the distance that a projectile at that angle will travel with the given initial speed.
A run of your program might look like this:
Enter an initial speed: 15.0
At angle 25.00 projectile travels 17.59 meters
At angle 30.00 projectile travels 19.88 meters
At angle 35.00 projectile travels 21.57 meters
At angle 40.00 projectile travels 22.61 meters
At angle 45.00 projectile travels 22.96 meters
At angle 50.00 projectile travels 22.61 meters
At angle 55.00 projectile travels 21.57 meters
At angle 60.00 projectile travels 19.88 meters
At angle 65.00 projectile travels 17.59 meters
At angle 70.00 projectile travels 14.76 meters
At angle 75.00 projectile travels 11.48 meters
At angle 80.00 projectile travels 7.85 meters
At angle 85.00 projectile travels 3.99 meters
A second run might look like this:
Enter an initial speed: 9.0
At angle 25.00 projectile travels 6.33 meters
At angle 30.00 projectile travels 7.16 meters
At angle 35.00 projectile travels 7.77 meters
At angle 40.00 projectile travels 8.14 meters
At angle 45.00 projectile travels 8.27 meters
At angle 50.00 projectile travels 8.14 meters
At angle 55.00 projectile travels 7.77 meters
At angle 60.00 projectile travels 7.16 meters
At angle 65.00 projectile travels 6.33 meters
At angle 70.00 projectile travels 5.31 meters
At angle 75.00 projectile travels 4.13 meters
At angle 80.00 projectile travels 2.83 meters
At angle 85.00 projectile travels 1.44 meters
A third run might look like this:
Enter an initial speed: 18.0
At angle 25.00 projectile travels 25.33 meters
At angle 30.00 projectile travels 28.63 meters
At angle 35.00 projectile travels 31.07 meters
At angle 40.00 projectile travels 32.56 meters
At angle 45.00 projectile travels 33.06 meters
At angle 50.00 projectile travels 32.56 meters
At angle 55.00 projectile travels 31.07 meters
At angle 60.00 projectile travels 28.63 meters
At angle 65.00 projectile travels 25.33 meters
At angle 70.00 projectile travels 21.25 meters
At angle 75.00 projectile travels 16.53 meters
At angle 80.00 projectile travels 11.31 meters
At angle 85.00 projectile travels 5.74 meters
NOTE ABOUT THE sin() and cos() FUNCTIONS: Note that the Math.sin(x) and Math.cos(x) do not work with degrees but instead work with radians. Fortunately, the Math library also gives us an easy way to convert between degrees and radians – the Math.toRadians(x) function. Here is some example code that you can paste below to test out before you start writing your code:
double degrees = 90.0;
double cos90 = Math.cos(Math.toRadians(degrees));
double sin90 = Math.sin(Math.toRadians(degrees));
System.out.format("cos(90) = %.2f%n", cos90);
System.out.format("sin(90) = %.2f%n", sin90);
If you run the above code you will see the following output:
cos(90) = 0.00
sin(90) = 1.00
NOTE ABOUT THE format() METHOD: In the above code you can see the format method in use. You can use this method to print out a number to 2 decimal places as required by this project. The format method uses a format string – this string prints like a normal println except you can insert variable values by using a %f, %d, or %s as format specifiers, which are replaced by the list of values that come after the string in the format method call. For example the following method call:
System.out.format("cos(90) = %.2f%n", cos90);
replaces the format specifier %.2f with the value of the double variable cos90. The % marks it as a format specifier, the f says it’s a floating-point value (double or float type), the .2 indicates that it will print with a precision of exactly 2 places after the decimal point, rounding up or down as necessary.
The %n in a format string indicates a newline character. Note that this is different from the n character which should be avoided – the %nformat specifier will be correctly replaced by the Java compiler to be a platform-independent end of line character (as it does with println()).
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.
