自由射击弹丸运动问题

时间:2015-05-07 23:49:49

标签: matlab plot

我正在为freemat编写一个代码,用于计算和绘制包含四个不同阻力系数值的球形物体的射弹运动。一切都很好,直到最后一部分,我需要有多个情节。距离与高度的关系图很好,但是当我尝试制作其他图时会出现问题。我认为我无法定义正确的变量和数组来制作情节。我需要绘制的其他变量是每个阻力系数的最大y和最大z以及每个阻力系数的最大时间。我想单独为每个图设置一个图,而不是使用子图。

这就是我所拥有的:

% Clear screen and variables

clear

clc

% Initial variables

x0=0; %Initial x position

y0=0; %Initial y position

v0=50; %Initial velocity

theta=30; %Degrees

A=0.0042; %Silhouette area of a sphere

dt=0.001; %Change in time  

g=9.81; %Gravity

rho=1.2; %Density of air, used in calculating drag

m=.145; %Mass of object

C=[0,.2,.5,1.2]; %Drag coefficients 

N=length(C); %

%Arrays to hold values used in the program

x=[];

y=[];

t=[];

vx=[];

vy=[];

ax=[];

ay=[];

MaxH=[];

MaxX=[];

%Assigning variables and equations to be used in the calculations

for j=1:N;

    Dm=rho*C(j)*A/2/m; 

    t(j,1)=0;

    x(j,1)=0;

    y(j,1)=0;

    vx(j,1)=v0*cosd(theta);

    vy(j,1)=v0*sind(theta);

    ax(j,1)=Acceleration( vx(j,1), v0, Dm, 0);

    ay(j,1)=Acceleration( vy(j,1), v0, Dm, -g);

    h=1;

    i=1;

    %Loop to calculate velocity, position, and acceleration

    while (h>0.0001 && i<6000);

        v=sqrt(vx(j,1)^2 + vy(j,1)^2);  %Calculation for velocity

        vx(j,i+1)= vx(j,i) + ax(j,i)*dt; %

        vy(j,i+1)= vy(j,i) + ay(j,i)*dt;

        x(j,i+1)= x(j,i) + vx(j,i)*dt + 0.5*ax(j,i)*dt^2;

        y(j,i+1)= y(j,i) + vy(j,i)*dt + 0.5*ay(j,i)*dt^2;

        ax(j,i+1)= Acceleration(vx(j,i) , v , Dm , 0);

        ay(j,i+1)= Acceleration(vy(j,i) , v , Dm , -g);

        h= y(j,i+1);

        t(j,i+1)= t(j,i) + dt;

        i=i+1;

        end

    %Display for the number of iterations at each Drag value

    nodes(j)=i;

    printf('Drag: %1.1f      Iteration:  %d\n',C(j),i);

end

%Graphs

hold on
for k=1:N

    plot(x(k,1:nodes(k)),y(k,1:nodes(k)))

    title('Distance vs. Height')

    xlabel('Distance in meters')

    ylabel('Height in meters')


    sizefig(1000,800);
end

hold off

print 'xy.jpeg'

这是函数Acceleration

function result=Acceleration(Vc,v,Dm,a0);

    result= a0-Dm*Vc*v;

0 个答案:

没有答案