Why are symbolic expressions being rounded?

조회 수: 2 (최근 30일)
Bill Tubbs
Bill Tubbs 2020년 9월 2일
답변: Bill Tubbs 2020년 9월 3일
I'm deriving some expressions symbolically and then substituting values and computing the results and I only recently noticed that the results are being rounded.
>> format long
>> syms x; double(subs(10/x,x,3))
ans =
3.333300000000000
or simply,
>> x = sym(3.33333333)
x =
3.3333
>> double(x)
ans =
3.333300000000000
I can't even reproduce results from the documentation it seems:
>> syms x
>> p = sym(pi);
>> piVpa = vpa(p)
piVpa =
3.1416
According to the documentation, this is supposed to produce:
piVpa =
3.1415926535897932384626433832795
  댓글 수: 1
Bill Tubbs
Bill Tubbs 2020년 9월 2일
편집: Bill Tubbs 2020년 9월 2일
Actually, I think it is double that is doing the rounding:
>> syms x; 10/3 - subs(10/x,x,3)
ans =
0
>> 10/3 - double(subs(10/x,x,3))
ans =
3.333333333355171e-05

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

채택된 답변

Bill Tubbs
Bill Tubbs 2020년 9월 3일
Mathworks technical support have diagnosed the problem. Don't know what caused it originally but the 'FloatingPointOutput' setting in the symbolic preferences was set to 1.
Here is the solution to fix it:
>> sympref
ans =
struct with fields:
FourierParameters: [1×2 sym]
HeavisideAtOrigin: [1×1 sym]
AbbreviateOutput: 1
TypesetOutput: 1
FloatingPointOutput: 1
PolynomialDisplayStyle: 'default'
MatrixWithSquareBrackets: 0
>> sympref('FloatingPointOutput','default');
>> double(sym('pi'))
ans =
3.141592653589793
>> p = sym(pi);
>> piVpa = vpa(p)
piVpa =
3.1415926535897932384626433832795

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 9월 2일
Have you changed your digits setting?
help digits
Are you calling the correct double function? What does this show?
which double(x)
  댓글 수: 1
Bill Tubbs
Bill Tubbs 2020년 9월 2일
Digits = 32
which double(x)
returns
/Applications/MATLAB_R2019b.app/toolbox/symbolic/symbolic/@sym/sym.m % sym method

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

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by