A bullet is fired from a gun: what angle of
elevation of the gun will result in the bullet traveling the
maximum distance before hitting the ground? If you ignore the
effects of air resistance, the bullet travels in a parabolic
arc and it is easy to compute that the optimum firing angle is
45 degrees. However, if you consider the effects of air
resistance, this becomes a much more difficult problem -- one
for which there is no simple analytical solution.

This example uses NLREG to compute the firing angle that
optimizes the horizontal distance traveled, taking into
consideration the effects of air resistance. Since there is
no analytical function to compute the distance as a function
of the angle, the NLREG procedure uses an iterative procedure
to simulate the flight of the bullet and determine the
horizontal distance traveled for a specified angle. This
procedure uses the "finite differences" method to compute the
effects of gravitational acceleration and air resistance
forces on the bullet over short steps of time. For each
angle, the procedure iterates until the bullet hits the ground
and then determines the horizontal distance traveled at the
point of impact.

If you study the example, you will see that the force due to
air resistance is proportional to the square of the speed. In
this example, the initial muzzle velocity was chosen to be 300
meters/second. The air resistance coefficient was selected so
that the bullet would have a terminal velocity (if dropped
from a very high height through the air) of about 90
meters/second. If you set the air resistance coefficient to 0
(which has the effect of ignoring air resistance) you will
find that the computed optimum angle is about 45 degrees. But
with air resistance, the optimum angle is about 32 degrees.

Title "Firing angle for maximum range";
/* Parameter whose optimum value is to be determined */
Parameter Angle=40; /* Firing angle */
Constrain Angle=20,70; /* Put reasonable range on angle */
/* Constants for problem */
Constant Vm = 300; /* Muzzle velocity (m/s) */
Constant G = 9.80; /* Acceleration of gravity (m/s^2) */
Constant Airres = 0.0012; /* Air resistance factor */
Constant Step = 0.005; /* Time step (seconds) */
/* Work variables */
Double Vx,Vy,X,Y,V,Dar;
Double Xlast,Ylast;
/*
* Projectile is fired from ground level.
*/
X = 0;
Y = 0;
/*
* Compute initial X,Y velocities.
*/
Vx = Vm * cosd(Angle);
Vy = Vm * sind(Angle);
/*
* Simulate the flight of the projectile by advancing time
* through small steps until it hits the ground.
*/
do {
/*
* Compute new X,Y position using current velocity and
* time step.
*/
X += Step * Vx;
Y += Step * Vy;
/*
* Apply gravitational acceleration to Y velocity.
*/
Vy -= Step * G;
/*
* Apply air resistance to speeds.
* Note: Force of air resistance is proportional to the
* speed squared.
*/
/* Compute total linear speed */
V = sqrt(Vx^2 + Vy^2);
/* Determine deceleration due to air resistance */
Dar = Airres * V^2;
/* Apply this to the X and Y velocities */
Vx -= Step * Dar * Vx / V;
Vy -= Step * Dar * Vy / V;
/*
* Remember last position above the ground.
*/
if (Y > 0) {
Xlast = X;
Ylast = Y;
}
/*
* Loop until projectile hits the ground.
*/
} while (Y > 0);
/*
* Projectile hit the ground (and may be underground).
* Use last above-ground position to interpolate X
* at the point of impact (i.e., when Y was 0).
* Assume it moved in a straight line during the last step.
*/
X = Xlast - Ylast * ((X - Xlast) / (Y - Ylast));
/*
* Maximize the distance.
* Note: The absolute value of the function is minimized
* which results in X being maximized.
*/
Function 100000 - X;
Data;