Plotting trajectory in 2D

32 次查看(过去 30 天)
Rildo
Rildo 2014-10-20
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

采纳的回答

Nick
Nick 2014-10-20
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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by