After having demonstrated linear programming in R I want to introduce quadprog, a package for modelling and solving quadratic problems.

On stackoverflow, a useful discussion in this context is here: https://stackoverflow.com/questions/31498344/quadratic-optimization-in-r-with-both-equality-and-inequality-constraints

Here, I demonstrate a working example for how to solve a quadratic problem with the solve.QP function available in quadprog for R.

Below follows a mathematical formulation of the problem to solve:

To model this problem in accordance with general quadratic programming logic we must apply vector and matrix calculation, and state the problem in the following form:

I do this in the code below:

``````# matrix definitions
C <- rbind(c(2,0),
c(0,4))

A <- rbind(c(1,1),
c(2,3))

# vector definitions
d <- c(-6,-20)

b <- c(10,0)``````

Now, I model the problem using the solve.QP function provided in the quadprog package:

``````library(quadprog)

# model and solve
solution <- solve.QP(Dmat = C,
dvec = d,
Amat = A,
bvec = b)``````

What is the optimal value of this quadratic problem?

``solution\$value + 5``
``## [1] 122.3333``

What is the solution to this quadratic optimization problem?

``solution\$solution``
``## [1] 4.666667 2.666667``

Feel free to check out my other posts on optimization in R, like e.g. with lpSolve and nloptr.