Filter löschen
Filter löschen

Plotting trajectory in 2D

32 Ansichten (letzte 30 Tage)
Rildo
Rildo am 20 Okt. 2014
Bearbeitet: Walter Roberson am 31 Okt. 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by