% Compute the trajectory of a baseball in flight, with and without air resistance % and plot results x = 0.0; % horizontal position of home plate, meters y = 1.0; % height of ball over strike zone v = 40.0; % initial speed of ball, meters per second theta = 76; % angle of ball off bat, degrees % initial position and speed of ball u0 = [x, y, v*cosd(theta), v*sind(theta)]; % solve for the first ten seconds of flight tspan = [0 10]; % solve the baseball equations numerically [t, u_withdrag] = ode45(@f_withdrag, tspan, u0); [t, u_nodrag] = ode45(@f_nodrag, tspan, u0); % get the x,y coordinates from the solutions and plot figure(1); clf() x = u_nodrag(:,1); y = u_nodrag(:,2); plot(x, y, 'r-') hold on x = u_withdrag(:,1); y = u_withdrag(:,2); plot(x, y, 'b-') plot([120, 120], [0, 5.2], 'k-', 'linewidth',2) legend('no drag', 'with drag', 'outfield fence') grid on xlim([-10, 130]) ylim([0, 110]) xlabel('x, meters') ylabel('y, meters') title('baseball trajectory')