Example 2: Solving a general nonlinear programming problem

A general nonlinear programming problem is solved using a user-supplied gradient.
import com.imsl.math.*;

public class MinConNLPEx2 implements MinConNLP.Gradient{
    
    public double f(double[] x, int iact, boolean[] ierr){
            double result;
            ierr[0] = false;
                if(iact == 0){
                    result = (x[0]-2.e0)*(x[0]-2.e0) + (x[1]-1.e0)*(x[1]-1.e0);
                    return result;
                } else {
                    switch (iact) {
                        case 1: 
                            result = (x[0]-2.e0*x[1] + 1.e0);
                            return result;
                        case 2: 
                            result = (-(x[0]*x[0])/4.e0 - (x[1]*x[1]) + 1.e0);
                            return result;
                        default:
                            ierr[0] = true;
                            return 0.e0;
                    }
	        }
    }
    
    
    public void gradient(double[] x, int iact, double[] result){
                if(iact == 0){
                    result[0] = 2.e0*(x[0]-2.e0);
                    result[1] = 2.e0*(x[1]-1.e0);
                    return;
                } else {
                    switch (iact) {
                        case 1: 
                            result[0] = 1.e0;
			    result[1] = -2.e0;
                            return;
                        case 2: 
                            result[0] = -0.5e0*x[0];
			    result[1] = -2.e0*x[1];
                            return;
                    }
	        }
    }
    
    public static void main(String args[]) throws Exception {
        int     m = 2;
        int     me = 1;
        int     n = 2;
        MinConNLP minconnon = new MinConNLP(m, me, n);
        minconnon.setGuess(new double[]{2.,2.});
        MinConNLPEx2 grad = new MinConNLPEx2();
        double x[] = minconnon.solve(grad);
        System.out.println("x is "+x[0] +" "+x[1]);
    }
}

Output

x is 0.8228756555325117 0.9114378277662558
Link to Java source.