// trajectory of projectile with drag // #include #include using namespace std; int main() { const double dt = .01; // time increments const double D = .00126; // drag coefficient const double G = 9.8; // gravity const double V0 = 500; // initial velocity double angle; cout << " Enter an angle " << endl; cin >> angle; double x=0, y=0; double vx = V0 * cos(angle *(M_PI / 180)); double vy = V0 * sin(angle *(M_PI / 180)); double xsave, ysave; while (y>=0) { double vtotal = sqrt((vx * vx) + (vy * vy)); xsave = x; ysave = y; x = x + dt * (vx); y = y + dt * (vy); vx = vx + dt * (-D * vx * vtotal); vy = vy + dt * (-D * vy * vtotal -G); cout << x << " " << y << endl; } // note that the final point has y < 0 // to get the precise point where y=0, can do // a simple interpolation step as follows: //double xfinal = x - y*(xsave-x)/(ysave-y); //cout << " projectile distance = " << xfinal << endl; }