2

Matlabでドラッグを使用して発射体の動きをモデル化しようとしています。すべてが完璧に機能します....「弾丸」が地面に当たったときに停止させる方法がわからない場合を除きます。

私は最初に反復ループを試し、データ配列を定義し、y値が負の場合にその配列のセルを空にしました....残念ながら、常微分方程式ソルバーはそれをあまり好きではありませんでした。

これが私のコードです

      function [ time , x_position , y_position ] = shell_flight_simulator(m,D,Ve,Cd,ElAng)

rho=1.2; % kg/m^3
g=9.84; % acceleration due to gravity
A = pi.*(D./2).^2; % m^2, shells cross-sectional area (area of circle)

    function [lookfor,stop,direction] = linevent(t,y);
        % stop projectile when it hits the ground
        lookfor = y(1); %Sets this to 0
        stop = 1; %Stop when event is located
        direction = -1; %Specify downward direction

        options = odeset('Events',@event_function); % allows me to stop integration at an event
        function fvec = projectile_forces(x,y)
            vx=y(2);
            vy=y(4);
            v=sqrt(vx^2+vy^2);

            Fd=1/2 * rho * v^2 * Cd * A;

            fvec(1) = y(2);
            fvec(2) = -Fd*vx/v/m;
            fvec(3) = y(4);
            fvec(4) = -g -Fd*vy/v/m;
            fvec=fvec.';
        end

        tspan=[0, 90]; % time interval of interest

        y0(1)=0;   % initial x position
        y0(2)=Ve*cos(ElAng); % vx
        y0(3)=0;   % initial y position
        y0(4)=Ve*sin(ElAng); % vy

        % using matlab solver
        [t,ysol] = ode45(@projectile_forces, tspan, y0);
    end
end
x =  ysol(:,1);
vx = ysol(:,2);
y =  ysol(:,3);
vy = ysol(:,4);


plot(x,y, 'r-');
xlabel('X Position (m)');
ylabel('Y Position (m)');
title ('Position Over Time');

end

これはy=0のときにイベントを定義し、発射物を停止すると思いましたが、何もしません。私は何が間違っているのですか?

4

1 に答える 1