<div>% Script file: ball.m</div><div>%</div><div>% Purpose:</div><div>% This program calculates the distance traveled by a ball</div><div>% thrown at a specified angle "theta" and a specified</div><div>% velocity "vo" from a point on the surface of the Earth,</div><div>% ignoring air friction and the Earth's curvature. It</div><div>% calculates the angle yielding maximum range, and also</div><div>% plots selected trajectories.</div><div>%</div><div>% Record of revisions:</div><div>% Date Programmer Description of change</div><div>% ==== ========== =====================</div><div>% 12/10/97 S. J. Chapman Original code</div><div>%</div><div>% Define variables:</div><div>% conv Degrees to radians conv factor</div><div>% gravity Accel. due to gravity (m/s^2)</div><div>% ii, jj Loop index</div><div>% index Location of maximum range in array</div><div>% maxangle Angle that gives maximum range (deg)</div><div>% maxrange Maximum range (m)</div><div>% range Range for a particular angle (m)</div><div>% time Time(s)</div><div>% theta Initial angle (deg)</div><div>% traj_time Total trajectory time (s)</div><div>% vo Initial velocity (m/s)</div><div>% vxo Xcomponent of initial velocity (m/s)</div><div>% vyo Ycomponent of initial velocity (m/s)</div><div>% x Xposition of ball (m)</div><div>% y Yposition of ball (m)</div><div>% Constants </div><div>conv = pi / 180; % Degreestoradians conversion factor </div><div>g = -9.81; % Accel. due to gravity</div><div>vo = 20; % Initial velocity</div><div>%Create an array to hold ranges</div><div>range = zeros(1,91); % Calculate maximum ranges</div><div>for ii = 1:91</div><div> theta = ii - 1;</div><div> vxo = vo * cos(theta*conv);</div><div> vyo = vo * sin(theta*conv);</div><div> traj_time = -2 * vyo / g;</div><div> range(ii) = vxo * traj_time;</div><div>end</div><div>% Write out table of ranges</div><div>fprintf ('Range versus angle theta:\n');</div><div>for ii = 1:91</div><div> theta = ii - 1;</div><div> fprintf(' %2d %8.4f\n',theta, range(ii));</div><div>end</div><div>% Calculate the maximum range and angle</div><div>[maxrange index] = max(range);</div><div>maxangle = index - 1;</div><div>fprintf ('\nMax range is %8.4f at %2d degrees.\n',maxrange, maxangle);</div><div>% Now plot the trajectories</div><div>for ii = 5:10:85</div><div> % Get velocities and max time for this angle</div><div> theta = ii;</div><div> vxo = vo * cos(theta*conv);</div><div> vyo = vo * sin(theta*conv);</div><div> traj_time = -2 * vyo / g;</div><div> % Calculate the (x,y) positions</div><div> x = zeros(1,21);</div><div> y = zeros(1,21);</div><div> for jj = 1:21</div><div> time = (jj - 1) * traj_time/20;</div><div> x(jj) = vxo * time;</div><div> y(jj) = vyo * time + 0.5 * g * time^2;</div><div> end</div><div> plot(x,y,'b');</div><div> if ii == 5</div><div> hold on;</div><div>end</div><div>end</div><div>% Add titles and axis lables</div><div>title ('\bfTrajectory of Ball vs Initial Angle \theta');</div><div>xlabel ('\bf\itx \rm\bf(meters)');</div><div>ylabel ('\bf\ity \rm\bf(meters)');</div><div>axis ([0 45 0 25]);</div><div>grid on;</div><div>% Now plot the max range trajectory</div><div>vxo = vo * cos(maxangle*conv);</div><div>vyo = vo * sin(maxangle*conv);</div><div>traj_time = -2 * vyo / g;</div><div>% Calculate the (x,y) positions</div><div>x = zeros(1,21);</div><div>y = zeros(1,21);</div><div>for jj = 1:21</div><div> time = (jj - 1) * traj_time/20;</div><div> x(jj) = vxo * time;</div><div> y(jj) = vyo * time + 0.5 * g * time^2;</div><div>end</div><div>plot(x,y,'r','LineWidth',3.0);</div><div>hold off</div>