Advertisement
ksoltan

Baseball Simulation

Nov 17th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.65 KB | None | 0 0
  1. % Find optimal initial velocity and angle at which to hit a baseball to
  2. % make it over the Green Monster Wall
  3. function baseball()
  4.     % Loop through initial speed and angle combinations
  5.     speed = 30 : 60;
  6.     angle = 5 : 89;
  7.     heights = zeros(length(speed), length(angle));
  8.     for s = 1 : length(speed)
  9.         for a = 1 : length(angle)
  10.             % baseball simulation returns the final height of the ball
  11.             h = baseball_simulation(speed(s), angle(a));
  12.             heights(s, a) = h;
  13.         end
  14.     end
  15.     figure(3)
  16.     % plot the height of the ball as a function of angle and speed
  17.     pcolor(angle, speed, heights);
  18.     hold on
  19.     % Plot line to show where the ball ends up close to the 12 m height is
  20.     %(the wall) and 1 m, close to the ground
  21.     clabel(contour(angle, speed, heights, [1, 12], 'LineColor', 'w', 'LineWidth', 1))
  22. end
  23.  
  24. function res = baseball_simulation(init_speed, init_angle)
  25. % Function takes in initial speed and angle of a baseball and returns the
  26. % final height of the ball
  27.     % Baseball constants
  28.     b_mass = 0.145; % kg
  29.     b_radius = 0.07468 / 2; % m
  30.     b_c_area = pi * b_radius^2; % cross sectional area, m
  31.     coeff_drag = 0.3;
  32.    
  33.     % Environment constants
  34.     air_density = 1.2; % kg / m^3
  35.     g = 9.8; % m / s^2
  36.     wall_x = 97; % distance from place of hit
  37.     wall_y = 12; % height of wall
  38.    
  39.     % Run the main function which should return the final height
  40.     res = main();
  41.    
  42.     function res = main()
  43.         res = simulate();
  44.     end
  45.  
  46.     % Returns the final height of the ball
  47.     function res = simulate()
  48.         options = odeset('Events', @event_function);
  49.         % Return all data points calculated by ode45, as well as the param
  50.         % values at the time TE when the function event is called
  51.         [Time, Y, TE, YE] = ode45(@change, [0, 10], ...
  52.             [0, 1, velocity_vector(init_speed, init_angle)], options);        
  53.         res = YE(2); % final height of the ball, determined when the function stops
  54.        
  55.         % Trigger event when the ball hits the ground or reaches
  56.         % the wall
  57.         function [value, isterminal, direction] = event_function(t, params)
  58.            % To trigger, the value should equal 0. If the x pos of ball is
  59.            % the x pos of wall, it will be 0
  60.            value = params(2);
  61.            if wall_x - params(1) <= 0 || params(2) <= 0
  62.                value = 0;
  63.            % If the ball does not reach wall, check if it has hit the
  64.            % ground
  65.            end
  66.            isterminal = 1; % stop function as soon as this event is reached
  67.            direction = 0; % At any zero (when value = 0) consider this event, not
  68.                           % only when the function is either increasing (1) or
  69.                           % decreasing (-1)
  70.         end
  71.     end
  72.  
  73.     function res = velocity_vector(speed, angle)
  74.         % Takes the magnitude of the velocity and the angle at which the
  75.         % object is moving to the horizontal
  76.         % Returns a velocity vector in the form (vx, vy)
  77.         % Angle is in degrees, therefore must be converted to radians
  78.         angle_rad = angle * pi / 180;
  79.         res = [speed * cos(angle_rad), speed * sin(angle_rad)];
  80.     end
  81.  
  82.     function res = change(t, params)
  83.        % Position is a vector. (x, y)
  84.        Pos = params(1 : 2);
  85.        % velocity is a vector. (vx, vy)
  86.        V = params(3 : 4);
  87.        % change in position is velocity
  88.        dPosdt = V;
  89.        % change in velocity is acceleration
  90.        dVdt = acceleration(t, Pos, V);
  91.        res = [dPosdt; dVdt];
  92.     end
  93.  
  94.     function res = acceleration(t, Pos, V)
  95.         % Drag is dependent only on velocity, therefore position and time
  96.         % are not needed. But just in case we included an acceleration
  97.         % source dependent upon them, t and pos are passed into the
  98.         % function.
  99.        vx = V(1);
  100.        vy = V(2);
  101.        % Force of drag
  102.        v = sqrt(vx^2 + vy^2);
  103.        drag = -1/2 * coeff_drag * b_c_area * air_density * v^2;
  104.        % Force of gravity
  105.        gravity = -b_mass * g;
  106.        
  107.        % Unit velocity vector that points in the horizontal direction
  108.        unit_vx = vx / v;
  109.        % Unit velocity vector that points in the vertical direction
  110.        unit_vy = vy / v;
  111.        
  112.        % Total horizontal acceleration is just the horizontal component of
  113.        % drag. Must be divided by m because F = ma, a = F/m
  114.        dvxdt = (drag * unit_vx) / b_mass;
  115.        % Total vertical acceleration is the vertical component of drag and
  116.        % the acceleration due to gravity
  117.        dvydt = (gravity + drag * unit_vy) / b_mass;
  118.        
  119.        res = [dvxdt; dvydt];
  120.     end
  121.    
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement