matlab绘制抛物线轨迹

前文已经学会了语法和绘图函数,今天学习一个综合练习。

预备知识:高中小球斜抛的物理知识

代码如下:

%Purpose:
%This program calculates the distance traveled by a ball throw at a
%specified angle "theta" and a spedified velocity "vo" from a
%point,ignoring air friction.It calculates the angle yielding maximum
%range,and also plots selected trajectories.
%
%Define variable:
%conv           Degrees to adians conv factor
%grav           The gravity acceleration
%ii,jj          Loop index
%index          The maximum range in array
%maxangle       The angle that gives the maximum range
%maxrange       Maximum range
%time           Time
%theta          Initial angle
%fly_time       The total trajectory time
%vo             The initial velocity
%vxo            x-component of the initial velocity
%vyo            y-component of the initial velocity
%x              x position of ball
%y              y position of ball
%定义常数数值
conv=pi/180;
grav=-9.82;
vo=input('Enter the initial velocity:');
range=zeros(1,91);
%计算最大水平距离
for ii = 1:91
    theta = ii -1;
    vxo = vo * cos(theta * conv);
    vyo = vo * sin(theta * conv);
    max_time = -2 * vyo/grav;
    range(ii) = vxo * max_time;
end
%显示计算水平距离的列表
fprintf('Angle versus Range\n');
for ii = 1:5:91
    theta = ii-1;
    fprintf('%2d\t\t%8.4f\n',theta,range(ii));
end
%计算最大的角度和水平距离
[maxrange,index]=max(range);
maxangle = index -1;
fprintf('\n Max range is %8.4f at %2d degrees.\n',maxrange,maxangle);
%绘制轨迹图形
for ii = 5:10:80
    theta =ii;
    vxo = vo*cos(theta*conv);
    vyo = vo*sin(theta*conv);
    max_time = -2 * vyo/grav;
    %计算小球轨迹的x、y坐标数值
    x=zeros(1,21);
    y=zeros(1,21);
    for jj=1:21
        time = (jj-1) * max_time/20;
        x(jj) = vxo * time;
        y(jj) = vyo * time + 0.5 * grav * time^2;
    end
    plot(x,y,'g');
    if ii == 5
        hold on;
    end
end
%添加图形的标题和坐标轴名称
title('\bf Trajectory of Ball vs Initial Angle \it\theta');
xlabel('\bf\itx\rm\bf(meters)');
ylabel('\bf\ity\rm\bf(meters)');
axis([0,max(range)+5,0,-vo^2/2/grav]);
grid on;
%绘制最大水平的轨迹图形
vxo = vo * cos(maxangle * conv);
vyo = vo * sin(maxangle * conv);
max_time = -2 * vyo/grav;
%计算(x,y)点
x = zeros(1,21);
y = zeros(1,21);
for jj = 1:21
    time = (jj -1)*max_time/20;
    x(jj) = vxo * time;
    y(jj) =vyo * time + 0.5 * grav * time ^ 2;
end
plot(x,y,'r','Linewidth',2);
hold off;

将上述代码保存为ball.m,在matlab的命令窗口,输入ball后回车。输入不同的初始速度。运行情况如下:

>> ball
Enter the initial velocity:45
Angle versus Range
 0        0.0000
 5       35.8083
10       70.5286
15      103.1059
20      132.5504
25      157.9674
30      178.5847
35      193.7757
40      203.0790
45      206.2118
50      203.0790
55      193.7757
60      178.5847
65      157.9674
70      132.5504
75      103.1059
80       70.5286
85       35.8083
90        0.0000

 Max range is 206.2118 at 45 degrees.

抛物线如下:


matlab绘制抛物线轨迹_第1张图片
Vo为45m/s的抛物线.png

下面对ball.m中一些代码做一点解释:

  • zeros(1,91) 生成1*91的零矩阵
  • xlabel('\bf\itx\rm\bf(meters)')
    • \bf表示后边字符为黑体
    • \it表示后边字符为斜体
    • \rm恢复正常字体
    • \theta 参照文本字符串的高级控制
  • hold off 方便下次调用ball.m时擦除前面的figure

练习:

1.对每个角度的抛物线用不同的颜色绘制,并用图例(legend函数)标注
2.绘制同样抛射角,不同初速度的图像

你可能感兴趣的:(matlab绘制抛物线轨迹)