MATLAB: I’m writing a code for projectile motion and i got a code but I’m not sure how to convert For loop into a while loop

projectile motion

% clear all existing variable in matlab to ensure no confusion...
clear
Sx_f=input('Enter the horizontal distance ');
Sy_f=input('Enter the vertical distance ');
%specify initial position of ball(i.e. point where ball leaves launcher):
x_0=-0.05; %m



y_0=0.1; %m
% position to/at net
Sx_net=1.37; %m
Sy_net=Sy_f; %m
% final position
Syf=0.02;
Sxf=Sx_f;
% specify initial velocity and angle
theta_0=atand((Sx_net)^2*Syf-(Sxf)^2*Sy_net/(Sx_net*Sxf*(Sx_net-Sxf)))*(pi/180) % rads
u_0=(Sxf/cos(theta_0))*sqrt(9.81/(2*Sxf*tan(theta_0)-Sy_net)) % m/s
% specify length of time-step for calculation
delta_t= 1/100; % s

t=0:delta_t:1;
% let i_ts be the time-step number
i_ts=1;
% initialise the time as zero at the start of first time step
t(i_ts)=0; % s
% set initial condition 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;
x(1)=x_ball(i_ts); % initial position of the ball in x axis
y_ball(i_ts)=y_init;
y(1)=y_ball(i_ts); % initial position of ball in y axis
ux_ball(i_ts)=ux_init;
uy_ball(i_ts)=uy_init;
% find the acceration 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*t+0.5*accel_x*(t.^2);
y_final= y_init +uy_init*t+0.5*accel_y*(t.^2);
ux_final=ux_init+accel_x*t;
uy_final=uy_init+accel_y*t;
%constants for the ball
m=0.0027; %kg
A=pi*(0.0126)^2; % area of ball m^2
C=0.445; %Drag Coefficient of a sphere
rho= 1.2; %kg/m^3 (density of air)
Fd=rho*C*A/2; % drag force (N)
g=accel_y; %m/s^2 (acceleration due to gravity
% for loop for projectile without drag
for j=1:size(x_final,2)
if(j>1 && y_final(j)<=0)
break;
end
% while loop for projectile with Drag
i=1;
while (y)>= 0
ax=-(Fd/m)*u_0*ux_init;
ay=accel_y-(Fd/m)*u_0*uy_init;
ux_init=ux_init+ax*delta_t;
uy_init=uy_init+ay*delta_t;
x(i+1)=x(i)+ux_init*delta_t+0.5*ax*delta_t^2;
y(i+1)=y(i)+uy_init*delta_t+0.5*ay*delta_t^2;
i=i+1;
end
figure(1)
plot(x_final(j),y_final(j),'r.',x,y,'k-')
ylabel('height (m)')
xlabel('Distance (m)')
title('Displacement of ball in x and y direction')
legend('Without Drag','With Drag')
hold on
grid on
figure(2)
plot(t(j),y_final(j),'b.')
hold on
plot(t(j),x_final(j),'g.')
hold off
ylabel('displacement (m)')
xlabel('time (s)')
legend('y-displacement','x-displacement','location','northwest')
title('Displacement of ball in x and y direction against time')
hold on
grid on
figure(3)
plot(t(j),ux_final(j),'b.')
hold on
plot(t(j),uy_final(j),'m.')
hold off
ylabel('velocity (m/s)')
xlabel('time (s)')
legend('velocity in x-axis','velocity in y-axis','location','west')
title('Velocity of ball in x and y direction against time')
hold on
grid on
end

Best Answer

for variable = start : increment : last
code section
end
with positive increment can be converted into
hidden_variable = start;
variable = [];
while hidden_variable <= last
variable = hidden_variable;
code section
hidden_variable = hidden_variable + increment;
end
Many people would instead code it as
variable = start;
while variable <= last
code section
variable = variable + increment;
end
However that is not exactly the same as what MATLAB does. In particular, the code differs as to what happens if the code section modifies the variable, and the code differs as to exactly what value variable will end up with after the loop, and also as to what happens if the final value is less than the original value.