This is an example of using NLREG to find the minimum value of a
multivariate, nonlinear function.
The time taken for an object to slide down a
frictionless guide from position (0,h) to another position
(d,0) (i.e., falling through a distance h while moving
horizontally a distance d) depends on the path that the
object takes as it follows the guide. It turns out that the
path that minimizes the descent time is not a straight line
from (0,h) to (d,0) but rather a curve called a
brachistochrone (Greek for "shortest time") with a steeper slope near
the beginning, that gives the object a chance to accelerate quickly, and then a
shallower slope further on. (Technically, a brachistochrone is an inverted
Finding the shape of this curve is a classic problem in the
branch of mathematics called the Calculus of Variations. The
following NLREG example solves a simpler case of this problem: the
object slides along a straight guide from (0,1000) to an
intermediate position (px,py), and then along another straight
guide from (px,py) to (1000,0). What point, (px,py),
minimizes the descent time?
Title "Two segment path for fastest descent";
Parameter px; // X coordinate of bend
Parameter py; // Y coordinate of bend
Constrain px=.1,999; // px must be in range 0 < px < d
Constrain py=.1,999; // py must be in range 0 < py < h
Double G=980; // Acceleration of gravity = 980 cm/sec^2
Double sx=0, sy=1000; // Starting x and y coordinate
Double ex=1000, ey=0; // Ending x and y coordinate
Double d1,d2; // Length of each segment
Double a1,a2; // Acceleration along each segment
Double t1,t2; // Fall time along each segment
Double s1; // Speed at end of segment 1
* Determine length of each segment.
d1 = sqrt((px-sx)*(px-sx) + (py-sy)*(py-sy));
d2 = sqrt((px-ex)*(px-ex) + (py-ey)*(py-ey));
* Determine acceleration for each segment (proportional to slope).
a1 = G*(sy-py)/d1;
a2 = G*(py-ey)/d2;
* Determine time for segment 1 (starting speed is 0).
t1 = sqrt(2.*d1/a1);
* Determine speed at end of segment 1.
s1 = a1 * t1;
* Determine time for segment 2 (speed is s1 at start of segment).
t2 = (sqrt(s1*s1 + 2.*a2*d2) - s1) / a2;
* Minimize the total fall time.
function t1 + t2;