What is wrong with my if statement?

Hello all,
I'm not sure how to phrase my if statement so that when the value of y at the point where x is equal to dto, the statement will run.
Currently it stops at the beginning of the if statement so i know that thats where the problem lies.
Below is my code for the relevant parts:
if max(x)>=Ox %&& (y(x==max(x))<=Oy)
dto = find(x < Ox,1,'last')
tto = dto/v0x
if y(x==dto)<=Oy %%this is where it stops
xlim([0, Ox+Ot+0.5])
ylim([0, max(y)+0.5])
hold on
for i=1:length(tto)
plot(x(i),y(i),'ko')
pause(0.05)
end
elseif y(x==dto)>Oy
for i=1:length(tFinal)
plot(x(i),y(i),'ko')
pause(0.05)
end
end
end
Thanks in advance! :3

댓글 수: 9

Diaa
Diaa 2021년 1월 8일
Can you put some values of x, y, ox, ot and oy that reproduce your problem?
Ryan Fedeli
Ryan Fedeli 2021년 1월 8일
^ This would be helpful.
Also I want to add that since you're using the find function, dto is defined as an index, so it's an integer. If x does not contain integers, this would be one reason why it never runs
Anon
Anon 2021년 1월 8일
편집: Anon 2021년 1월 8일
so the x:
x = linspace(0,11.8817,100)
this was calculated using initial speed and time of flight
the y:
y0 = 2
v0y = 5.7358
t = linspace(0,1.4505,100) %these values are used to calculate the y value below:
g = -9.81
y = y0 + v0y.*t+0.5*g*t.^2 %this is the y value plotted
Ox = 7
Oy = 5
Ot = 0.5
Anon
Anon 2021년 1월 8일
ah okay i didnt know about the find function only working for integers - what function allows me to find any value in an array
You've used logical indexing to get y where x=dto:
if y(x==dto)<=Oy %%this is where it stops
x is an array of values from 0 to 11.8817, and when I print dto, it returns 59, so the if statement isn't satisfied. So it goes to the elseif, where you say
for i=1:length(tFinal)
where, at least in the code you've given us, tFinal isn't defined. So it does nothing.
Well the find function works for any type of array. It returns the index (an integer number) of the desired value. So in the array:
p = [2.0 4.2 6.0 8.4 10.0];
index = find(p==6.0)
find will return 3. 3 is the place in the array p where p = 6.0.
So to find the value at the index given by find, all you need is
value = p(index)
the tFinal value is the last number in:
t = linspace(0,1.4505,100)
so it would be 1.4505s for tFinal
Anon
Anon 2021년 1월 8일
Okay that make sense Ryan thank you! I'll try to rewrite that bit
I have changed the value for the dto function thank you Ryan so now i ca get past the If statement howvever i only have two points plotted with this for loop:
for i=1:length(tto)
plot(x(i),y(i),'ko')
pause(0.05)
disp('here now')
end

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 1월 8일

댓글:

2021년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by