Plotting trajectory in 2D

61 visualizaciones (últimos 30 días)
Rildo
Rildo el 20 de Oct. de 2014
Editada: Walter Roberson el 31 de Oct. de 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

Respuesta aceptada

Nick
Nick el 20 de Oct. de 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 comentarios
Rildo
Rildo el 20 de Oct. de 2014
Great answer, thanks.
Phila Siffundza
Phila Siffundza el 31 de Oct. de 2017
thank you, that really helped

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by