How can I change symbolic displays to decimal displays?

1,640 views (last 30 days)
How do I display a symbolic expression as a decimal?
  6 Comments
Kantaphon Meechart
Kantaphon Meechart on 21 Apr 2017
Hi, I have the same problem too. I try format short but the answer still fraction number.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 May 2013
Edited: MathWorks Support Team on 22 May 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 Comments
Stephen23
Stephen23 on 5 Jun 2020
Edited: Stephen23 on 5 Jun 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
madhan ravi on 5 Jun 2020
Edited: madhan ravi on 5 Jun 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!

Sign in to comment.

More Answers (3)

Shashank Prasanna
Shashank Prasanna on 20 May 2013
>> format
Resets MATLAB display format to default.
  2 Comments
Steven Lord
Steven Lord on 13 Sep 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.

Sign in to comment.


Andrew Gibbons
Andrew Gibbons on 16 Feb 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.

Youssef  Khmou
Youssef Khmou on 20 May 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 Comments
Walter Roberson
Walter Roberson on 20 May 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.
Maria Coronell
Maria Coronell on 21 May 2013
Yes, it show 2.345.. the thing is, that some values are showing as fraction and other values just in decimal form. I dont understand what happened! Look at this!
Re1 =
7.489184809171960e+004
Nu1 =
3.473689192970496e+002
h1 =
1.047513915597188e+004
Re2 =
1422543653887999360776996931534847612609041143314926486102736896/18415266480134414561195254170806684536779318144366885369925
Nu2 =
(70353137442027663*18415266480134414561195254170806684536779318144366885369925^(9/20)*1422543653887999360776996931534847612609041143314926486102736896^(11/20))/2073374681446543372437154885376994391455329959260590683959033514499768320000

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!