Symbolic Toolbox enforcing rational numbers when I want decimals
18 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuesta aceptada
Walter Roberson
el 26 de Jun. de 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 comentarios
Eric
el 4 de Abr. de 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
el 4 de Abr. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Conversion Between Symbolic and Numeric en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!