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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by