[R] using optimize() correctly ...
Berend Hasselman
bhh at xs4all.nl
Sun May 24 07:52:18 CEST 2009
Esmail Bonakdarian-4 wrote:
>
> Hi,
>
> I am trying to use the optimize function to optimize a function. The
> results I am getting don't agree with what I compute on my own and
> when I look at the graph of
>
> f(x) = 100 + ((x-10)**2 + (x-10)) * cos(x-10), where -10 <= x <= 10
>
> in gnuplot.
>
> I suspect I am making a mistake in the usage of the R optimize
> function, perhaps someone could point out where?
>
>
> > f<-function(x) 100 + ((x-10)**2 + (x-10)) * cos(x-10)
>
> to MAXIMIZE:
>
> > result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=TRUE)
> > result
> $maximum
> [1] -2.728743
>
> $objective
> [1] 247.3284
>
> to MINIMIZE
>
> > result=optimize(f, c(-10,10), lower = -10, upper=10, maximum=FALSE)
> > result
> $minimum
> [1] 6.290112
>
> $objective
> [1] 91.52681
>
>
> However, I believe the correct values should be
>
> minimize:
> x = -5.838 val= -133.020
>
> maximize:
> x = -8.957 val= 438.448
>
>
Your function is not unimodal.
The help for optimize states:
"If f is not unimodal, then optimize() may approximate a local, but perhaps
non-global, minimum to the same accuracy."
In you case, optimize is locating a local maximum/minimum.
You should restrict the search to the interval [-10 , -7 ] so
result=optimize(f, c(-10,10), lower = -10, upper=-7, maximum=TRUE)
will locate your global maximum.
Berend
--
View this message in context: http://www.nabble.com/using-optimize%28%29-correctly-...-tp23689607p23690832.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list