[R] using optimize() correctly ...

Esmail esmail.js at gmail.com
Sun May 24 02:34:11 CEST 2009


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

Thanks,
Esmail


This is w/ R version 2.8.1 (2008-12-22) under Ubuntu 9.04



More information about the R-help mailing list