数学建模社区-数学中国

标题: 编程实例 [打印本页]

作者: 森之张卫东    时间: 2015-9-15 22:19
标题: 编程实例
编程实例:
具体代码:
  1. <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>
复制代码
具体内容见附件:


截图.JPG (125.83 KB, 下载次数: 16)

截图.JPG

具体例子.doc

167.5 KB, 下载次数: 0, 下载积分: 体力 -2 点

具体例子


作者: 森之张卫东    时间: 2015-9-15 22:20
  1. % Script file: ball.m
  2. %
  3. % Purpose:
  4. % This program calculates the distance traveled by a ball
  5. % thrown at a specified angle "theta" and a specified
  6. % velocity "vo" from a point on the surface of the Earth,
  7. % ignoring air friction and the Earth's curvature. It
  8. % calculates the angle yielding maximum range, and also
  9. % plots selected trajectories.
  10. %
  11. % Record of revisions:
  12. % Date Programmer Description of change
  13. % ==== ========== =====================
  14. % 12/10/97 S. J. Chapman Original code
  15. %
  16. % Define variables:
  17. % conv Degrees to radians conv factor
  18. % gravity Accel. due to gravity (m/s^2)
  19. % ii, jj Loop index
  20. % index Location of maximum range in array
  21. % maxangle Angle that gives maximum range (deg)
  22. % maxrange Maximum range (m)
  23. % range Range for a particular angle (m)
  24. % time Time(s)
  25. % theta Initial angle (deg)
  26. % traj_time Total trajectory time (s)
  27. % vo Initial velocity (m/s)
  28. % vxo Xcomponent of initial velocity (m/s)
  29. % vyo Ycomponent of initial velocity (m/s)
  30. % x Xposition of ball (m)
  31. % y Yposition of ball (m)
  32. % Constants
  33. conv = pi / 180;    % Degreestoradians conversion factor
  34. g = -9.81;          % Accel. due to gravity
  35. vo = 20;            % Initial velocity
  36. %Create an array to hold ranges
  37. range = zeros(1,91); % Calculate maximum ranges
  38. for ii = 1:91
  39.     theta = ii - 1;
  40.     vxo = vo * cos(theta*conv);
  41.     vyo = vo * sin(theta*conv);
  42.     traj_time = -2 * vyo / g;
  43.     range(ii) = vxo * traj_time;
  44. end
  45. % Write out table of ranges
  46. fprintf ('Range versus angle theta:\n');
  47. for ii = 1:91
  48.     theta = ii - 1;
  49.     fprintf(' %2d %8.4f\n',theta, range(ii));
  50. end
  51. % Calculate the maximum range and angle
  52. [maxrange index] = max(range);
  53. maxangle = index - 1;
  54. fprintf ('\nMax range is %8.4f at %2d degrees.\n',maxrange, maxangle);
  55. % Now plot the trajectories
  56. for ii = 5:10:85
  57.     % Get velocities and max time for this angle
  58.     theta = ii;
  59.     vxo = vo * cos(theta*conv);
  60.     vyo = vo * sin(theta*conv);
  61.     traj_time = -2 * vyo / g;
  62.     % Calculate the (x,y) positions
  63.     x = zeros(1,21);
  64.     y = zeros(1,21);
  65.     for jj = 1:21
  66.         time = (jj - 1) * traj_time/20;
  67.         x(jj) = vxo * time;
  68.         y(jj) = vyo * time + 0.5 * g * time^2;
  69.     end
  70.     plot(x,y,'b');
  71.     if ii == 5
  72.     hold on;
  73. end
  74. end
  75. % Add titles and axis lables
  76. title ('\bfTrajectory of Ball vs Initial Angle \theta');
  77. xlabel ('\bf\itx \rm\bf(meters)');
  78. ylabel ('\bf\ity \rm\bf(meters)');
  79. axis ([0 45 0 25]);
  80. grid on;
  81. % Now plot the max range trajectory
  82. vxo = vo * cos(maxangle*conv);
  83. vyo = vo * sin(maxangle*conv);
  84. traj_time = -2 * vyo / g;
  85. % Calculate the (x,y) positions
  86. x = zeros(1,21);
  87. y = zeros(1,21);
  88. for jj = 1:21
  89.     time = (jj - 1) * traj_time/20;
  90.     x(jj) = vxo * time;
  91.     y(jj) = vyo * time + 0.5 * g * time^2;
  92. end
  93. plot(x,y,'r','LineWidth',3.0);
  94. hold off
复制代码






欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5