Vous êtes sur la page 1sur 2

Projectile flight. Suppose a cannonball is propelled vertically into the air with a starting velocity v0.

Any calculus book will tell us that the position of the ball after t seconds is s(t) = -0.5 g t2 + v0 t, where g 9.81 m/sec2 is the gravitational force of the earth. No calculus book ever mentions why someone would want to carry out such an obviously dangerous experiment, so we will do it in the safety of the computer. In fact, we will confirm the theorem from calculus by a simulation. In our simulation, we will consider how the ball moves in very short time intervals t. In a short time interval the velocity v is nearly constant, and we can compute the distance the ball moves as s = v t. In our program, we will simply set double deltaT = 0.01; and update the position by s = s + v * deltaT; The velocity changes constantlyin fact, it is reduced by the gravitational force of the earth. In a short time interval, v decreases by g t, and we must keep the velocity updated as v = v - g * deltaT; In the next iteration the new velocity is used to update the distance. Now run the simulation until the cannonball falls back to the earth. Get the initial velocity as an input (100 m/sec is a good value). Update the position and velocity 100 times per second, but only print out the position every full second. Also print out the values from the exact formula s(t) = -0.5 g t2 + v0 t for comparison. Use a class Cannonball. What is the benefit of this kind of simulation when an exact formula is available? Well, the formula from the calculus book is not exact. Actually, the gravitational force diminishes the farther the cannonball is away from the surface of the earth. This complicates the algebra sufficiently that it is not possible to give an exact formula for the actual motion, but the computer simulation can simply be extended to apply a variable gravitational force. For cannonballs, the calculus-book formula is actually good enough, but computers are necessary to compute accurate trajectories for higher-flying objects such as ballistic missiles. Here is a sample program run. Enter the initial velocity: 100 Simulation position: 95.14404999999988 Velocity: 90.18999999999977 The exact formula position is: 95.09500000000007 Simulation position: 180.47809999999953 Velocity: 80.37999999999954 The exact formula position is: 180.3800000000001 Simulation position: 256.002149999999 Velocity: 70.56999999999931 The exact formula position is: 255.8549999999986 Simulation position: 321.7161999999982 Velocity: 60.75999999999908 The exact formula position is: 321.5199999999975 Simulation position: 377.62024999999716 Velocity: 50.94999999999885 The exact formula position is: 377.3749999999968 Simulation position: 423.714299999996 Velocity: 41.13999999999862 The exact formula position is: 423.4199999999966 Simulation position: 459.9983499999944 Velocity: 31.329999999998417 The exact formula position is: 459.6549999999967 Simulation position: 486.4723999999931 Velocity: 21.519999999998543 The exact formula position is: 486.07999999999726 Simulation position: 503.13644999999144 Velocity: 11.70999999999859 The exact formula position is: 502.69499999999823 Simulation position: 509.99049999999016 Velocity: 1.8999999999985846 The exact formula position is: 509.4999999999997

Your main class should be called CannonballRunner. Complete the following class in your solution: /** This class simulates a cannonball fired up in the air. */ public class Cannonball { /** Creates a Cannonball object at position 0. @param ivel the inital velocity */ public Cannonball(double ivel) { . . . } /** Updates the position and velocity of this cannonball after a given time interval. @param deltaT the time interval */ public void move(double deltaT) { . . . } /** Gets the velocity of this cannonball. @return the velocity */ public double getVelocity() { . . . } /** Gets the position of this cannon ball. @return the (vertical) position */ public double getPosition() { . . . } // private implementation . . . } Use the following class as your tester class: public class CannonballTester { public static void main(String[] args) { Cannonball ball = new Cannonball(100); // 100 m/sec ball.move(1); // move by one second System.out.println(ball.getPosition()); System.out.println("Expected: 100"); System.out.println(ball.getVelocity()); System.out.println("Expected: " + (100 - 9.81)); } }

Vous aimerez peut-être aussi