Question

I'm trying to model projectile motion with drag in Matlab. Everything works perfectly....except I can't figure out how to get it to stop when the "bullet" hits the ground.

I initially tried an iteration loop, defining a data array, and emptying cells of that array for when the y value was negative....unfortunately the ode solver didn't like that too much.

Here is my code

      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

I thought this would define an event when y=0 and stop the projectile, but it doesn't do anything. What am I doing wrong?

Was it helpful?

Solution

When trying to find the time at which the solution to the ODE reaches a certain level you should use an Events function - see the BALLODE demo for an example that stops the solution process when one of the components of the solution reaches 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top