Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

brent returns spurious minimum #798

Closed
bjarthur opened this issue Mar 14, 2020 · 1 comment · Fixed by #799
Closed

brent returns spurious minimum #798

bjarthur opened this issue Mar 14, 2020 · 1 comment · Fixed by #799

Comments

@bjarthur
Copy link
Contributor

is the following example a limitation of brent's algorithm or a bug in the code? the function to be minimized is a step, so has wonky derivatives. would be nice if it gave a reasonable answer though.

in both cases below the function is -1 for negative input and 1 for positive input. in the first case, which returns 1 as the minimum (a wrong answer), the search interval is -1:2. in the second, which works, the interval is -2:2.

julia> optimize(x->x<0 ? -1 : 1, -1, 2)
Results of Optimization Algorithm
 * Algorithm: Brent's Method
 * Search Interval: [-1.000000, 2.000000]
 * Minimizer: 2.000000e+00
 * Minimum: 1.000000e+00
 * Iterations: 36
 * Convergence: max(|x - x_upper|, |x - x_lower|) <= 2*(1.5e-08*|x|+2.2e-16): true
 * Objective Function Calls: 37

julia> optimize(x->x<0 ? -1 : 1, -2, 2)
Results of Optimization Algorithm
 * Algorithm: Brent's Method
 * Search Interval: [-2.000000, 2.000000]
 * Minimizer: -1.055728e+00
 * Minimum: -1.000000e+00
 * Iterations: 37
 * Convergence: max(|x - x_upper|, |x - x_lower|) <= 2*(1.5e-08*|x|+2.2e-16): true
 * Objective Function Calls: 38

@bjarthur
Copy link
Contributor Author

for comparison, python gives a valid answer, albeit not within the range specified:

>>> from scipy import optimize
>>> def f(x):
...   return -1 if x<0 else 1
... 
>>> optimize.brent(f,brack=(-1,2))
-5.854101854603439
>>> optimize.brent(f,brack=(-2,2))
-8.472135811722177

but matlab does not:

>> f = @(x) sign(x)

f =

  function_handle with value:

    @(x)sign(x)

>> fminbnd(f,-1,2)

ans =

    2.0000

>> fminbnd(f,-2,2)

ans =

   -1.0557

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant