My Program Is Ignoring my ELSE statement...How To Fix...Projectile Motion
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Taylor McMillan
el 31 de Mayo de 2018
Respondida: Walter Roberson
el 31 de Mayo de 2018
I AM WANTING TO STOP PLOTTING ONCE Y < 0.
%{
This function was created by Taylor McMillan.
PURPOSE: Model simple projectile motion. Introduce students to Matlab.
ENTER THE FOLLOWING INTO THE COMMAND WINDOW:
ProjectileMotion(0.1, 0, 0, 50, pi/4, 30)
%}
%%START PROGRAM %%
function ProjectileMotion(time_step, xic, yic, vic, angic, max_time)
%%Create A Time Series %%
h = time_step;
N = max_time/h;
t = 0:h:max_time - h;
%%Initial Conditions / Parameters %%
x(1) = xic; % Initial x-position (meters)
y(1) = yic; % Initial y-position (meters)
v(1) = vic; % Initial speed (meters/second)
ang(1) = angic; % Initial launch angle (radians)
g = 9.81; % Acceleration due to gravity (meters/second/second)
%%Projectile Equations %%
if y >=0 && x >= 0
x = x(1) + v(1)*cos(ang(1))*t;
y = y(1) + v(1)*sin(ang(1))*t - 0.5*g*t.^2;
%%Plot The Motion %%
else
figure();
plot(x, y)
xlabel('x - position (m)', 'FontSize', 20)
ylabel('y - position (m)', 'FontSize', 20)
title('Trajectory', 'FontSize', 26)
end
END PROGRAM %%
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 31 de Mayo de 2018
Your initial x and y are 0 . 0 >=0 && 0 >= 0 so the code in the body if the "if" will be executed, and the "else" will not be executed. And then you get to the end of the function and return. You do not have a loop.
Your plot() assumes that x and y are vectors, consistent with you having initialized x(1) and y(1). But the code y >=0 && x >= 0 will fail as soon as y becomes a vector, because you cannot use && with vector values. You could try switching to & instead of && but remember that y >=0 & x >= 0 means the same as all(y >=0 & x >= 0)
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre General Physics 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!