森之张卫东 发表于 2015-9-15 22:19

编程实例

编程实例:
具体代码:<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> = 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 ();</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>具体内容见附件:


森之张卫东 发表于 2015-9-15 22:20

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

页: [1]
查看完整版本: 编程实例