[R] Regarding the optimize function

stat999 yumik091186 at gmail.com
Fri Sep 30 23:19:28 CEST 2011


 have a piecewise function which takes a constant value for x greater than
some point. 
It looks like this: 
y<-function(x,a){ 
  if (x<a+3) 
   { 
    y=(x-a)^2 
   }else{ 
    y=9 
   } 
  return(y) 
} 

In practice, I do not know where this point, a, starts. 

I would like to find a minimum of this function. 

So my naive approach is: 

upper<-100 
op<-optimize(y,interval=c(0,upper), a=5) 
(delta.y<-op$minimum) 

y.value<-rep(NA,1000) 
y.domain<-seq(0,upper,length.out=1000) 
for ( i in 1 : length(y.value)) 
   { 
    y.value[i]<-y(y.domain[i],a=5) 
   } 
plot(y.domain,y.value,type="l") 
abline(v=delta.y,col=2) 

However, in this way, I fail to find a point that takes minimum when the
interval argument of the optimize function is large.

I have to make the interval as small as c(0,20) to find the correct point
that corresponds to the minimum of the function. 

Could you explain why this is the case?? 

Thanks 
  

--
View this message in context: http://r.789695.n4.nabble.com/Regarding-the-optimize-function-tp3861257p3861257.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list