/*********************************************** Test Routine f(x0,x1) = (x0-1)^2 + (x1-6)^2 xinit = (-1.2, 1.0) xopt = (1,6) fopt = 0 ************************************************/ #include #include #include "opt.h" static double function(int n, double x[]) { double x0, x1; x0 = x[0]; x1 = x[1]; return (x0-1)*(x0-1) + (x1-6)*(x1-6); } static double *grad_function(int n, double x[], double grad[]) { dbl x0, x1; x0 = x[0]; x1 = x[1]; grad[0] = 2*(x0-1); grad[1] = 2*(x1-6); return grad; } main() { int n = 2; double x[2]; double fopt; int timeout = 1000; double eps = 1.0e-20; x[0] = -1.20; x[1] = 1.00; if (QuasiNewtonMethod(n, function, grad_function, x, &fopt, timeout, eps) == success) { printf("converge "); } else { printf("timeout "); } printf(" [ %lf %lf ] %lf\n", x[0], x[1], fopt); }