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

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

采纳的回答

Sajid Afaque
Sajid Afaque 2021-4-5
编辑:Sajid Afaque 2021-4-5
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by