You are required to confirm the equation by a
simulation. In this simulation, you will consider how the ball moves
in very short time interval Δt.
I. You will assume that Δt is a constant double with a
value of 0.01. You can get the updated position using s = s + v * Δ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, you must
keep the velocity updated as v = v - g * Δt; In the next iteration, the new velocity is used to update the
distance.
You need to run the simulation until the cannon ball falls back to the ground. Your program should take
the initial velocity as an input. Update the position and velocity 100 times per second, but print out the
position only every full second. Also, printout the values from the exact formula s = vi t – (1/2) gt2
for the
comparison. Lastly plot the path that cannon ball will take to get the bonus marks.
#include <iostream>
using namespace std;
int main()
{
double t=0.01;
int v;
cout<<"\nEnter initial velocity: ";
cin>>v;
cout<<"\nEnter initial position: ";
int s;
cin>>s;
cout<<"\nEnter gravity: ";
int g;
cin>>g;
for(int i=0;i<100;i++){
s = s + v * t;
v = v - g * t;
}
cout<<"\nFinal position = "<<s;
return 0;
}
Comments
Leave a comment