Well I cannot get the second and third out put to work correctly.

// Purpose is to program a simulation for the flight
of a cannonball.

#include <iostream>
#include <cmath>
using namespace std;

int main ()
       // Declare variables and constants.
       double vel_i, x_final=0, x_initial=0, time= 0.01,
       const double G = 9.8;
       const double DELTA_T = 0.01;

       // Get user input.
       cout << "Please input initial velocity of the
cannonball:\n ";
       cin >> vel_i;

       while (vel_i >=0)
               x_initial = x_initial + (vel_i * DELTA_T);
               cout << x_initial;// estimate position of cannon
               vel_i = vel_i - (G * DELTA_T);  // estimate answer.
               x_0 = -.5 * G * (time * time) +(x_initial * time);
//exact position.
               time =+.01;

               // Compute new velocity.
               cout << "The velocity of the cannonball is: " <<
vel_i << " m/s\n" << endl;

               // Compute new position of cannonball.
               cout << "The new position of the cannonball is: " <<
x_initial << " m\n" << endl;

               // Compute the exact formula.
               cout << "The exact position of the cannonball is: "
<< x_0 << " m\n" << endl;

       // Exit Program
       return 0;

The part cout << x_initial;// is just to show us an output before the very end. Any ways and idea what is wrong? Those things in there are supposivly the equations.

Homework Four CSci/GE 142 Winter 2006

Continue reading the textbook, according to the course calendar

Projectile Flight (from Horstmann Big C++)
Suppose a cannonball is propelled straight into the air with a starting velocity v0. Any calculus book will state that the position of the ball after t seconds is s(t) = (-1/2)g*t2 + v0t, 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
const double delta_t = 0.01;
and update the position by
s = s + v * delta_t;
The velocity changes constantly—in fact, it is reduced by the gravitational force of the earth. In a short time interval, ∆ v = -g * ∆t, we must keep the velocity updated as
v = v – g * delta_t;
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 print out the position only every full second. Also print out the values from the exact formula s(t) = (-1/2)g*t2 + v0t for comparison.

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 further 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 a ballistic missiles.

That is the HW assignment
Any help is greatly appreshiated.

should this : x_initial * time
be this: vel_i * time

and try putting this:
vel_i = vel_i - (G * DELTA_T); // estimate new velocity for the next DELTA_T.

after this:

// Compute new velocity.
cout << "The velocity of the cannonball is: " <<
vel_i << " m/s\n" << endl;

And reword your output statements to something like this:

cout << "for the " << i << " unit of time the velocity was " << vel_i << endl;
cout << "at the end of the " << i << " unit of time the estimated position was " << x_initial << " and the exact position was " << x_0 << endl;

where i is initialized to 1 at before the loop and incremented by 1 each time through the loop.

This article has been dead for over six months. Start a new discussion instead.