我正在尝试用空气阻力来模拟弹丸运动。
这是我的代码:
function [ time , x , y ] = shellflightsimulator(m,D,Ve,Cd,ElAng)
% input parameters are:
% m mass of shell, kg
% D caliber (diameter)
% Ve escape velocity (initial velocity of trajectory)
% Cd drag coefficient
% ElAng angle in RADIANS
A = pi.*(D./2).^2; % m^2, shells cross-sectional area (area of circle)
rho = 1.2 ; % kg/m^3, density of air at ground level
h0 = 6800; % meters, height at which density drops by factor of 2
g = 9.8; % m/s^2, gravity
dt = .1; % time step
% define initial conditions
x0 = 0; % m
y0 = 0; % m
vx0 = Ve.*cos(ElAng); % m/s
vy0 = Ve.*sin(ElAng); % m/s
N = 100; % iterations
% define data array
x = zeros(1,N + 1); % x-position,
x(1) = x0;
y = zeros(1,N + 1); % y-position,
y(1) = y0;
vx = zeros(1,N + 1); % x-velocity,
vx(1) = vx0;
vy = zeros(1,N + 1); % y-velocity,
vy(1) = vy0;
i = 1;
j = 1;
while i < N
ax = -Cd*.5*rho*A*(vx(i)^2 + vy(i)^2)/m*cos(ElAng); % acceleration in x
vx(i+1) = vx(i) + ax*dt; % Find new x velocity
x(i+1) = x(i) + vx(i)*dt + .5*ax*dt^2; % Find new x position
ay = -g - Cd*.5*rho*A*(vx(i)^2 + vy(i)^2)/m*sin(ElAng); % acceleration in y
vy(i+1) = vy(i) + ay*dt; % Find new y velocity
y(i+1) = y(i) + vy(i)*dt + .5*ay*dt^2; % Find new y position
if y(i+1) < 0 % stops when projectile reaches the ground
i = N;
j = j+1;
else
i = i+1;
j = j+1;
end
end
plot(x,y,'r-')
end
这就是我放入 Matlab 的内容:
shellflightsimulator(94,.238,1600,.8,10*pi/180)
这会产生一个奇怪的情节,而不是抛物线。此外,这些位置似乎是负值。注意:ElAng 以弧度为单位!
我究竟做错了什么?谢谢!