CNLMath : Linear Systems : lin_lsq_lin_constraints
lin_lsq_lin_constraints
Solves a linear least-squares problem with linear constraints.
Synopsis
#include <imsl.h>
float *imsl_f_lin_lsq_lin_constraints (int nra, int nca, int ncon, float a[], float b[], float c[], float bl[], float bu[], int con_type[], float xlb[], float xub[], ..., 0)
The type double function is imsl_d_lin_lsq_lin_constraints.
Required Arguments
int nra (Input)
Number of least-squares equations.
int nca (Input)
Number of variables.
int ncon (Input)
Number of constraints.
float a[] (Input)
Array of size nra × nca containing the coefficients of the nra least-squares equations.
float b[] (Input)
Array of length nra containing the right-hand sides of the least-squares equations.
float c[] (Input)
Array of size ncon × nca containing the coefficients of the ncon constraints.
float bl[] (Input)
Array of length ncon containing the lower limit of the general constraints. If there is no lower limit on the ith constraint, then bl[i] will not be referenced.
float bu[] (Input)
Array of length ncon containing the upper limit of the general constraints. If there is no upper limit on the ith constraint, then bu[i] will not be referenced. If there is no range constraint, bl and bu can share the same storage.
int con_type[] (Input)
Array of length ncon indicating the type of constraints exclusive of simple bounds, where con_type[i] = 0, 1, 2, 3 indicates =, <=, >= and range constraints, respectively.
float xlb[] (Input)
Array of length nca containing the lower bound on the variables. If there is no lower bound on the ith variable, then xlb[i] should be set to 1.0e30.
float xub[] (Input)
Array of length nca containing the upper bound on the variables. If there is no lower bound on the ith variable, then xub[i] should be set to 1.0e30.
Return Value
A pointer to the to a vector of length nca containing the approximate solution. To release this space, use imsl_free. If no solution was computed, then NULL is returned.
Synopsis with Optional Arguments
#include <imsl.h>
float *imsl_f_lin_lsq_lin_constraints (int nra, int nca, int ncon, float a[], float b[], float c[], float bl[], float bu[], int con_type[], float xlb[], float xub[],
IMSL_RETURN_USER, float x[],
IMSL_RESIDUAL, float **residual,
IMSL_RESIDUAL_USER, float residual_user[],
IMSL_PRINT,
IMSL_ITMAX, int max_iter,
IMSL_REL_FCN_TOL, float rel_tol,
IMSL_ABS_FCN_TOL, float abs_tol,
0)
Optional Arguments
IMSL_RETURN_USER, float x[] (Output)
Store the solution in the user supplied vector x of length nca.
IMSL_RESIDUAL, float **residual (Output)
The address of a pointer to an array containing the residuals b  Ax of the least-squares equations at the approximate solution.
IMSL_RESIDUAL_USER, float residual_user[] (Output)
Store the residuals in the user-supplied vector of length nra.
IMSL_PRINT,
Debug output flag. Choose this option if more detailed output is desired.
IMSL_ITMAX, int max_iter (Input)
Set the maximum number of add/drop iterations.
Default: max_iter = 5*max(nranca)
IMSL_REL_FCN_TOL, float rel_tol (Input)
Relative rank determination tolerance to be used.
Default: rel_tol = sqrt(imsl_f_machine(4))
IMSL_ABS_FCN_TOL, float abs_tol (Input)
Absolute rank determination tolerance to be used.
Default: abs_tol = sqrt(imsl_f_machine(4))
Description
The function imsl_f_lin_lsq_lin_constraints solves linear least-squares problems with linear constraints. These are systems of least-squares equations of the form
Ax b
subject to
bl Cx bu
xl x xu
Here A is the coefficient matrix of the least-squares equations, b is the right-hand side, and C is the coefficient matrix of the constraints. The vectors bl, bu, xl and xu are the lower and upper bounds on the constraints and the variables, respectively. The system is solved by defining dependent variables y  Cx and then solving the least-squares system with the lower and upper bounds on x and y. The equation Cx  y = 0 is a set of equality constraints. These constraints are realized by heavy weighting, i.e., a penalty method, Hanson (1986, pp. 826-834).
Examples
Example 1
In this example, the following problem is solved in the least-squares sense:
3x1 + 2x2 + x3 = 3.3
4x1 +2x2 + x3 = 2.2
2x1 + 2x2 + x3 = 1.3
x1 + x2 + x3 = 1.0
Subject to
x1 = x2 + x3 1
0 x1 0.5
0 x2 0.5
0 x3 0.5
 
#include <imsl.h>
 
int main()
{
int nra = 4;
int nca = 3;
int ncon = 1;
float *x;
float a[] = {3.0, 2.0, 1.0,
4.0, 2.0, 1.0,
2.0, 2.0, 1.0,
1.0, 1.0, 1.0};
float b[] = {3.3, 2.3, 1.3, 1.0};
float c[] = {1.0, 1.0, 1.0};
float xlb[] = {0.0, 0.0, 0.0};
float xub[] = {0.5, 0.5, 0.5};
int con_type[] = {1};
float bc[] = {1.0};
 
x = imsl_f_lin_lsq_lin_constraints (nra, nca, ncon, a, b, c,
bc, bc, con_type, xlb, xub,
0);
imsl_f_write_matrix ("Solution", 1, nca, x,
0);
}
Output
 
Solution
1 2 3
0.5 0.3 0.2
Example 2
The same problem solved in the first example is solved again. This time residuals of the least-squares equations at the approximate solution are returned, and the norm of the residual vector is printed. Both the solution and residuals are returned in user-supplied space.
 
#include <imsl.h>
#include <stdio.h>
 
int main()
{
int nra = 4;
int nca = 3;
int ncon = 1;
float x[3];
float residual[4];
float a[] = {3.0, 2.0, 1.0,
4.0, 2.0, 1.0,
2.0, 2.0, 1.0,
1.0, 1.0, 1.0};
float b[] = {3.3, 2.3, 1.3, 1.0};
float c[] = {1.0, 1.0, 1.0};
float xlb[] = {0.0, 0.0, 0.0};
float xub[] = {0.5, 0.5, 0.5};
int con_type[] = {1};
float bc[] = {1.0};
 
imsl_f_lin_lsq_lin_constraints (nra, nca, ncon, a, b, c,
bc, bc, con_type, xlb, xub,
IMSL_RETURN_USER, x,
IMSL_RESIDUAL_USER, residual,
0);
 
imsl_f_write_matrix ("Solution", 1, nca, x, 0);
imsl_f_write_matrix ("Residual", 1, nra, residual, 0);
printf ("\n\nNorm of residual = %f\n",
imsl_f_vector_norm (nra, residual, 0));
}
 
Output
 
Solution
1 2 3
0.5 0.3 0.2
 
Residual
1 2 3 4
-1.0 0.5 0.5 -0.0
 
Norm of residual = 1.224745
Fatal Errors
IMSL_BAD_COLUMN_ORDER
The input order of columns must be between 1 and “nvar” while input order = # and “nvar” = # are given.
IMSL_BAD_POLARITY_FLAGS
The bound polarity flags must be positive while component # flag “ibb[#]”.
IMSL_TOO_MANY_ITN
Maximum numbers of iterations exceeded.