[R] Optimize function in R: unable to find maximum of logistic function

Rolf Turner r.turner at auckland.ac.nz
Mon Oct 28 22:01:16 CET 2013


This could be described as a bug, perhaps.  Or it could be described as
an indication that numerical optimization is inevitably tricky. Notice that
if you narrow down your search interval from [0,5] to [0,0.5] you get the
right answer:

 > optimize(f, c(0, 0.5), maximum= TRUE,tol=1e-10)
$maximum
[1] 4.192436e-11

$objective
[1] 1

I guess there's a problem with finding a gradient that is effectively
(numerically) zero when "k" is equal to 5.


     cheers,

     Rolf Turner


On 10/29/13 06:00, Shantanu MULLICK wrote:
> Hello Everyone,
>
> I want to perform a 1-D optimization by using the optimize() function. I
> want to find the maximum value of a "logistic" function. The optimize()
> function gives the wrong result.
>
> My code:
> f= function (k) {
> T_s = 20
> result = (2- 2/(1+ exp(-2*T_s*k)))
> return(result)
> }
> optimize(f, c(0, 5), tol = 0.0000000000001, maximum= TRUE)
>
> The maximum value for the function happens at k=0, and the maximum value is
> 1. Yet  the optimise function, says that the maximum value happens at k=
> 4.9995, and the maximum value is 0.
>



More information about the R-help mailing list