Plot results of a While loop
Show older comments
Hi,
The code calculates the velocity and the x and y position of a PingPong ball for several time intervalls. Each time intervall is 1/100 seconds
I want to plot all results from the while loop so I can see how "y_final" and "x_final" vary with time.
The problem is when I run the code, I get only a emty graph.
Can someone give me a hint?
..................................................................................................................
clear
% specify initial position of ball (i.e. point where ball leaves laucher):
x_0 = -0.05; %m
y_0 = 0.1; %m
% specify initial velocity and angle
u_0 = 5; % m/s
theta_0 = 10*(pi/180); % rads
%specify length of time-step for calculation
delta_t = 1/100; % s
%let i_ts be the time-step number
i_ts = 1; % (i.e. this is the first time-step
t(i_ts) = 0; % s
% set initial conditions for first time-step
x_init = x_0;
y_init = y_0;
ux_init = u_0 * cos(theta_0);
uy_init = u_0 * sin(theta_0);
% record the position and velocity of the ball at the start of the
% time-step
x_ball(i_ts) = x_init;
y_ball(i_ts) = y_init;
ux_ball (i_ts) = ux_init;
uy_ball (i_ts) = uy_init;
% find the acceleration acting on the projectile at the start of the
% time-step
accel_x = 0;
accel_y = -9.81;
% calculate final velocity and position at the end of the first time-step
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t;
uy_final = uy_init + accel_y*delta_t;
while y_final>0;
delta_t = delta_t + 0.01;
x_final = x_init + ux_init*delta_t + 0.5*accel_x*(delta_t^2);
y_final = y_init + uy_init*delta_t + 0.5*accel_y*(delta_t^2);
ux_final = ux_init + accel_x*delta_t
uy_final = uy_init + accel_y*delta_t
figure(1)
plot (x_final, y_final, ux_final, uy_final);
ylim([0 1000]);
xlim([ 0 10]);
end
hold on
box on
.......................................................................................................................
1 Comment
Asad (Mehrzad) Khoddam
on 11 Oct 2020
You have plotted y_final vs x_final that is only one point. You should plot them vs time. t_final, x_final and y_final should be a vector of values to be plotted
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!