title: solve simple quadratic optimization in R with sum constraint
body: <p>I want to solve a very simple quadratic optimization problem in R where one of the constraints is an equality constraint related to the sum of a vector. I tried to use the quadprog package but I cannot figure out how to make it work. The help function does not provide an example where the one of the constraints equals the sum. But perhaps quadprog is not the right package to use...</p>
  
  <p>My problem is the following. I would like to shrink/expand a vector of values in such a way that the sum is equal to a set value and their values are bound by 0 and a maximum value. As the results need to be as close to the initial values as possible I want to minimize the residual sum of squares. Below I present a toy example. It is easy to see that for example (5,5,6) is in the solution space but not the optimal solution.  </p>
  
  <pre><code>x_start &lt;- c(4,3,7)
  tot &lt;- 16
  
  # Objective function that needs to be minimized
  obj &lt;- function(x){return((x-x_start)^2)}
  
  # Subject to: 
  # Sum constraint
  con1 &lt;- function(x) {return(sum(x)-tot)}
  
  # Bounds 0 &lt;= x &lt;=6
  x_max &lt;- 6
  x_min &lt;- 0
  
  </code></pre>