sympref FloatingPointOutput adds roundoff error
2 次查看(过去 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!