How can I change symbolic displays to decimal displays?
1.232 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Maria Coronell
el 20 de Mayo de 2013
Comentada: Stephen23
el 23 de Jun. de 2024
How do I display a symbolic expression as a decimal?
6 comentarios
Kantaphon Meechart
el 21 de Abr. de 2017
Hi, I have the same problem too. I try format short but the answer still fraction number.
Respuesta aceptada
Walter Roberson
el 21 de Mayo de 2013
Editada: MathWorks Support Team
el 22 de Mayo de 2019
There are several ways to approximate a number or change the display in Symbolic Math Toolbox.
To approximate a numeric value in double precision, you can use the “double” function. For example:
val = double(sqrt(2))
1.4142
If your expression uses a symbolic variable, you can use the “vpa” function. “vpa” gives a numerical approximation to the value (32 digits by default). For example:
syms x
val = vpa(sqrt(2)*x)
1.4142135623730950488016887242097*x
Starting in R2019a, if you want the equivalent of format short in Symbolic Math Toolbox, you can use “sympref”. For example:
sympref('FloatingPointOutput',true)
syms x
val = sqrt(2)*x
1.4142*x
Setting this preference will display any number in fixed-decimal format with four digits after the decimal point. This preference does not approximate any symbolic number into floating-point precision, and hence you can still perform the exact symbolic computation. Note that setting this preference carries over successive MATLAB sessions, so you need to call “sympref('default')” to restore the original setting.
15 comentarios
Stephen23
el 5 de Jun. de 2020
Editada: Stephen23
el 5 de Jun. de 2020
@madhan ravi: your syntax is obfuscated and misleads the reader of that code. As the digits documentation explains, when digits is called with an output argument "d1 = digits(d) sets the new precision d and returns the old precision in d1". So your code is equivalent to this:
d_old = digits(d_new);
vpa(...,d_old)
which does not seem to be a particularly useful syntax: if you need to use the old number of digits (as your code does), then just not calling digits at all would be simpler and easier to understand.
Why not just this?:
vpa(...)
...
digits(...) % if required
"When there is a multiple choice question..."
I cannot find any obviously relevant reference to a particulay syntax or method in the MATLAB documentation with the term "multiple choice": how is this related to MATLAB or the symbolic toolbox?
madhan ravi
el 5 de Jun. de 2020
Editada: madhan ravi
el 5 de Jun. de 2020
Ah yes Stephen now it’s clear what sir Walter was trying to say. I don’t know maybe I missed something in a hurry. When I had a long equation the result didn’t change when the equation were more complex with symbolic variables. Ah now I have seen your edited comment with the digits(...) after , will experiment with it , thanks!
Más respuestas (4)
Shashank Prasanna
el 20 de Mayo de 2013
>> format
Resets MATLAB display format to default.
2 comentarios
Steven Lord
el 13 de Sept. de 2017
That will help if you want to change how numeric data is displayed. But it will not change how symbolic expressions are displayed. Walter's answer (with the additional point I added) is a solution and I have just marked it Accepted.
Andrew Gibbons
el 16 de Feb. de 2021
Did you try vpa? vpa does a better job of displaying easily readable answers for problems that involve matrices (not 1x1 scalars).
vpa(S,4), where S is what you were trying to solve for (in your case the reynold's or nusselt number) so you would put something like vpa(Nu2,4) to get 4 digits of precision. I hope that helps.
0 comentarios
Youssef Khmou
el 20 de Mayo de 2013
hi, Maria,
If you want to display decimal ( floating point) numbers try :
>>format long % or format short
If you want fractional display try :
>>format rat
and try :
>>doc format
3 comentarios
Walter Roberson
el 20 de Mayo de 2013
If you just enter a number at the command line, how does it come out? For example if you entered
2.345
then does it come out in decimal or does it come out in fraction form?
I am trying to determine here whether you MATLAB is set to display fractions automatically or if instead the program is designed to display as fractions deliberately.
Felipe Jiménez Hernández
el 22 de Jun. de 2024
Recent versions of Matlab's symbolic toolbox seem to have changed this behavior for the worse, at least in my machines (I'm using 9.9.0.1495850 (R2020b) Update 1 in Windows, don't want to update because the new editor is pure sh*t).
Anyway, apparently you cannot easily see a variable number of digits on screen any more.
I have found a workaround that works up to 16 or 17 digits (but no more, because it relies on conversion to a double). If x is a sym, you can do this:
num2str(eval(char(x))), 16)
This works the same if x=sym('pi') or x=vpa('pi',40). Look:
>> format long g
>> x = sym('pi')
x =
pi
>> vpa(x, 40) % expected to see 40 digits, but no
ans =
pi
>> num2str(x, 40) % num2str does not work on sym variables
Error using num2str (line 47)
Input to num2str must be numeric.
>> num2str(eval(char(x)), 40) % digits from 17 going are wrong for pi (they are of double(pi))
ans =
'3.141592653589793115997963468544185161591'
>> num2str(eval(char(x)), 16) % these are correct for true pi
ans =
'3.141592653589793'
>> x = vpa('pi', 40) % vpa won't fix anything
x =
pi
>> num2str(x, 40) % a vpa number is still a sym
Error using num2str (line 47)
Input to num2str must be numeric.
>> num2str(eval(char(x)), 40) % still wrong from digit 17 going
ans =
'3.141592653589793115997963468544185161591'
If someone knows or finds a way for Matlab to show 40 digits of true pi on screen (whatever precision it uses internally), please say, I will try it.
(By the way, GNU Octave's symbolic package does this the way it should be done. Try it.)
5 comentarios
Felipe Jiménez Hernández
el 23 de Jun. de 2024
This is the behavior in my machine, in case you care:
>> ver symbolic
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.9.0.1495850 (R2020b) Update 1
MATLAB License Number: 40825541
Operating System: Microsoft Windows 11 Home Version 10.0 (Build 22631)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
Symbolic Math Toolbox Version 8.6 (R2020b)
>> format long % just in case
>> sympref('FloatingPointOutput', false);
>> x = sym(pi)
x =
pi
>> x - pi
ans =
0
>> x = sym('pi')
x =
pi
>> x - pi
ans =
pi - pi
>> sympref('FloatingPointOutput', true);
>> x = sym(pi)
x =
3.1416
>> x - pi
ans =
0
>> x = sym('pi')
x =
pi
>> x - pi
ans =
pi - 3.1416
The option does make show some digits on screen, but I cannot see as many as doing my discouraged workaround:
num2str(eval(char(x)), 17)
If there is something less discouraged to show more digits, I will gladly use it, especially if it can show more than 17 digits of true pi. In any case, I understand you prolly don't have my version of the symbolic toolbox to do any tests. (On another front, what would be great would be the option to reverse to the old Matlab editor instead of the new s*tty one --I would update my symbolic toolbox.)
And I also don't know how to define a sym variable for the exact value of pi as opposed to a sym variable representing a numerical value of pi with many correct digits. I just preferred sym('pi') to generate the former rather than a valueless sym variable named pi (pun intended).
Stephen23
el 23 de Jun. de 2024
"How do you create a symbolic variable for a finite-precision value of pi?"
The SYM() documentation states that you can use the 'f' (for floating point) flag:
sympref('FloatingPointOutput', false);
y = sym(pi,'f')
vpa(y,40)
fprintf('%.39f',pi) % for comparison
"If there is something less discouraged to show more digits, I will gladly use it, especially if it can show more than 17 digits of true pi.... And I also don't know how to define a sym variable for the exact value of pi as opposed to a sym variable representing a numerical value of pi with many correct digits."
The SYM() documentation states that you can use the default 'r' flag, exactly as I showed in my last comment:
x = sym(pi)
vpa(x,40)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!