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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Sajid Afaque
Sajid Afaque am 5 Apr. 2021
Bearbeitet: Sajid Afaque am 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by