Example 1: Solving a general nonlinear programming problem

A general nonlinear programming problem is solved using a finite difference gradient.
import com.imsl.math.*;

public class MinConNLPEx1 implements MinConNLP.Function{
    
    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 static void main(String args[]) throws Exception {
        int     m = 2;
        int     me = 1;
        int     n = 2;
        double	xinit[] = {2., 2.};
        double  x[] = {0.};
        MinConNLP minconnon = new MinConNLP(m, me, n);
        minconnon.setGuess(xinit);
        MinConNLPEx1 fcn = new MinConNLPEx1();
        x = minconnon.solve(fcn);
        System.out.println("x is "+x[0] +" "+x[1]);
    }
}

Output

x is 0.8228756555325116 0.9114378277662559
Link to Java source.