Example 1: Bounded Value Least Squares

The following example solves a linear least squares problem with bounds on the variables and compares the result to its unbounded solution. The normal of the residuals is 0.0 for the exact solution.
import com.imsl.math.*;

public class BoundedVariableLeastSquaresEx1 {
    public static void main(String args[]) throws Exception {
        double	a[][] = {
            { 1, -3,  2},
            {-3, 10, -5},
            { 2, -5,  6}};
        double	b[] = {27, -78, 64};
        double  xlb[] = {-1, -1, -1};
        double  xub[] = {5, 5, 5};
        double  xl_ub[] = {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY,
                           Double.NEGATIVE_INFINITY};
        double  xu_ub[] = {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY,
                           Double.POSITIVE_INFINITY};

        // compute the bounded solution
        BoundedVariableLeastSquares bvls =
            new BoundedVariableLeastSquares(a, b, xlb, xub);
        bvls.solve();
        double[] x_bounded = bvls.getSolution();
        new PrintMatrix("Bounded Solution").print(x_bounded);
        System.out.println("Norm of the Residuals = "+bvls.getResidualNorm());

        // compute the unbounded solution
        bvls = new BoundedVariableLeastSquares(a, b, xl_ub, xu_ub);
        bvls.solve();
        double[] x_unbounded = bvls.getSolution();
        new PrintMatrix("\nUnbounded Solution").print(x_unbounded);
        System.out.println("Norm of the Residuals = "+bvls.getResidualNorm());
    }
}

Output

Bounded Solution
   0   
0   5  
1  -1  
2   5  

Norm of the Residuals = 35.0142828000232

Unbounded Solution
   0   
0   1  
1  -4  
2   7  

Norm of the Residuals = 0.0
Link to Java source.