[R] Using multiroot for root solution for a matrix based function

Berend Hasselman bhh @end|ng |rom x@4@||@n|
Tue Nov 17 20:34:12 CET 2020


Forgot to send this to R-help.


You have already asked this question  on stackoverflow (https://stackoverflow.com/questions/64835251/how-can-i-use-multiroot-with-matrix-based-functions-in-r/64835725#64835725).

In a comment  G. Grothendieck provided an answer. Have you tried it?

Define a function f.mod that converts a vector to a matrix like this

f.mod <-  function(q,m) f(matrix(q,2),m)

Then try multiroot with an appropriate starting value

q.start <- rep(0,4)
multiroot(f.mod, q.start, parms=m)

Finally convert the answer provided by multiroot to a matrix.

You can also use other solvers. Such as nleqslv like this

library(nleqslv)
nleqslv(q.start,f.mod,m=m) # uses Broyden
nleqslv(q.start,f.mod,m=m,method="Newton") # uses Newton

The second call of nleqslv uses less iterations that multiroot.
You can check the answers.


Berend Hasselman

> On 17 Nov 2020, at 09:19, quinter sam <qsam0000 using gmail.com> wrote:
> 
> I have a function which is actually an output of another function and I
> therefore cannot change it. I am trying to use *multiroot * from
> package *rootSolve
> * to compute the roots of the function but its not working at all. Is there
> something I am not seeing or is there another alternative that is based on
> Newton-Raphson technique?
> 
> library(rootSolve)
> f <- function(q,m){
> c(F1 = 12 * ((exp(q[, 1]) * m[1])/(exp(q[, 1]) * m[1] + exp(q[, 2]) * m[2]
> + m[3])) - c(1,2),
> F2 = 12 * ((exp(q[, 2]) * m[2])/(exp(q[, 1]) * m[1] + exp(q[, 2]) * m[2] +
> m[3])) - c(3,3))
> }
> m = c(0.1,0.2,0.7)
> 
> I am trying to solve for *q* and from based on the given m, I expect
> something like this;
> q <- matrix(c(-0.1335314,0.6931472,0.2719337,0.4054651), nrow=2)
> 
> How would I call the multiroot for the function f to hopefully get the
> above results. I thought of using newtonRaphson from package pracma but
> that possibly only handles univariate inputs.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list