Effacer les filtres
Effacer les filtres

Plotting trajectory in 2D

32 vues (au cours des 30 derniers jours)
Rildo
Rildo le 20 Oct 2014
How come this code is not resulting in a plot of the trajectory at varying angles?
function [x , y] = trajectory(Vo,O,t,g)
% Function for varying angle
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
g = 9.8;
Vo = 10;
t = (0:1:5);
for O = (0: (pi/8): pi);
[x,y] = trajectory(Vo,O,t,g);
end
plot (x,y);
hold on
end

Réponse acceptée

Nick
Nick le 20 Oct 2014
Inside your function you are calling your function.You are also redefining the parameters you are sending to your function Vo, t inside the function
There might be more efficient ways but one way you could do this is take the for loop out of the function and your function would compute and plot x and y every call
function [x , y] = trajectory(Vo,O,t,g)
% Function for varying angle
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
plot (x,y);
hold on
end
then in order to call this function use
for O = (0: (pi/8): pi);
[x,y] = trajectory(10,O,[0:1:5],9.8);
end
if you want to keep that loop inside the function and just have a function call
function [x , y] = trajectory(Vo,t,g)
% Function for varying angle
for O = (0: (pi/8): pi);
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
plot (x,y);
hold on
end
end
and to call it would be
[x,y] = trajectory(10,[0:1:5],9.8);
  2 commentaires
Rildo
Rildo le 20 Oct 2014
Great answer, thanks.
Phila Siffundza
Phila Siffundza le 31 Oct 2017
thank you, that really helped

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by