Symbolic Toolbox enforcing rational numbers when I want decimals

12 views (last 30 days)
I am using the Symbolic Toolbox to generate analytical derivatives for optimization problems. However commonly I encounter something like the following in my models:
syms m1
fun = 0.123123*m1
where MATLAB generates the following:
fun = (8871947150731801*m1)/72057594037927936
As I am writing the analytical derivative to an m-file programmatically, I cannot substitute a value into m1 as it is not known beforehand, and therefore I am left with a large rational number!
So my question, is there a way to force the Symbolic Toolbox to use decimal / floating point numbers? It will keep my derivative files shorter and easier to inspect.

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jun 2012
Numbers are automatically converted to rational unless you have specifically commanded something else. You do that using sym with a "flag". Flags apply only when numbers are being converted. In your case the syntax would be
syms m1
fun = sym(0.123123, 'd') * m1
Note: when a symbolic number is specified as decimal, then the current Digits applies to it, and it will combine with other constants in ways that use only the Digits. For example if you did happen to have a rational number in the expression that was being multiplied by the decimal number, then the rational would be combined with the decimal to form a single decimal number according to Digits -- losing the implicit precision of the rational expression.
Using decimal numbers instead of rational can make it difficult or impossible to find solutions to equations, or can introduce false answers that might seem to verify but are completely spurious. These problems can occur especially if one side of the equation is near zero but below it and the other side is near zero but above it: round-off can make the difference seem to be "close enough" to 0 to be accepted, when in fact an more detailed analysis might show that the two sides always have opposite sign and never reach or cross 0.
It is thus analytically better to work with rationals as long as possible, only converting for display presentation purposes (or, if one must, find a numeric solution to something that is not analytically tractable.) One can convert for presentation purposes using vpa() -- or, if the expression now involves only constants, use double() to convert to a double precision number.
  4 Comments
Eric
Eric on 4 Apr 2013
What about going the opposite way? I am solving an equation symbolically and the output is a rational number. I would like this to have this output as a decimal, how would I do that?
Walter Roberson
Walter Roberson on 4 Apr 2013
As I wrote at the end,
One can convert for presentation purposes using vpa() -- or, if the expression now involves only constants, use double() to convert to a double precision number.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!