Azzera filtri
Azzera filtri

Plotting trajectory in 2D

32 visualizzazioni (ultimi 30 giorni)
Rildo
Rildo il 20 Ott 2014
Modificato: Walter Roberson il 31 Ott 2017
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

Risposta accettata

Nick
Nick il 20 Ott 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 Commenti
Rildo
Rildo il 20 Ott 2014
Great answer, thanks.
Phila Siffundza
Phila Siffundza il 31 Ott 2017
thank you, that really helped

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by