The 'if' nested in my 'for loop' does not seem to be working, any idea why?

2 views (last 30 days)
I wanted a command to be executed when 'x_n' and 'y_n' meets a certain condition, but the nested 'if' statement does not seem to be working.
Any help would be appreciated.
(x and y are vectors and the 'for loop' is going through their values)
for n = 2:length(t)
clf
plot([target,(target+0.5)],[0,0],'b','Linewidth',3);
hold on
t_n = t(n);
x_n = x(n);
y_n = y(n);
if y_n == 0.25 && x_n>=target && x_n<=(target+0.5) %condition of x_n and y_n where the ball is supposed to land
ytest = y_n;
xtest = x_n;
break
% intended for the projectile to stop as if it landed on the board
end
plot(x_n,y_n,'ro','LineWidth',4,'MarkerSize',5)
ylim([-1,2.5])
xlim([0,3])
drawnow
end

Accepted Answer

Sajid Afaque
Sajid Afaque on 5 Apr 2021
Edited: Sajid Afaque on 5 Apr 2021
recheck your first condition
y_n == 0.25
check if y_n is exactly equal to 0.25 as expected.
0.249999999 is not equal to 0.25 when using '==' operator

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!