/****************************************************** * Program to demonstrate the use of multi-dimensional * * Steepest Descent Optimization subroutine * * --------------------------------------------------- * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1]. * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * * --------------------------------------------------- * * Example: Find a local maximum of * * F(x,y,z) = sin(x)+2*cos(y)-sin(z) * * * * SAMPLE RUN: * * * * How many dimensions: 3 * * * * Convergence criterion: .000000001 * * * * Maximum number of iterations: 50 * * * * Starting constant: 1 * * * * Input the starting point: * * * * X[1] = 1 * * X[2] = 1 * * X[3] = 1 * * * * The results are: * * * * X[1] = 1.5707963 * * X[2] = -0.0000435 * * X[3] = -1.5707963 * * * * Local maximum = 4.0000000 * * * * The number of steps was: 30 * * * ******************************************************/ #include #include #define MACHEPS 1e-15 double D[4], Y[4]; double X[11],X1[11]; double e,xk; int i,l,m,n; /******************************************************** Function subroutine with derivatives [L=3] */ double Eval() { D[1]=cos(X[1]); D[2]=-2.0*sin(X[2]); D[3]=-cos(X[3]); return sin(X[1])+2.0*cos(X[2])-sin(X[3]); } /*******************************************************/ // Function called by Steepds() void Utilit() { int i; double dd; // Find the magnitude of the gradient dd=0.0; for (i=1; i0.0) xk=xk*1.2; // Decelerate if heading the wrong way e51: if (Y[3]Y[2]) goto e100; // Restore the X[i] for (i=1; i=m) return; if (fabs(Y[3]-Y[2])