1

I am trying to evaluate and find the minimum and maximum values of a function over a certain interval. I also want it to evaluate the endpoints to see if they are the maximum or the minimum values. I have the following code which is not giving me what I want. The minimum values should be -1 and 2 but I am getting -0.9999 and 1.9999. Any help would be much appreciated.

minVal1 = fminbnd(f,-1,0);  
minVal2 = fminbnd(f,0,2);
Floris
  • 45,857
  • 6
  • 70
  • 122
  • Looks like a problem with floating point differences. See: http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples – darthbith May 04 '14 at 19:41
  • The values you get are `-1` and `2` to within the precision of your algorithm. You need to set the precision to be `0.00001` or so if you want the number to "look like" the number you expect. – Floris May 04 '14 at 19:42
  • @Floris Is there no way to manipulate the code to give me what I want instead of messing with the precision? – user3598116 May 04 '14 at 19:43
  • `fminbnd(f, -1, 0, optimset('TolFun', 1e-5));` might be all you need (play with the value…) – Floris May 04 '14 at 19:44
  • @Floris What is "Xtol"? Matlab is not recognizing it. – user3598116 May 04 '14 at 19:47
  • Sorry - try `TolFun`. - tolerance on function evaluation ("when function changes by less than x, stop. You have reached a stationary point") . I should have said `TolX` in my original version, but `TolFun` goes straight to the heart of the problem. – Floris May 04 '14 at 19:48

1 Answers1

0

I believe that your problem lies in the fact that the default of TolFun for Matlab's fminbnd` function is 0.0001 - so when the function evaluation changes by less than that number, it stops. This may lead to stopping before reaching the true maximum.

If you want to be "right to within 0.0001", you need to set the tolerance on the function evaluation. You could use for example

minVal1 = fminbnd(f, -1, 0, optimset('TolFun', 1e-5));

That ought to get you the precision you need. Make the tolerance even smaller if you need greater precision (a the expense of computation time). See more details on how to fine tune these parameters on the Matlab website

Floris
  • 45,857
  • 6
  • 70
  • 122