It works for me: > x <- 2*c(1,0.5,0.8,0.5,1,0.9, 0.8,0.9,1) > Dmat <- matrix(x, byrow=T, nrow=3, ncol=3) > dvec <- numeric(3) > Amat <- matrix(0,3,4) > Amat[,1 ] <- c(1,1,1) > Amat[,2:4 ]<- t(diag(3)) > bvec <- c(3,0,0,0) > > solve.QP(Dmat,dvec,Amat,bvec=bvec, meq=1) $solution [1] 1.5 1.5 0.0
$value [1] 6.75 $unconstrained.solution [1] 0 0 0 $iterations [1] 3 0 $iact [1] 1 4 > > R.version.string [1] "R version 2.10.1 (2009-12-14)" > win.version() [1] "Windows Vista (build 6002) Service Pack 2" > packageDescription("quadprog")$Version [1] "1.4-12" On Sun, Apr 11, 2010 at 11:31 AM, li li <hannah....@gmail.com> wrote: > Hi, > thanks! > > I added meq=1 and it did not seem to work. The result is the same as before. > >> x <- 2*c(1,0.5,0.8,0.5,1,0.9, 0.8,0.9,1) >> Dmat <- matrix(x, byrow=T, nrow=3, ncol=3) >> dvec <- numeric(3) >> Amat <- matrix(0,3,4) >> Amat[,1 ] <- c(1,1,1) >> Amat[,2:4 ]<- t(diag(3)) >> bvec <- c(3,0,0,0) >> >> solve.QP(Dmat,dvec,Amat,bvec=bvec, meq=1) > $solution > [1] 1.500000e+00 1.500000e+00 -8.881784e-16 > $value > [1] 6.75 > $unconstrained.solution > [1] 0 0 0 > $iterations > [1] 3 0 > $Lagrangian > [1] 4.5 0.0 0.0 0.6 > $iact > [1] 1 4 >> > > > 2010/4/11 Gabor Grothendieck <ggrothendi...@gmail.com> >> >> Add meq=1 to the arguments. >> >> On Sun, Apr 11, 2010 at 9:50 AM, li li <hannah....@gmail.com> wrote: >> > Hi, thank you very much for the reply! >> > >> > Consider minimize quadratic form w'Aw with A be the following matrix. >> >> Dmat/2 >> > [,1] [,2] [,3] >> > [1,] 1.0 0.5 0.8 >> > [2,] 0.5 1.0 0.9 >> > [3,] 0.8 0.9 1.0 >> > I need to find w=(w1,w2,w3), a 3 by 1 vector, such that sum(w)=3, and >> > wi>=0 >> > for all i. >> > >> > Below is the code I wrote, using the function solve.QP , however, the >> > solution for w still have a >> > negtive component. Can some one give me some suggestions? >> > >> > Thank you very much! >> > >> >> x <- 2*c(1,0.5,0.8,0.5,1,0.9, 0.8,0.9,1) >> >> Dmat <- matrix(x, byrow=T, nrow=3, ncol=3) >> >> dvec <- numeric(3) >> >> Amat <- matrix(0,3,4) >> >> Amat[,1 ] <- c(1,1,1) >> >> Amat[,2:4 ]<- t(diag(3)) >> >> bvec <- c(3,0,0,0) >> >> >> >> solve.QP(Dmat,dvec,Amat,bvec=bvec) >> > $solution >> > [1] 1.500000e+00 1.500000e+00 -8.881784e-16 >> > $value >> > [1] 6.75 >> > $unconstrained.solution >> > [1] 0 0 0 >> > $iterations >> > [1] 3 0 >> > $Lagrangian >> > [1] 4.5 0.0 0.0 0.6 >> > $iact >> > [1] 1 4 >> > >> > >> > >> > >> > >> > >> > >> > >> > >> > >> > >> > 2010/4/10 Gabor Grothendieck <ggrothendi...@gmail.com> >> >> >> >> Check out the quadprog package. >> >> >> >> On Sat, Apr 10, 2010 at 5:36 PM, li li <hannah....@gmail.com> wrote: >> >> > Hi, thanks for the reply. >> >> > A will be a given matrix satisfying condition 1. I want to find the >> >> > vector w that minimizes the >> >> > quadratic form. w satisfies condition 2. >> >> > >> >> > >> >> > 2010/4/10 Paul Smith <phh...@gmail.com> >> >> > >> >> >> On Sat, Apr 10, 2010 at 5:13 PM, Paul Smith <phh...@gmail.com> >> >> >> wrote: >> >> >> >> I am trying to minimize the quardratic form w'Aw, with certain >> >> >> >> constraints. >> >> >> >> In particular, >> >> >> >> (1) A=(a_{ij}) is n by n matrix and it is symmetric positive >> >> >> definite, >> >> >> >> a_{ii}=1 for all i; >> >> >> >> and 0<a_{ij}<1 for i not equal j. >> >> >> >> (2) w'1=n; >> >> >> >> (3) w_{i}>=0 >> >> >> >> >> >> >> >> Analytically, for n=2, it is easy to come up with a result. For >> >> >> >> larger >> >> >> n, it >> >> >> >> seems >> >> >> >> difficult to obtain the result. >> >> >> >> >> >> >> >> Does any one know whether it is possible to use R to numerically >> >> >> >> compute >> >> >> it? >> >> >> > >> >> >> > And your decision variables are? Both w[i] and a[i,j] ? >> >> >> >> >> >> In addition, what do you mean by "larger n"? n = 20 is already large >> >> >> (in your sense)? >> >> >> >> >> >> Paul >> >> >> >> >> >> ______________________________________________ >> >> >> R-help@r-project.org mailing list >> >> >> https://stat.ethz.ch/mailman/listinfo/r-help >> >> >> PLEASE do read the posting guide >> >> >> >> >> >> >> >> >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> >> >> and provide commented, minimal, self-contained, reproducible code. >> >> >> >> >> > >> >> > [[alternative HTML version deleted]] >> >> > >> >> > ______________________________________________ >> >> > R-help@r-project.org mailing list >> >> > https://stat.ethz.ch/mailman/listinfo/r-help >> >> > PLEASE do read the posting guide >> >> > http://www.R-project.org/posting-guide.html >> >> > and provide commented, minimal, self-contained, reproducible code. >> >> > >> > >> > > > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.