필터 지우기
필터 지우기

sympref FloatingPointOutput adds roundoff error

조회 수: 3 (최근 30일)
Max Murtaugh
Max Murtaugh 2020년 10월 18일
답변: Walter Roberson 2023년 11월 29일
I was trying to use symunit to keep track of units for my thermodynamics class and I set symbolic values to floating point output so it was easier to keep track of values. However, I found that it introduces significant round off error for some reason. I was led to believe that it would only affect the displayed values not the calculations but can't seem to figure out how to get the unrounded value back. I have the calculation where I realized this below.
clear
format short e
u = symunit;
baseUnits('SI');
sympref('FloatingPointOutput', false);
P = 100*u.kPa;
P_w = 3.1698*u.kPa;
w = 0.622*P_w/(P - P_w)
4929039/242075500
val_1 = double(separateUnits(w))
val_1 = 2.0362e-02
sympref('FloatingPointOutput', true);
w
w=0.0204
val_2 = double(separateUnits(w))
val_2 = 2.0400e-02
val_3 = double(w)
val_3 = 2.0400e-02
for reference here are the values without symunit (since the units cancel out anyway)
P = 100;
P_w = 3.1698;
w = 0.622*P_w/(P - P_w)
w = 2.0362e-02
How do I prevent this round-off error? Also, is there anyway to display symbolic results the same way as "format short g/e" where it gives 5 significant figures? 5 significant figures is often the minimum I need for my classes.

채택된 답변

Walter Roberson
Walter Roberson 2023년 11월 29일
That does not happen in current releases, so it must have beeen fixed between R2020b and R2023b
format short e
u = symunit;
baseUnits('SI');
sympref('FloatingPointOutput', false);
P = 100*u.kPa;
P_w = 3.1698*u.kPa;
w = 0.622*P_w/(P - P_w)
w = 
val_1 = double(separateUnits(w))
val_1 =
2.0362e-02
sympref('FloatingPointOutput', true);
w
w = 
0.0204
val_2 = double(separateUnits(w))
val_2 =
2.0362e-02
val_3 = double(w)
val_3 =
2.0362e-02
P = 100;
P_w = 3.1698;
w = 0.622*P_w/(P - P_w)
w =
2.0362e-02

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Stress and Strain에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by