[R] lsqlin in pracma

Berend Hasselman bhh at xs4all.nl
Sat Aug 29 21:31:54 CEST 2015


> On 29 Aug 2015, at 20:53, Berend Hasselman <bhh at xs4all.nl> wrote:
> 
> 
>> On 29 Aug 2015, at 18:29, Ravi Varadhan <ravi.varadhan at jhu.edu> wrote:
>> 
>> In solve.QP(), you don't need to expand the equality into two inequalities.  It can DIRECTLY handle the equality constraints.  The first `meq' rows of the constraint matrix are equality constraints. Here is the excerpt from  the documentation.
>> 
>> meq
>> the first meq constraints are treated as equality constraints, all further as inequality constraints (defaults to 0).
>> 
>> 
>> Therefore, solve.QP() can provide the full functionality of lsqlin in Matlab.  However, one caveat is that the bounds constraints have to be implemented via inequalities in solve.QP(), which is a slight pain, but not a deal breaker.
>> 
> 
> It would be helpful if you could show us how to use solve.QP() in this case.
> I’ve been trying with no success.
> 
> B

I’ll answer my comment.

# Example from Matlab for lsqlin

C <- matrix(c(
    0.9501,   0.7620,   0.6153,   0.4057,
    0.2311,   0.4564,   0.7919,   0.9354,
    0.6068,   0.0185,   0.9218,   0.9169,
    0.4859,   0.8214,   0.7382,   0.4102,
    0.8912,   0.4447,   0.1762,   0.8936), 5, 4, byrow=TRUE)
d <- c(0.0578, 0.3528, 0.8131, 0.0098, 0.1388)
A <- matrix(c(
    0.2027,   0.2721,   0.7467,   0.4659,
    0.1987,   0.1988,   0.4450,   0.4186,
    0.6037,   0.0152,   0.9318,   0.8462), 3, 4, byrow=TRUE)
b <- c(0.5251, 0.2026, 0.6721)

Dmat <- t(C) %*% C
dvec <- (t(C) %*% d)

Aeq <- c(3, 5, 7, 9)
beq <- 4
lb <- rep(-0.1, 4)   # lower and upper bounds
ub <- rep( 2.0, 4)

Amat <- rbind(Aeq,-A,diag(4),-diag(4))
bvec <- c(beq,-b,lb,-ub)
rslt <- solve.QP(Dmat, dvec, t(Amat), bvec, meq=1)
rslt$solution
sum(Aeq * rslt$solution) - beq
sum((C %*% rslt$solution - d)^2)


What a fiddle.

Berend



More information about the R-help mailing list