Berend Hasselman wrote: > > > B. Jonathan B. Jonathan wrote: >> >> Hi there, I have following equations to be solved for a and b: >> >> a/(a+b) = x1 >> ab/((a+b)^2 (a+b+1)) = x2 >> >> Is there any direct function available to solve them without >> disentangling them manually? Thanks for your help. >> > > There is a package nleqslv that will solve a system of equations with > either a Newton or Broyden method. > > You can do this > > library(nleqslv) > > # trial values > > x1 <- .6 > x2 <- .2 > > eqn <- function(par) { > a <- par[1] > b <- par[2] > f <- numeric(2) > > f[1] <- a/(a+b) - x1 > f[2] <- a*b/((a+b)^2 * (a+b+1)) - x2 > > f > } > > pstart <- c(1,1) > > nleqslv(pstart,eqn,control=list(trace=1)) > > #or this if you don't need the trace > > nleqslv(pstart,eqn) > > There is also a package BB that uses different methods and is especially > geared towards very large systems. > > Berend >
You can also pass values for x1 and x2 to your function like this (you can replace the NA's with your default values). eqn <- function(par,x1=NA,x2=NA) { a <- par[1] b <- par[2] f <- numeric(2) f[1] <- a/(a+b) - x1 f[2] <- a*b/((a+b)^2 * (a+b+1)) - x2 f } pstart <- c(1,1) nleqslv(pstart,eqn, x1=.6, x2=.2) Berend -- View this message in context: http://r.789695.n4.nabble.com/Solving-a-equation-tp3743338p3743461.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list 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.