How good does double() convert symbolic result to numerical result?

2 views (last 30 days)
How precise does double() guarantee in conversion? I have this concern when I found my symbolic expression result has very "gigantic" structures, something like 1039459438477/994857584939, 3.95857675e8348575, log(340985093409503). And there are some addition and multiplication among these gigantic structures. In brief, the symbolic result grows very messy after some symbolic computation. Then if I use symfun() to convert this messy symbolic expression into symbolic function and then plug in some numbers, and use double() to convert it into numerical value, will the result be reliable?

Answers (2)

John D'Errico
John D'Errico on 1 Mar 2016
Of course, if the result is a number that will overflow or underflow a double, how could you expect it to be reliable? How could you even define "reliable" in that context? What meaning would it have?
Do the computations entirely in symbolic form, then only at the end should you convert it to double. vpa is a better tool here than double. If the number does indeed lie in the range of a double precision number, then you could use double at the very end.
HOWEVER, even if you do that, if it involves huge numbers as you describe, there would be no good reason to trust the results implicitly. Why would you trust such a computation, if you do not understand enough about the numerical analysis that you are asking this question here? And certainly there is no way that we can KNOW whether to tell you that you should trust such a computation.
Sorry, but unless we can see exactly what you are doing, we cannot possibly tell you that you can trust the result.

Walter Roberson
Walter Roberson on 1 Mar 2016
Edited: Walter Roberson on 2 Mar 2016
The accuracy can depend upon how the expressions are represented. For example, exp(A)*exp(B) might get evaluated in two parts that end up having to throw away useful information, instead of being calculated as if it were exp(A+B) . When A is close to -B then the difference in accuracy can be significant.
For these kinds of expressions, it is a good idea to use combine() to combine the powers before evaluating to numeric value.
  2 Comments
John D'Errico
John D'Errico on 1 Mar 2016
Edited: John D'Errico on 1 Mar 2016
But even there, A+B will exhibit massive subtractive cancellation. If the values are doubles, you can still be in trouble.
Walter Roberson
Walter Roberson on 2 Mar 2016
combine() will combine symbolically, so there will not be any massive subtraction calculation... though I guess you might perhaps have problems if Digits is not high enough during the processing.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!