Example 2: Nonlinear least-squares problem

A nonlinear least-squares problem is solved using a user-supplied Jacobian.
import com.imsl.math.*;

public class NonlinLeastSquaresEx2 {
    public static void main(String args[]) throws
    NonlinLeastSquares.TooManyIterationsException {
        
        NonlinLeastSquares.Jacobian zsj = new NonlinLeastSquares.Jacobian() {
            public void f(double x[], double f[]) {
                f[0] = 10. * (x[1] - x[0]*x[0]);
                f[1] = 1. - x[0];
            }
            public void jacobian(double x[], double fjac[][]) {
                fjac[0][0] = -20.*x[0];
                fjac[1][0] = 10.;
                fjac[0][1] = -1.;
                fjac[1][1] = 0.;
            }
        };
        
        int	m = 2;
        int	n = 2;
        double	xguess[] = {-1.2, 1.};
        double	xscale[] = {1., 1.};
        double	fscale[] = {1., 1.};
        double	x[] = new double[2];
        NonlinLeastSquares zs = new NonlinLeastSquares(m,n);
        zs.setGuess(xguess);
        zs.setXscale(xscale);
        zs.setFscale(fscale);
        x = zs.solve(zsj);
        
        for (int k = 0;  k < n;  k++) {
            System.out.println("x["+k+"] = "+x[k]);
        }
    }
}

Output

x[0] = 1.0
x[1] = 1.0
Link to Java source.