#include <oxstd.h>
#import <maximize>

fRosenbrock(const vP, const adFunc, const avScore, const amHessian)
{
    adFunc[0] = -100 * (vP[1] - vP[0] ^ 2) ^ 2
        - (1 - vP[0]) ^ 2;               // function value

    if (avScore)                   // if !0: compute score
    {  // this bit is not needed for numerical derivatives
        (avScore[0])[0] = 400 * (vP[1] - vP[0]^2)
             * vP[0] + 2 * (1 - vP[0]);
        (avScore[0])[1] = -200 * (vP[1] - vP[0]^2);
    }
return 1;                           // 1 indicates success
}

main()
{
    decl vp, dfunc, ir;

    MaxControl(1000, 50);
	
    vp = zeros(2, 1);                   // starting values
    ir = MaxBFGS(fRosenbrock, &vp, &dfunc, 0, FALSE);

    print("\n", MaxConvergenceMsg(ir),
        " using analytical derivatives",
        "\nFunction value = ", dfunc, "; parameters:",vp);

    vp = zeros(2, 1);                   // starting values
    ir = MaxBFGS(fRosenbrock, &vp, &dfunc, 0, TRUE);

    print("\n", MaxConvergenceMsg(ir),
        " using numerical derivatives",
        "\nFunction value = ", dfunc, "; parameters:",vp);
}

