How can I change symbolic displays to decimal displays?

How do I display a symbolic expression as a decimal?

댓글 수: 6

Please give examples. Do you mean like it's showing 1/4 and you want it to show 0.250000, or showing 5/2 and you want it so show 2.50000?
thats what im saying, I want decimal numbers for my answers. Thanks
you can try to add "format short" of "format long" to your code before the number you want to show. Hope that will help you!
"format short" and "format long" have no effect on symbolic numbers or symbolic expressions.
Hi, I have the same problem too. I try format short but the answer still fraction number.
Kantaphon, see my Answer below.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2013년 5월 21일
편집: MathWorks Support Team 2019년 5월 22일
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

Thanks, it works.. but before, I didnt have to put any command to have my decimal numbers, thats what i want. I don't know if the problem is that i have to change some toolbox or what.. but if you know how to do it please, I'll appreciate.
In most cases, the Symbolic Toolbox will convert formulae involving floating point numbers into their rational fractions.
When the Symbolic Toolbox is working with a rational formula, it will attempt to find a closed-form solution, but if it cannot do so then (if meaningful) it will attempt to find a fixed-point numeric solution instead.
When the Symbolic Toolbox recognizes that it is working with a fixed-point formula (rather than rational) then it will skip trying to find a closed-form solution and will proceed with trying to find a fixed-point numeric solution.
Because of the above behavior, two formulae that are the same except for a single decimal point in one of them, might be calculated different ways and can potentially arrive at very different answers; for example a formula with a complex term might have the term cancel out to 0 when calculated in full rational precision, but might have a large non-zero complex term when calculated in fixed-point.
For example, at the MuPAD command line,
int(x^2+1, x=0..10^10)
would proceed by closed-form, but if the "1" was replaced by "1." then it would do a numeric calculation.
Now, if you have a symbolic term that is in rational form and you add a constant, then if the constant is in rational form the addition would b done in rational form; if, however, the constant is in fixed-point form then the rational symbolic term will be evaluated down to a fixed-point number before the addition is done.
1 + RATIONALTERM gives a rational answer
1. + RATIONALTERM gives a fixed-point answer
Possibly your code path has changed by as little as one character in a constant.
Note that if your symbolic expression contains a symbolic variable, using double won't work.
syms x
y = sqrt(2)*x
double(y) % throws an error
In that case you could use vpa to approximate the symbolic expression.
syms x
y = sqrt(2)*x
vpa(y)
This works, but what if we are working on a matrix having some symbols too?
Then use vpa.
We updated Walter's answer to reflect some of the suggestions in the comments
madhan ravi
madhan ravi 2020년 6월 4일
편집: madhan ravi 2020년 6월 4일
Using vpa(...,digits(...)) also gives control to the decimals. It would be great if these informations are added in the documentation, took a while to find the workaround.
https://www.mathworks.com/help/symbolic/vpa.html#d120e189280 shows passing decimal digits into vpa() -- it is right there in the command summary.
Yes it is I meant the example with an illustration with a symbolic variable :)
Yes sir Walter, I meant something like
syms a
vpa(a*3.4647474,digits(3))
Why would you want to do that? digits() returns the previous number of digits in effect. For example if the number of digits in effect was 32, then digits(3) would return 32 and you would be doing vpa to 32 digits with the side effect of having switched to 3 digits for future calculations.
The documentation already gives the example,
Find π to 100 significant digits by specifying the second argument.
vpa(pi,100)
ans =
3.141592653589793238462643383279502884197169...
39937510582097494459230781640628620899862803...
4825342117068
It’s not my desire to do that obviously. When there is a multiple choice question , I am forced to do that. Because the answers in the multiple choice questions have only few decimals which is evaluated in another software. So using this option would give more flexibility to compare the results.
@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 2020년 6월 5일
편집: madhan ravi 2020년 6월 5일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Shashank Prasanna
Shashank Prasanna 2013년 5월 20일
>> format
Resets MATLAB display format to default.

댓글 수: 2

It's not a solution to this problem.
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
Andrew Gibbons 2021년 2월 16일

3 개 추천

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 2013년 5월 20일
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

Thanks, i've tried this but didn't work.
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.
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

댓글을 달려면 로그인하십시오.

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

"apparently you cannot easily see a variable number of digits on screen any more."
Of course you can:
n = sym(2)
n = 
2
x = sqrt(n)
x = 
vpa(x,40)
ans = 
1.41421356237309504880168872420969807857
However, your example calls SYM with the text 'pi', which you incorrectly think refers to the mathematical constant pi = 3.14159265359.... but in fact since version R2020a, calling SYM('pi') simply creates a symbolic variable using the greek letter π (exactly like it will also create variables using any other greek letter). This change is clearly documented:
"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."
The SYM() documentation shows many examples of how to define pi (the mathematical constant) symbolically. The examples are very easy to find for anyone who reads the documentation. The examples all define pi using this syntax (note: the input is the double pi, not text):
x = sym(pi)
x = 
π
vpa(x,40)
ans = 
3.141592653589793238462643383279502884197
Your approach using text input, CHAR() and EVAL() is very indirect, inefficient, relies on undocumented features, and is best avoided.
(By the way, MATLAB's symbolic package clearly documents the way it should be done. Try reading it.)
Of course? Don't assume what others see in their versions of Matlab. Mine is not even too old.
Look:
>> version
ans =
'9.9.0.1495850 (R2020b) Update 1'
>> n = sym(2)
n =
2
>> x = sqrt(n)
x =
1.4142
>> vpa(x, 40)
ans =
1.4142
So no, I cannot.
Also (your example):
>> version
ans =
'9.9.0.1495850 (R2020b) Update 1'
>> x = sym(pi)
x =
3.1416
>> vpa(x, 40)
ans =
3.1416
And btw, creating the symbolic variable for number pi with the syntax
x = sym(pi)
instead of
x = sym('pi')
is a terrible new idea (a new terrible idea?), because pi in Matlab is double(pi), so you are passing sym a double, and sym has to figure out that you mean the infinitely many digits of pi instead of the finite-precision approximation of pi. ¿When is the double near enough? ¿How do you create a symbolic variable for a finite-precision value of pi? Look (in a newer version, which I refuse to use in my other computers due to the terrible, terrible editor):
>> version
ans =
'9.13.0.2126072 (R2022b) Update 3'
>> format long g
>> pi
ans =
3.14159265358979
>> x = sym(pi)
x =
pi
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.14159265358979)
x =
3.1415926535897932384626433832795
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.1415926535898)
x =
3.1415926535897932384626433832795
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.1415926535897)
x =
3.1415926535896998572638949553948
>> vpa(x, 40)
ans =
3.14159265358969985726389495539478957653
Matlab should learn how things are done in Octave, for example.
You have in effect
sympref('FloatingPointOutput', true)
Thank you, @Walter Roberson. That helps to a little extent.
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).
"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')
y = 
vpa(y,40)
ans = 
3.141592653589793115997963468544185161591
fprintf('%.39f',pi) % for comparison
3.141592653589793115997963468544185161591
"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)
x = 
π
vpa(x,40)
ans = 
3.141592653589793238462643383279502884197

댓글을 달려면 로그인하십시오.

질문:

2013년 5월 20일

댓글:

2024년 6월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by