guRus! I have a function f = exp(x^2-y+(1/z)) Also, x can take values from 1 to 37, y from 2 to 20 and Z from -13 to 51. How can I find the maximum of f using any of the optimization functions please? Is there a way to store the possible values of x, y and Z in a single variable like in a List or in a multi-dimensional array? Thanks for your help Raghu [[alternative HTML version deleted]]
Hello, You don't need an optimzation routine to know that the maximum of this function is +Inf. It's attained at z = 0+ (when z converges to zero by positive values). This breaks the R function optim(), by the way. It " needs finite values of 'fn' " Hope this helps, Rui Barradas Em 08-07-2012 10:15, Raghuraman Ramachandran escreveu:> guRus! > > I have a function f = exp(x^2-y+(1/z)) > > Also, x can take values from 1 to 37, y from 2 to 20 and Z from -13 to 51. > > How can I find the maximum of f using any of the optimization functions > please? > > Is there a way to store the possible values of x, y and Z in a single > variable like in a List or in a multi-dimensional array? > > Thanks for your help > Raghu > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Hello,
There are several optimization functions in R. There's even a group
dedicated to the field. See the CRAN Task View: Optimization and
Mathematical Programming.
To use optim(), you could do something like
f <- function(x, y, z) (x^2 - y - z^2/2) # I've changed the function
negf <- function(x) -f(x[1], x[2], x[3])
opt1 <-optim(par = c(1, 2, 0), negf, gr = NULL, method =
"L-BFGS-B",
lower = c(1, 2, -13), upper = c(37, 20, 51))
opt2 <- optim(par = c(1, 2, -13), negf, gr = NULL, method =
"L-BFGS-B",
lower = c(1, 2, -13), upper = c(37, 20, 51))
# Not constrained
optim(par = c(1, 2, 0), negf, gr = NULL, method = "SANN")
Hope this helps,
Rui Barradas
Em 08-07-2012 11:25, Raghuraman Ramachandran escreveu:> Hi Rui
>
> Many thanks for your kind help. I agree in the given example it is +inf.
> But I wanted to know more generically that if I have a function with
> many variables each one taking a set of values then how can I use an
> Optim function? I will look into the help.
>
> Thanks once again.
>
> Raghu
>
> On Sun, Jul 8, 2012 at 11:19 AM, Rui Barradas <ruipbarradas at sapo.pt
> <mailto:ruipbarradas at sapo.pt>> wrote:
>
> Hello,
>
> You don't need an optimzation routine to know that the maximum of
> this function is +Inf. It's attained at z = 0+ (when z converges to
> zero by positive values).
>
> This breaks the R function optim(), by the way. It
>
> " needs finite values of 'fn' "
>
> Hope this helps,
>
> Rui Barradas
>
> Em 08-07-2012 10:15, Raghuraman Ramachandran escreveu:
>
> guRus!
>
> I have a function f = exp(x^2-y+(1/z))
>
> Also, x can take values from 1 to 37, y from 2 to 20 and Z from
> -13 to 51.
>
> How can I find the maximum of f using any of the optimization
> functions
> please?
>
> Is there a way to store the possible values of x, y and Z in a
> single
> variable like in a List or in a multi-dimensional array?
>
> Thanks for your help
> Raghu
>
> [[alternative HTML version deleted]]
>
> ________________________________________________
> R-help at r-project.org <mailto:R-help at r-project.org>
mailing list
> https://stat.ethz.ch/mailman/__listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
> http://www.R-project.org/__posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
This looks like a homework trap set up to catch those trying to use facilities like Rhelp. f = exp(x^2-y+z^(-1))= exp(x^2) * exp(1/z)/exp(y) To maximize clearly needs biggest x (37), smallest y (2) and a z that makes exp(1/z) big -- 0. Except that you'll get Inf etc. Actually, several of the optimization tools in R don't do too badly on this (about half of the routines in optplus (the R-forge version of optimx) get reasobable answers, but there are some oddities depending on starting values. JN On 07/08/2012 06:00 AM, r-help-request at r-project.org wrote:> Message: 64 > Date: Sun, 8 Jul 2012 10:15:34 +0100 > From: Raghuraman Ramachandran <optionsraghu at gmail.com> > To: R Project Help <r-help at r-project.org> > Subject: [R] Help in Optimization of a function > Message-ID: > <CADgEnDmRbxPoCHSDGghAeVUJxGcbZCAz6Bs99Ty_t=FfEsAXkw at mail.gmail.com> > Content-Type: text/plain > > guRus! > > I have a function f = exp(x^2-y+(1/z)) > > Also, x can take values from 1 to 37, y from 2 to 20 and Z from -13 to 51. > > How can I find the maximum of f using any of the optimization functions > please? > > Is there a way to store the possible values of x, y and Z in a single > variable like in a List or in a multi-dimensional array? > > Thanks for your help > Raghu > > [[alternative HTML version deleted]] > > > > ------------------------------