MATLAB仿真运行到无穷大,但不是在调试器中逐步运行时

时间:2018-03-27 05:10:10

标签: matlab loops

我的代码模拟了降落伞部署时火箭的冲击载荷。当脚本作为一个整体运行时,震动的值变为无穷大。但是,当脚本在调试器模式下逐步运行时,它按预期工作。我不确定发生了什么或如何解决它。我的代码编写如下:

clear all; close all; clc; tic

h_init = 100; %initial altitude, ft
%INPUTS
E = .38e6;      %elastic modulus of nylon, psi
A = (1/16)*(1);  %cross sectional area of cord, in^2
CD = 2.20;       %parachute coefficient of drag
D_p = 144;       %parachute diameter, in
W_p = 2;        %weight of parachute, lbm
W_r = 105;      %dry weight of rocket, lbm
L = 17*3*12;     %length of shock cord, in
v_init = -350;     %initial upwards velocity of rocket/parachute, ft/s
dt = 0.00001;    %simulation time step, s
t_sim = 4;      %total simulation time, s
g = 32.2;       %gravitational acceleration, ft/s^2

%CALCULATIONS
k = (3900)./L;      %spring constant, lbf/in
S_p = pi*D_p.^2/4/144;   %parachute cross sectional area, ft^2
m_p = W_p/g;    %mass of parachute, slugs
m_r = W_r./g;    %mass of rocket, slugs


N = ceil(t_sim/dt);
N_L = length(W_r);

time = dt:dt:t_sim + dt;
Y1 = zeros(N_L,N);
Y2 = zeros(N_L,N);
V1 = zeros(N_L,N);
V2 = zeros(N_L,N);
FS = zeros(N_L,N);
Dr = zeros(N_L,N);
Shock = zeros(N_L,1);
Exten = zeros(N_L,1);
VDiff = zeros(N_L,1);
DrMax = zeros(N_L,1);
TDrop = zeros(N_L,1);

for j = 1:N_L
    v1 = v_init;    %initial velocity of parachute after deployment, ft/s
    y1 = 0;         %initial parachute position relative to deployment altitude, ft
    v2 = v_init;    %initial velocity of rocket after deployment, ft/s
    y2 = 0;         %initial position of rocket relative to deployment altitude, ft

    Y1(j,1) = y1;
    Y2(j,1) = y2;
    V1(j,1) = v1;
    V2(j,1) = v2;
    FS(j,1) = 0;
    Dr(j,1) = 0;
    slack = 1;
    for i = 1:N
        rho = findDensity(h_init+y1);%air density, slug/ft^3
        Q = 144*0.5*rho*v1^2;                           %dynamic pressure, psi
        D = -sign(v1)*(Q/144)*S_p*CD;                   %drag, lbf
        if y1-y2 >= L/12
            delta = 12*(y1-y2)-L;   %shock cord extension, in
            if slack ==1
                TDrop(j) = dt*i;
                slack = 0;
            end
        elseif y2-y1 >= L/12
            delta = -12*(y1-y2)-L;  %shock cord extension, in
        else
            delta = 0;
        end

        fs = k*delta;                                               %spring force, lbf

        F1 = D - W_p - fs;                                          %net force on parachute, lbf
        F2 = fs - W_r(j);                                              %net force on rocket, lbf

        a1 = F1/m_p;                                                %acceleration of parachute, ft/s^2
        a2 = F2/m_r(j);                                                %acceleration of rocket, ft/s^2

        y1 = y1 + v1*dt;                                            %update parachute position, ft
        y2 = y2 + v2*dt;                                            %update rocket position, ft

        v1 = v1 + a1*dt;                                            %update parachute velocity, ft/s
        v2 = v2 + a2*dt;                                            %update rocket velocity, ft/s

        Y1(j,i+1) = y1;
        Y2(j,i+1) = y2;
        V1(j,i+1) = v1;
        V2(j,i+1) = v2;
        FS(j,i+1) = fs;
        Dr(j,i+1) = D;
    end
        Shock(j) = max(FS(j,:));
        Exten(j) = 12*max(Y1(j,:)-Y2(j,:)-L/12);
        VDiff(j) = max(abs(V1(j,:)-V2(j,:)));
        DrMax(j) = max(Dr(j,:));
end

H1 = h_init+Y1;
H2 = h_init+Y2;
fprintf('Max Shock Load: %.2f lbf', Shock)
toc

function density = findDensity(h)
    temp = findTemp(h);
    pressure = findPressure(h);
    density = pressure / (1718 * (temp + 459.7));
end

function pressure = findPressure(h)
    temp = findTemp(h);
    if(h > 82345)
        pressure = 51.97 * (((temp + 459.7) / 389.98) ^ -11.388);
    elseif(h > 36152)
        pressure = 473.1 * exp(1.73 - (0.000048 * h));
    else
        pressure = 2116 * (((temp + 459.7) / 518.6) ^ 5.256);
    end
end

function temp = findTemp(h)
    if(h > 82345)
        temp = (0.00164 * h) - 205.5;
    elseif(h > 36152)
        temp = -70;
    else
        temp = 59 - (0.00356 * h);
    end
end

0 个答案:

没有答案