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

조회 수: 1 (최근 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