variable in if statement is undefined on some execution paths

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

请先登录,再进行评论。

更多回答(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