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

Patrick Burns pburns at pburns.seanet.com
Mon Oct 1 10:56:48 CEST 2007

```Given a univariate problem where the maximum
must be between -1 and 1, I would test with a grid
of points, then refine that if necessary.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Mike Lawrence wrote:

>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
>
>______________________________________________
>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.
>
>
>
>

```

More information about the R-help mailing list