A cannon ball is thrown straight into the air with velocity
v0. Position of the cannon ball after t seconds is given by, s = vi t – (1/2) gt.You are required to confirm equation by a simulation. In simulation, you will consider how the ball moves in very short time interval Δt.In a short time interval, the velocity v is nearly constant, and we can compute distance the ball moves using Δs = vΔt. Δt is a constant double with value 0.01. Get updated position using s = s + v * Δt. Velocity changes constantly—in fact, it is reduced by the gravitational force. In a short time interval, Δv = –gΔt, you must keep the velocity updated as v = v - g * Δt; In next iteration, new velocity is used to update distance.You need to run simulation until cannon ball falls back to ground. Take initial velocity as input. Update position and velocity 100 times per second, but print out the position only every full second. Also, printout values from exact formula s = vi t – (1/2) gt2 for comparison. Lastly plot path.
#include<math.h>
using namespace std;
int main()
{
double t=0.01;
int v=0;
cout<<"\nEnter initial position: ";
double s;
cin>>s;
cout<<"\nEnter gravity: ";
int g;
cin>>g;
for(int i=0;i<100;i++){
s = v*t - (1/2)*g*pow(t,2);
s = s + v * t;
v = v - g * t;
}
cout<<"\nFinal position = "<<s;
return 0;
Comments
Leave a comment