sympref FloatingPointOutput adds roundoff error
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Max Murtaugh
el 18 de Oct. de 2020
Respondida: Walter Roberson
el 29 de Nov. de 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.
1 comentario
Respuesta aceptada
Walter Roberson
el 29 de Nov. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Thermal Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!