sympref FloatingPointOutput adds roundoff error
1 view (last 30 days)
Show older comments
Max Murtaugh
on 18 Oct 2020
Answered: Walter Roberson
on 29 Nov 2023
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.
Accepted Answer
Walter Roberson
on 29 Nov 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Stress and Strain in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!