1

I'm having the following issue with my code. I've been trying to use some other posts that I found on line, like this one. But they didn't what I'm looking for.

My code uses a MATLAB Exchange function which optimize a numerical value that is important to be with 32 digits after the dot such as

0.59329669191989231613604260928696

The optimization function can be found here and it is called fminsearchbnd The optimization function calculate this and store the value in a variable that I use all over my code. In order not to perform the optimization everytime I want to store the variable (I tried either on a *.mat and on a label in the string form. But when I retrieve it, MATLAB transforms it in a double precision variable 'cutting' all the numbers after the 14th. However I need all of them because they are important!

Is it possible to read a number like that w/o using vpa() because with a symbolic value I can't do anything.

Any help is really appreciated. Thanks

EDIT: fminsearchbnd gives me this class(bb) -> double and when I want to see it on the workspace it is 0.586675392365899. But when I set formatSpec = '%.32f\n'; because I want to see all the numbers that the optimization gives me, typing set(editLabel,'String',num2str(bb,formatSpec))

Community
  • 1
  • 1
Nicholas
  • 1,915
  • 31
  • 55
  • Those final digits you claim to want are nothing but numerical garbage. What do you want to use them for? – Marc Claesen Jun 05 '13 at 08:24
  • I know that 32 digits are numerical garbage, but when I round it to 14 digits (double precision in matlab) my solution changes :( – Nicholas Jun 05 '13 at 09:16
  • It feels there's more going on here than meets the eye...feels like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you perhaps post a minimal working example of the whole optimization, including where you save/load the result? – Rody Oldenhuis Jun 05 '13 at 09:23
  • Have you scaled the parameters in your optimization? See [this](http://www.alglib.net/optimization/tipsandtricks.php#header1) and [this](http://www.alglib.net/optimization/scaling.php). – horchler Jun 05 '13 at 14:44
  • 1
    I *think* you stumbled upon a bug in `fminsearchbnd`. I've written a very similar function based on `fminsearchcon`, and one of the many initial bugs I encountered was that the results remained scaled/transformed to the internal algorithms wrapped around `fminsearch`. These functions work by applying transformations to your variables, *mimicking* the behavior of bounds/constraints. The results you find may simply not have been transformed back properly. Could you try to do the optimization again with [my tool](http://www.mathworks.com/matlabcentral/fileexchange/24298-optimize)? – Rody Oldenhuis Jun 05 '13 at 19:06
  • @RodyOldenhuis I'll try thank you very much. I'll let you know. – Nicholas Jun 07 '13 at 05:43

1 Answers1

2

You're trying to store/use a number that cannot be represented exactly in an IEEE754 64-bit double-precision floating point number.

I'm not sure how you got that number without using vpa() in the first place, since 64-bit double is Matlab's maximum of precision...

You could use the multiple precision toolbox by Ben Barrowes, or HPF by John d'Errico, also from the FEX. You'll have to convert/construct to/from string if you want to store/load it to/from file.

But I have to agree with John's comment there:

The fact is, most of the time, if you can't do it with a double, you are doing something wrong

so...why exactly are those 32-or-more digits important?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • actually they are not so important, because I'm looking for the optimum parameter for a numerical technique that I've implemented (well-known in literature). My main issue is that `fminsearch` which is inside `fminsearchbnd` finds a number with 32 digits. When I take the 'double' version of it, my solution is not optimized any more. So I can workaround that imposing to `fminsearch` to search a number with only 6 digits for example. But it seems to me that it is not possible, it didn't work even defining the TolX and TolFun options.. :( – Nicholas Jun 05 '13 at 09:15
  • @Nicholas: just for my understanding; can you post the result of `class(your_solution)`, where `your_solution` is the number returned by `fminsearchbnd`? – Rody Oldenhuis Jun 05 '13 at 09:22
  • @Nicholas: `fminsearch` only returns doubles and only accepts doubles for its bounds. Unless `fminsearchbnd` has completely hacked it I'm not sure what you mean. If you want variable precision bounded minimization with constraints on the bounds, then I think Mathematica may be an easier solution. Their [`NMinimize`](http://reference.wolfram.com/mathematica/ref/NMinimize.html) function supports Nelder-Mead and the other features of `fminsearchbnd`. – horchler Jun 05 '13 at 14:53