Example 2: Bounded Least Squares

The nonlinear least-squares problem

\min \frac{1}{2}\sum\limits_{i = 0}^1 {f_i \left( x \right)^2 }

- 2 \le x_0 \le 0.5

-1 \le x_1 \le 2

where

f_0 (x) = 10(x_1 - x_0^2 ) \,\, {\rm{and}} \,\, f_1 (x) = (1 - x_0 )

is solved. An initial guess (-1.2, 1.0) is supplied, as well as the analytic Jacobian. The residual at the approximate solution is returned.


import com.imsl.math.*;

public class BoundedLeastSquaresEx2 {

    public static void main(String args[]) throws Exception {
        int m = 2;
        int n = 2;
        int ibtype = 0;
        double[] xlb = {-2.0, -1.0};
        double[] xub = {0.5, 2.0};
        double[] xguess = {-1.2, 1.0};

        BoundedLeastSquares.Function rosbck
                = new BoundedLeastSquares.Function() {
                    public void compute(double[] x, double[] f) {
                        f[0] = 10.0 * (x[1] - x[0] * x[0]);
                        f[1] = 1.0 - x[0];
                    }
                };

        BoundedLeastSquares.Jacobian jacob
                = new BoundedLeastSquares.Jacobian() {
                    public void compute(double[] x, double[] fjac) {
                        fjac[0] = -20.0 * x[0];
                        fjac[1] = 10.0;
                        fjac[2] = -1.0;
                        fjac[3] = 0.0;
                    }
                };

        BoundedLeastSquares zf
                = new BoundedLeastSquares(rosbck, m, n, ibtype, xlb, xub);

        zf.setJacobian(jacob);
        zf.setGuess(xguess);
        zf.solve();
        new PrintMatrix("Solution").print(zf.getSolution());
        new PrintMatrix("Residuals").print(zf.getResiduals());
    }
}

Output

Solution
    0    
0  0.5   
1  0.25  

Residuals
    0   
0  0    
1  0.5  

Link to Java source.