What is wrong with my for loop
Mostrar comentarios más antiguos
Hello all,
so im trying to plot a projectile and this is a condition where the projectile hits a wall and then stops in that place
this is the code that i have currently:
if Ox == 0 && Oy == 0
xlim([0, max(x)+0.5])
ylim([0, max(y)+0.5])%if the obstacle condition is not on (values are 0) then this condition runs
axis('equal')
hold('on')
grid on %ensures following commands are added to the same plot
plot([0 max(x)],0)
comet(x,y,0.1) %plots the projectile as a comet
plot(x(y==max(y)),y(y==max(y)),'ro') %displays the point of max height
text(x(y==max(y)),y(y==max(y)),'\leftarrow MAX HEIGHT') %pointer to the max height
else %if the values for the obstacle are not 0 then this condition will run
rectangle('Position',[Ox 0 Ot Oy],'FaceColor',[0.6353 0.0784 0.1843]) %obstacle plot
hold on %ensures following commands are added to the same plot
axis equal
grid on
disp('im here')
if max(x)>=Ox
to = Ox/v0x %time until reaches obstacle
disp('im working btw')
xlim([0, Ox+Ot+0.5])
ylim([0, max(y)+0.5])
hold on
for i=0:length(to)
plot(x(i+1),y(i+1),'ko')
pause(0.1)
disp('here now')
end
%else
%comet(x,y,0.1) %plots the projectile as a comet animation
%plot(x(y==max(y)),y(y==max(y)),'ro') %displays the point of max height
%text(x(y==max(y)),y(y==max(y)),'\leftarrow MAX HEIGHT') %pointer to the max height
end
end
this is what the graph looks like for values v0=10 theta0=35 y0=2 Ox=7 Oy=5 Ot=0.5
why do i only have 2 points plotted? how can i get the full projectile?

4 comentarios
Rik
el 6 de En. de 2021
I may be missing something, but where is the code that ensure you plot multiple points? I don't see a loop, and none of the function I recognize as resulting in multiple points.
Also, comparing to 0 might give rise to rounding errors (just like (1/3)*3 could return 0.999 if you work out every step seperately in a decimal system). You should instead compare the absolute value to eps.
if SomeValue==0 %might not catch rounding errors
if abs(SomeValue)<2*eps %will catch rounding errors
Anon
el 6 de En. de 2021
Rik
el 6 de En. de 2021
Where do you define x and y? Are they scalars? Are you using this code in a loop?
Anon
el 6 de En. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!