Using multiroot for root solution for a matrix based function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Using multiroot for root solution for a matrix based function

Muhmd
 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Using multiroot for root solution for a matrix based function

Berend Hasselman

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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.