Precision Error in logical evaluation?

조회 수: 2 (최근 30일)
Nathan
Nathan 2014년 5월 14일
편집: Star Strider 2014년 5월 14일
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에서 Function Creation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by