Example 2: 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 gradient.
import com.imsl.math.*;

public class MinUnconMultiVarEx2  {
    
    static class MyFunction implements MinUnconMultiVar.Gradient {
        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 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 (0.9999999668823014, 0.9999999322542452)
Link to Java source.