# [R] optimize() stuck in local plateau ?

Mike Lawrence Mike.Lawrence at DAL.CA
Mon Oct 1 07:29:01 CEST 2007

```Hi all,

Consider the following function:

####
my.func = function(x){
y=ifelse(x>-.5,0,ifelse(x< -.8,abs(x)/2,abs(x)))
print(c(x,y)) #print what was tested and what the result is
return(y)
}
curve(my.func,from=-1,1)
####

When I attempt to find the maximum of this function, which should be
-.8, I find that optimize gets stuck in the plateau area and doesn't
bother testing the more interesting bits of the function:

####
optimize(my.func,interval=c(-1,1),maximum=TRUE)
####

I really don't understand why the search moves to the positive/
constant area of the function and neglects the more negative area of
the function. On step #4, after finding that there is no difference
between tests at -.23, .23 & .52, shouldn't the algorithm try -.52?
In fact, it seems to me that it would make sense to try -.52 on step
3, so that we've tested one negative, one positive (found no
difference), now one negative again. Thoughts?

Of course I could define my interval more reasonably for this
particular function, but this is in fact simply one of a class of
functions I'm exploring, none of which have known formal descriptions
as above (I'm exploring a large number of 'black boxes'). I do know
that the maximum must occur between -1 and 1 for all however. Please
advise on how I might use optimize more usefully.

Mike

--
Mike Lawrence
Graduate Student, Department of Psychology, Dalhousie University

Website: http://memetic.ca

Public calendar: http://icalx.com/public/informavore/Public

"The road to wisdom? Well, it's plain and simple to express:
Err and err and err again, but less and less and less."
- Piet Hein

```