1

In the following system, all variables on the right hand side are known, except for w1 and w2, for which we are trying to solve.

enter image description here

I am trying to solve this system in R, but I am not sure where to start. Also, in my real example, the system is composed of 10 equations instead of two: we are trying to solve for w1, w2 where FA = FB = FC = FD = .... = Fj.

Please let me know if the explanation is a bit confusing, and I will try to explain better! Thanks!

Mayou
  • 8,498
  • 16
  • 59
  • 98
  • With the additional restrictions you show us you can do this with R's builtin function `uniroot`. You only solve for `w2` (define `w1` as `1-w2`) and define the function value as `FA-FB`. You can also have a look at package nleqslv (available on CRAN). – Bhas Jun 19 '14 at 18:47
  • Thanks for your suggestion. So in `uniroot`, how can you define `w2 = 1 - w1`? Also, is it possible to define additional constraints, like `w1 > 0` and `w2 > 0`? Also, what if I was solving for three variables, `w1`, `w2` and `w3` where `w1 + w2 + w3 = 1`? Is it still possible to do it in uniroot? – Mayou Jun 19 '14 at 18:56
  • Look at package [`nloptr`](http://cran.r-project.org/web/packages/nloptr/nloptr.pdf). Using `nloptr(...)` you would minimize (RHS-LHS) as the objective function. – jlhoward Jun 19 '14 at 19:21
  • An alternative might be `BBsolve` or `BBoptim` from the `BB` package. Or even (I sneak this one in whenever I can :-) ) the `ktsolve` package which provides a wrapper to `BBsolve` . – Carl Witthoft Jun 19 '14 at 19:40
  • @Mariam. You define `w2 <- 1-w1` in the function calculating `FA-FB`. If you have more than two variables, you can't use `uniroot`. You will have to follow my suggestion or the other suggestions: `nleqslv`, `ktsolve`,`nloptr, `BBsolve`, `BBoptim`. With the non-negativity constraints have a look at the builtin function `constrOptim`. The sum conatraint can always be transformed into something as `w_n <- w1+w2+...+w_(n-1)` – Bhas Jun 19 '14 at 20:04
  • There was a typo in my previous post. I have now corrected it. Thanks – Mayou Jun 20 '14 at 14:02

0 Answers0