Please read the help for optimize() carefully. The following excerpted from there should help explain your problem:
"The first evaluation of f is always at x_1 = a + (1-phi)(b-a) where (a,b) = (lower, upper) and phi = (sqrt 5 - 1)/2 = 0.61803.. is the golden section ratio. Almost always, the second evaluation is at x_2 = a + phi(b-a). Note that a local minimum inside [x_1,x_2] will be found as solution, even when f is constant in there, see the last example." In your case, > x_1 = a + (1-phi)*(b-a) > x_1 [1] -0.236068 > x_2 = a + phi*(b-a) > x_2 [1] 0.236068 > Since your function is constant in (x_1, x_2), you get your solution in that interval. Try a different interval, and you'll get your answer: > optimize(my.func,interval=c(-1,0),maximum=TRUE) [1] -0.618034 0.618034 [1] -0.381966 0.000000 [1] -0.763932 0.763932 [1] -0.8090170 0.4045085 [1] -0.7016261 0.7016261 [1] -0.7401333 0.7401333 [1] -0.781153 0.781153 [1] -0.791796 0.791796 [1] -0.7983739 0.7983739 [1] -0.8024392 0.4012196 [1] -0.7958614 0.7958614 [1] -0.7999267 0.7999267 [1] -0.8008864 0.4004432 [1] -0.7993336 0.7993336 [1] -0.8002933 0.4001466 [1] -0.7997001 0.7997001 [1] -0.8000667 0.4000334 [1] -0.7998402 0.7998402 [1] -0.7999802 0.7999802 [1] -0.8000209 0.4000104 [1] -0.7999802 0.7999802 $maximum [1] -0.7999802 $objective [1] 0.7999802 Best, Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: [EMAIL PROTECTED] Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Lawrence Sent: Monday, October 01, 2007 1:29 AM To: Rhelp Subject: [R] optimize() stuck in local plateau ? 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@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. ______________________________________________ R-help@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.