Hello, optim searches for min (or max) of a function, but is it possible to solve for a specific value? I mean, I want to find the value of a and b that give the function value closest to ZERO (and not min or max) in the below. is it possible? thanks test=function(x){ a=x[1] b=x[2] if (all(x>0))(((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) else Inf } optim(c(1,2),test) -- View this message in context: http://www.nabble.com/Solving-equations-with-optim-tp16070263p16070263.html Sent from the R help mailing list archive at Nabble.com.
If you want to find the value of x such that f(x) = 0, then you can minimize f^2 or abs(f) using optim. Hope this helps, Ian francogrex wrote:> > Hello, optim searches for min (or max) of a function, but is it possible > to solve for a specific value? I mean, I want to find the value of a and b > that give the function value closest to ZERO (and not min or max) in the > below. is it possible? thanks > > test=function(x){ > a=x[1] > b=x[2] > if (all(x>0))(((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) > else Inf > } > optim(c(1,2),test) > >-- View this message in context: http://www.nabble.com/Solving-equations-with-optim-tp16070263p16070698.html Sent from the R help mailing list archive at Nabble.com.
There is one obvious way:> test <- function(x, value = 0) {a <- x[1] b <- x[2] ex <- if (all(x > 0)) (((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) else Inf (ex - value)^2 }> opt <- optim(c(1,2),test) > > opt[c("par", "value")]$par [1] 0.5141272 2.4810257 $value [1] 2.613923e-17> > originalTest <- function(x) {a <- x[1] b <- x[2] if (all(x > 0)) (((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) else Inf }> > with(opt, originalTest(par))[1] -5.112654e-09 Not bad. Bill Venables CSIRO Laboratories PO Box 120, Cleveland, 4163 AUSTRALIA Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304 Mobile: +61 4 8819 4402 Home Phone: +61 7 3286 7700 mailto:Bill.Venables at csiro.au http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of francogrex Sent: Sunday, 16 March 2008 3:24 AM To: r-help at r-project.org Subject: [R] Solving equations with optim Hello, optim searches for min (or max) of a function, but is it possible to solve for a specific value? I mean, I want to find the value of a and b that give the function value closest to ZERO (and not min or max) in the below. is it possible? thanks test=function(x){ a=x[1] b=x[2] if (all(x>0))(((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) else Inf } optim(c(1,2),test) -- View this message in context: http://www.nabble.com/Solving-equations-with-optim-tp16070263p16070263.h tml Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at 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.
On Sat, 15 Mar 2008, ianfiske wrote:> > If you want to find the value of x such that f(x) = 0, then you can minimize > f^2 or abs(f) using optim. Hope this helps,For all but the (default) Nelder-Mead method you will be better off with f^2 or some other differentiable function than abs(f), since e.g. BFGS gets its superior convergence results only for locally quadratic objective functions.> > Ian > > > > francogrex wrote: >> >> Hello, optim searches for min (or max) of a function, but is it possible >> to solve for a specific value? I mean, I want to find the value of a and b >> that give the function value closest to ZERO (and not min or max) in the >> below. is it possible? thanks >> >> test=function(x){ >> a=x[1] >> b=x[2] >> if (all(x>0))(((a/(a+b))*(beta(a,b)/(beta(a,b)-beta(a,b+6))))-0.35259) >> else Inf >> } >> optim(c(1,2),test) >> >> > > -- > View this message in context: http://www.nabble.com/Solving-equations-with-optim-tp16070263p16070698.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595