variable in if statement is undefined on some execution paths

1 view (last 30 days)
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 ??

Accepted Answer

David Young
David Young on 22 Jan 2015
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 Comments
David Young
David Young on 22 Jan 2015
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 on 22 Jan 2015
Thank you but why my original code gives this error ؟

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 22 Jan 2015
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

Community Treasure Hunt

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

Start Hunting!