Could anyone tell me why this keeps getting stuck in the first 'if' statement?

1 view (last 30 days)
function [outcome,val] = Q2_13009636(v,theta,R,H)
%gravity
g = -9.81;
outcome=4;
while outcome==4;
s = (R*tan(theta))* - ((g*R^2)/(2*v^2*(cos(theta))^2));
if s>H;
outcome=2;
val=s-H;
end
if H>=s;
if s>0;
outcome=1;
val=s;
end
end
if s<=0;
outcome=0;
val=0;
end
end
end %function end

Accepted Answer

Star Strider
Star Strider on 27 Jan 2016
Are ‘s’ or ‘H’ a vector?
Vector logic usually require the any or all functions to provide ‘correct’ comparisons.
  6 Comments
rlcal94
rlcal94 on 28 Jan 2016
You sir are a lifesaver, 100% on the dot. I bracketed it out wrong. Cannot believe I missed that.
Star Strider
Star Strider on 28 Jan 2016
My pleasure.
Believe me, it happens to us all! (Well, at least it’s happened to me.)

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Jan 2016
Your function uses "* - " in succession, so you are multiplying by the negative of something. That something has only positive components except for g, which is negative. negative times negative gives a positive, so the overall result will be positive unless tan(theta) is negative. And the value will probably be fairly large.
It appears to me that you wanted to multiply by something and subtract something else where the something else is expected to be positive.

Community Treasure Hunt

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

Start Hunting!