Azzera filtri
Azzera filtri

Precision Error in logical evaluation?

4 visualizzazioni (ultimi 30 giorni)
Nathan
Nathan il 14 Mag 2014
Modificato: Star Strider il 14 Mag 2014
In debugging some code I found the following results confusing. Anyone know how to further investigate this? Or do I simply need to avoid dividing a number by itself? I thought MatLab was "smart" enough to use 1 for the fraction of CF/CF. BTW I'm using R2014a student version on a mid 2009 Mac book pro with OSX 10.9.2
Here is the actual code that causes problems only some of the time as this is inside a function that is called 1000's of times but this error only happens a handful of them.
PL=fzero(@(y)(...
(s.State(6).H-s.State(4).H)*M_Dot/eta_pump...
-y*CVTModel_NV(RPM_Pump/RPM_Exp,y/CF)),[0.2*CF CF])/CF;
This is what I did at the command prompt in debugger mode at the line.
CVTModel_NV(RPM_Pump/RPM_Exp,0.2*CF/CF)
ans =
NaN
K>> CVTModel_NV(RPM_Pump/RPM_Exp,0.2)
ans =
0.828663580586898
K>> 0.2*CF/CF
ans =
0.200000000000000
K>> CVTModel_NV(RPM_Pump/RPM_Exp,ans)
ans =
NaN
K>> CF
CF =
2.669084425186238e+03
K>> CVTModel_NV(RPM_Pump/RPM_Exp,0.200000000000000000000)
ans =
0.828663580586898
At this point I commented this line out of the function CVTModel_NV:
Eff(SR>2|SR<0.5|PL<0.2|PL>1)=NaN; % to limit results to applicable area
And then got the following results:
K>> CVTModel_NV(RPM_Pump/RPM_Exp,0.2*CF/CF)
ans =
0.828663580586898
Then further testing reviled the problem.
K>> CF/CF
ans =
1
K>> 0.2*CF/CF<0.2
ans =
1
K>> 0.2*CF/CF==0.2
ans =
0

Risposta accettata

Nathan
Nathan il 14 Mag 2014
Adjusting the code to avoid the division repaired the problem. Goes to show that programing in MatLab while quite user friendly, it still requires some training in numerics....
PL=fzero(@(y)(...
(s.State(6).H-s.State(4).H)*M_Dot/eta_pump...
-y*CF*CVTModel_NV(RPM_Pump/RPM_Exp,y)),[0.2 1]);
  1 Commento
Star Strider
Star Strider il 14 Mag 2014
Modificato: Star Strider il 14 Mag 2014
Vectorizing them may also have solved it. If it only occurred a handful of times, those times could have been when the elements were vectors rather than scalars, and required element-wise operations.
You don’t tell us what the error was, so we’ll never know.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by