How to create a plot with this code?

11 ビュー (過去 30 日間)
BRS
BRS 2017 年 12 月 19 日
回答済み: Star Strider 2017 年 12 月 19 日
When trying to plot a graph nothing but the x axis and y axis are displayed in the figure. The code is for drawing the approximate path of a projectile which is experiencing drag.
close all
%constants and starting conditions
g =-9.81;
A = 0.25 * pi * (0.01512); %with 15.12mm the diameter of the projectile
ro = 1.293; %Binas' air density at 20C at sea level
c = 0.47; %drag coefficient for a sphere
B = 45; %launch angle
Xx = 0;
Xy = 1-sin(B)*0.202; %projectile-floor height in meters
Vc = 10.66; %launch speed in meters per second
Vx = sin(B)*Vc; %horizontal speed vector
Vy = cos(B)*Vc; %vertical speed vector
dt = 0.1; %timestep
m = 0.001684;%projectile mass in kilograms
awx(1) = 0;
awy(1) = 0;
dVy(1) = 0;
dVx(1) = 0;
dXx(1) = 0;
dXy(1) = 0;
Fwx(1) = 0;
Fwy(1) = 0;
t(1) = 0;
%model
while Xy >= 0
ax = awx;
Vx = Vx+dVx;
dVx = ax*dt;
Xx = Xx+dXx;
dXx = Vx*dt+0.5*ax*(dt)^2;
ay = g+awy;
Vy = Vy+dVy;
dVy = ay*dt;
Xy = Xy+dXy;
dXy = Vy*dt+0.5*ay*(dt)^2;
Vd = sqrt((Vx)^2+(Vy)^2);%sum of both speed vectors in flight
Fw = -0.5*ro*A*c*(Vd)^2;
theta = atan((Vx)/(Vy));%angle of Vd in fight
Fwy = cos(theta)*Fw;
Fwx = sin(theta)*Fw;
awx = (Fwx)/m;
awy = (Fwy)/m;
t = t+dt;
end
plot(Xx,Xy)
xlabel('x (m)')
ylabel('y (m)')
title('Projectile Path')

採用された回答

Star Strider
Star Strider 2017 年 12 月 19 日
You need to subscript and save ‘Xx’ and ‘Xy’, I created ‘Xxv’ and ‘Xyv’ to do this, and also ‘tv’ to save the time vector if you need it.
This plots:
%model
k = 1;
while Xy >= 0
ax = awx;
Vx = Vx+dVx;
dVx = ax*dt;
Xx = Xx+dXx;
dXx = Vx*dt+0.5*ax*(dt)^2;
Xxv(k) = Xx;
ay = g+awy;
Vy = Vy+dVy;
dVy = ay*dt;
Xy = Xy+dXy;
dXy = Vy*dt+0.5*ay*(dt)^2;
Xyv(k) = Xy;
Vd = sqrt((Vx)^2+(Vy)^2);%sum of both speed vectors in flight
Fw = -0.5*ro*A*c*(Vd)^2;
theta = atan((Vx)/(Vy));%angle of Vd in fight
Fwy = cos(theta)*Fw;
Fwx = sin(theta)*Fw;
awx = (Fwx)/m;
awy = (Fwy)/m;
t = t+dt;
tv(k) = t;
k = k + 1;
end
plot(Xxv,Xyv)
xlabel('x (m)')
ylabel('y (m)')
title('Projectile Path')
You likely still have some work to do on this.

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by