Precision Error in logical evaluation?

3 次查看(过去 30 天)
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

采纳的回答

Nathan
Nathan 2014-5-14
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 个评论
Star Strider
Star Strider 2014-5-14
编辑:Star Strider 2014-5-14
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by