sympref FloatingPointOutput adds roundoff error
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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)
val_1 = double(separateUnits(w))
sympref('FloatingPointOutput', true);
w
val_2 = double(separateUnits(w))
val_3 = double(w)
P = 100;
P_w = 3.1698;
w = 0.622*P_w/(P - P_w)
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Stress and Strain에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!