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

1 回表示 (過去 30 日間)
Haris Hameed
Haris Hameed 2020 年 6 月 27 日
コメント済み: Haris Hameed 2020 年 6 月 29 日
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

採用された回答

Alan Stevens
Alan Stevens 2020 年 6 月 27 日
編集済み: Alan Stevens 2020 年 6 月 27 日
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 件のコメント
Alan Stevens
Alan Stevens 2020 年 6 月 28 日
編集済み: Alan Stevens 2020 年 6 月 28 日
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;
Haris Hameed
Haris Hameed 2020 年 6 月 29 日
yes thank you , got it

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by