variable in if statement is undefined on some execution paths

조회 수: 1 (최근 30일)
ameen
ameen 2015년 1월 22일
답변: John D'Errico 2015년 1월 22일
function RA = fcn(density, V, L, Cb, Tf, Abt, B, T, hB, S)
if (Tf/L) <= 0.04
c4=Tf/L;
elseif Tf/L>0.04
c4=0.04;
end
c3 = 0.56*(Abt^1.5)/(B*T*(0.31*sqrt(Abt)+Tf-hB));
c2 = exp(-1.89*sqrt(c3));
CA=0.006*(L+100)^-0.16 - 0.00205+0.003*sqrt(L/7.5)*Cb^4*c2*(0.04-c4); RA = 0.5*density*V^2*S*CA;
Why i receive an error message about c4 is undefined on some execution paths ?? what is wrong with my If statement ??

채택된 답변

David Young
David Young 2015년 1월 22일
You can change
elseif Tf/L>0.04
to
else
since if the first test fails the second must succeed. That should remove the warning. It's also a little faster, since it avoids an unnecessary division and comparison.
Alternatively, you could write
c4 = min(0.04, Tf/L);
instead of the whole if-statement.
  댓글 수: 3
David Young
David Young 2015년 1월 22일
Same solution - you can replace the final elseif with else. The first elseif should have the test (B/L) >= 0.11 otherwise the case that B/L is exactly equal to 0.11 is not captured.
ameen
ameen 2015년 1월 22일
Thank you but why my original code gives this error ؟

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John D'Errico
John D'Errico 2015년 1월 22일
Of course, the simple answer is just to use
c4 = min(Tf./L,0.04);
No if statements are needed at all in this simple case, and the result is efficiently vectorized.
As for the message that c4 is undefined on some paths, I'd need to see more information. You have not provided the complete message, nor have you given us sufficient context to even be able to replicate that warning.
What is telling you this fact? Is it mlint? Is it in the command window? Is it an error message of some form? What version of MATLAB are you using?
Lacking any of that information, I might conjecture that in SOME version of MATLAB, that mlint tells you this fact, due to the lack of an else statement as the last clause in your ifs. mlint will not be intelligent enough to know that for real numbers a, that
if A <= 1
stuff
elseif a > 1
stuff
end
actually resolves all REAL cases for a. Whereas this simple form must resolve all cases:
if A <= 1
stuff
else
stuff
end

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by