Example 3: Minimum of a multivariate function

The minimum of 100(x_2 - x_1^2)^2 + (1-x_1)^2 is found using function evaluations and a user supplied Hessian.
import com.imsl.math.*;

public class MinUnconMultiVarEx3  {
    
    static class MyFunction implements MinUnconMultiVar.Hessian {
        public double f(double[] x) {
            return 100.*((x[1] - x[0] * x[0]) * (x[1] - x[0] * x[0])) +
            (1. - x[0]) * (1. - x[0]);
        }
        public void gradient(double[] x, double[] gp) {
            gp[0] =  -400. * (x[1] - x[0] * x[0]) * x[0] - 2. * (1. - x[0]);
            gp[1] = 200. * (x[1] - x[0]*x[0]);
        }
        public void hessian(double[] x, double[][] hess)
	{
		hess[0][0] = -4.e2*x[1] + 1.2e3*x[0]*x[0] + 2.e0;
                hess[1][0] = -4.e2*x[0];
                hess[0][1] = hess[1][0];
                hess[1][1] = 2.e2;
	}
    }
    
    public static void main(String args[]) throws Exception {
        MinUnconMultiVar solver = new MinUnconMultiVar(2);
        solver.setGuess(new double[]{-1.2, 1.0});
        double x[] = solver.computeMin(new MyFunction());
        System.out.println("Minimum point is (" +x[0] +", "+x[1]+")");
    }
}

Output

Minimum point is (1.0000000139452756, 1.0000000143547352)
Link to Java source.