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
Daniel Pollard
Daniel Pollard 2021 年 1 月 8 日
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.
Ryan Fedeli
Ryan Fedeli 2021 年 1 月 8 日
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)
Anon
Anon 2021 年 1 月 8 日
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
Anon
Anon 2021 年 1 月 8 日
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 ExchangeLoops 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