Clear Filters
Clear Filters

problem in plotting in a nested while loop in a for loop

3 views (last 30 days)
i want to plot for different values of Vx. but it is only plotting for one value. please guide
clc
clear all
Vx = [0.7383
1.3266
1.5226
1.6058
1.6388
1.6482
1.6486
1.6494
1.6552
1.6663
1.6787
1.6847
1.6727
1.6240
1.5007
1.1878]*1000;
m=.001;
A=pi*(.007)^2;
C=.9;
rho= 1.2 ;
D=rho*C*A/2;
g=9.81;
%Initial Conditions
delta_t= .001; %s
theta=10; %deg
count=1;
for aa=1:16
nn=1;
x(1)=0;
y(1)=0;
t(1)=0 ;
vin=Vx(aa);
vx=vin*cosd(theta);
vy=vin*sind(theta);
while min(y)> -.001
v = sqrt(vx^2 + vy^2);
ax=-(D/m)*vx^2;
ay=-g-(D/m)*vy^2;
vx=vx+ax*delta_t;
vy=vy+ay*delta_t;
x(nn+1)=x(nn)+vx*delta_t+.5*ax*delta_t^2;
y(nn+1)=y(nn)+vy*delta_t+.5*ay*delta_t^2;
t(nn+1)=t(nn)+delta_t;
nn=nn+1;
end
count=count+1;
plot(x,y)
xlabel('x distance (m)')
ylabel('y distance (m)')
title('Projectile Path')
hold on
end

Accepted Answer

Alan Stevens
Alan Stevens on 27 Jun 2020
Edited: Alan Stevens on 27 Jun 2020
Replace the code after count = count+1; with the following to get separate figures (though the curves ae all the same!):
figure
plot(x,y)
xlabel('x distance (m)')
ylabel('y distance (m)')
title(['Projectile Path Vx = ' num2str(Vx(aa))] )
%hold on
I think your while loop logic needs modifying to the following in order to get different curves:
flag = true;
while flag
v = sqrt(vx^2 + vy^2);
ax=-(D/m)*vx^2;
ay=-g-(D/m)*vy^2;
vx=vx+ax*delta_t;
vy=vy+ay*delta_t;
x(nn+1)=x(nn)+vx*delta_t+.5*ax*delta_t^2;
y(nn+1)=y(nn)+vy*delta_t+.5*ay*delta_t^2;
t(nn+1)=t(nn)+delta_t;
nn=nn+1;
if y(nn)<=0
x(nn) = NaN; y(nn) = NaN; t(nn) = NaN;
flag = false;
end
end
If you want all the curves to appear on the same figure then keep your original plot commands.
Also, I suspect your ay term should be:
ay=-g-(D/m)*vy*abs(vy);
as the drag will oppose gravity when the projectile is coming down (i.e. when vy is negative).
  5 Comments
Alan Stevens
Alan Stevens on 28 Jun 2020
Edited: Alan Stevens on 28 Jun 2020
Looking at your equations again, I notice an error in the physics. You need to caculate the drag force using the veocity v, then resolve this onto the x and y directions, not resolve the velocity first and then apply the drag to the separate x and y velocities. The end result is that your acceleration equations should look like the following;
ax=-(D/m)*vx*v;
ay=-g-(D/m)*vy*v;

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!