Plotting two variables from a loop
3 次查看(过去 30 天)
显示 更早的评论
Hi, the code below indicates 2D equations of the projectile of a payload delivery system. The numerical process induces varying values for 'x' and 'y'. I am trying to plot the projectile motion of the object (so x vs. y) for every time step (loop). How can I do this?
% Constants
g=9.81;
m=1.5;
A=0.028;
Cd=0.8;
rho=1.225;
B=(rho*Cd*A)/2;
Dt=1e-6;
% Initial values
x=0;
y=6.4008; %21 ft
V=12.5;
alpha=90;
Vx=V*sind(alpha);
Vy=V*cosd(alpha);
t=0;
for k=1:1e9
if y<0
break
end
ax= -(B/m)*(V*Vx);
ay=(B/m)*(V*Vy)-g;
x=x+Vx*Dt+0.5*ax*(Dt^2);
y=y+Vy*Dt+0.5*ay*(Dt^2);
Vx=Vx+ax*Dt;
Vy=Vy+ay*Dt;
V=sqrt((Vx^2)+(Vy^2));
t=t+Dt;
end
0 个评论
采纳的回答
KSSV
2020-4-3
% Constants
g=9.81;
m=1.5;
A=0.028;
Cd=0.8;
rho=1.225;
B=(rho*Cd*A)/2;
Dt=1e-6;
% Initial values
x=0;
y=6.4008; %21 ft
V=12.5;
alpha=90;
Vx=V*sind(alpha);
Vy=V*cosd(alpha);
t=0;
X = zeros([],1) ;
Y = zeros([],1) ;
for k=1:1e9
if y<0
break
end
ax= -(B/m)*(V*Vx);
ay=(B/m)*(V*Vy)-g;
x=x+Vx*Dt+0.5*ax*(Dt^2);
y=y+Vy*Dt+0.5*ay*(Dt^2);
Vx=Vx+ax*Dt;
Vy=Vy+ay*Dt;
V=sqrt((Vx^2)+(Vy^2));
t=t+Dt;
X(k) = x ;
Y(k) = y ;
end
plot(X,Y)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!