Matlab——猎人打猴

写好公式,方便输入在这里插入图片描述
最终版本:
在这里插入图片描述在这里插入图片描述

%shoot_monkey
%by TTY
%2020.3.13

clear all;
%--参数设定及自变量取样--
g=9.8;                           %重力加速度
d=60;                            %猴子与猎人的距离 
h=30;                            %猴子距地面的高度
seita=atand(h/d);                %瞄准猴子的仰角
input('在郁郁葱葱的原始森林...')   %场景设置
input('一个猎人端起了枪,枪口对准树上的猴子')
v0=input('请输入子弹的初速度v0='); %子弹初速度      
t=0:0.1:3;                       %自变量

%--列出轨迹方程--
x1=v0*cosd(seita)*t;              %子弹的x方向的运动轨迹
y1=v0*sind(seita)*t-(1/2)*g*t.^2; %子弹的y方向的运动轨迹
x2=d;                             %猴子的x方向的运动轨迹
y2=h-(1/2)*g*t.^2;                %猴子的y方向的运动轨迹

%--做出动图--
for n=1:length(t)
    scatter(x1(n),y1(n))          
    hold on                                    %%保证两个函数同时出现
    text('String','枪口','Position',[0.0 0.0],'Color','r');%标出枪口
    set(gcf,'Color',[0.0,0.230,0.230]);        %设置背景颜色(森林)
    set(gca,'ycolor',[1 1 1],'xcolor',[1 1 1]);%把坐标轴设为白色否则看不清
    scatter(d,y2(n))              
    plot([0 d],[0 h])                          %辅助线
    plot([d d],[0,100])                        %方便观察
    axis equal                    
    axis([0 d+20 0 h])
    xlabel('x/m')
    ylabel('y/m')
    title('猎人打猴演示','Color','w','FontSize',13)
    legend('子弹','猴子')
    hold off                                   %%擦除这一次循环的圆圈(看起来物体像在运动)
    M(n)=getframe;
end
%movie2gif(M,'monkey','LoopCount',0,'DelayTime',0);
%--标出射杀点--
hold on
axis([0 d+20 0 h])
t0=d/(v0*cosd(seita));
y0=v0*sind(seita)*t0-(1/2)*g*t0^2;
scatter(d,y0,'r','filled')
lgd=legend('射杀点','TextColor','r');
title(lgd,'Poor monkey')

注释:
1.动图的关键,就是上面标注有%%的两行
2.有一些设置颜色的命令是我正在学习的GUI界面设计,可有可无

原版本1:

%shoot_monkey
%by TTY

%--参数设定及自变量取样--
g=9.8;                           %重力加速度
d=70;                            %猴子与猎人的距离 
h=20;                            %猴子距地面的高度
seita=input('瞄准猴子的仰角=');   %瞄准猴子的仰角
v0=30;                           %子弹初速度      
t=0:0.1:5;                       %自变量

%--列出轨迹方程--
x1=v0*cosd(seita)*t;              %子弹的运动轨迹x
y1=v0*sind(seita)*t-(1/2)*g*t.^2; %子弹的运动轨迹y
x2=d;                             %猴子的运动轨迹x
y2=h-(1/2)*g*t.^2;                %猴子的运动轨迹y

%-----作图-----
plot(x1,y1,'*b','MarkerSize',5);
axis([0 100 0 40]);
grid on;
hold on;
plot(x2,y2,'o','MarkerEdgeColor','c','MarkerFaceColor','y','MarkerSize',8)
xlabel('x/m')
ylabel('y/m')
title('猎人打猴')
legend('子弹','猴子')

在这里插入图片描述

动画版:)

%--将作图改为动图--
for n=1:length(t)
    scatter(x1(n),y1(n))          
    hold on                       %%保留画面
    scatter(d,y2(n)) 
    plot([0 100],[5 5])           %打中的大概纵坐标
    plot([70 70],[0,100])         %打中的大概横坐标(方便观察)
    axis equal                    
    axis([0 100 0 40])
    xlabel('x/m')
    ylabel('y/m')
    title('猎人打猴')
    legend('子弹','猴子')
    hold off                 %%擦除这一次循环的圆圈(看起来物体像在运动)
    M(n)=getframe;
end
movie2gif(M,'shoot_monkey','LoopCount',0,'DelayTime',0);

注意:

  1. 保存为gif动图,需要先到官网下载movie2gif函数
  2. 解压后将这个文件和shoot_monkey.m放在一个文件夹下,就可以看到“Generate GIF file”
  3. 可能会出现在这里插入图片描述重命名文件后缀加上.gif就好了在这里插入图片描述
  4. 反思:这个题目其实不能设置任意的仰角,因为要瞄准猴子仰角必须等于arctan(d/h).但可以设置任意的速度来验证结论“定能打中猴子”在什么条件下成立。

附上同学的作业

v0=20;t=1;h0=animatedline;h1=animatedline;h2=animatedline;
%for i=0:0.1:t
%    Y1=[Y1 v0*sin(pi/6)*i];
%    Y2=[Y2 v0*sin(pi/6)*i-5*i^2];
%    Y3=[Y3 10-5*i^2];
%    X=[X v0*cos(pi/6)*i];
%    X3=[X3 vo*cos(pi/6)*t];
%end
%%plot(X,Y1);hold on;plot(X,Y2);hold on;plot(X3,Y3);

h0=animatedline('Color','m','LineWidth',1.5);
h1=animatedline('Color','c','LineWidth',2.5,'LineStyle',':');
h2=animatedline('Color','g','LineWidth',2.5,'LineStyle',':');
axis([0 20 0 10]);xlabel('位置x');ylabel('位置y');
for i=0:0.002:t
    x=v0*cos(pi/6)*i;
    y1=v0*sin(pi/6)*i-5*i^2;
    y3=10-5*i^2;
    x3=v0*cos(pi/6)*t;
    addpoints(h0,x,y1);
    addpoints(h1,x,y2);
    addpoints(h2,x,y3);
    drawnow;
end

animatedline创建动画线条
addpoints
Create an animated line using the animatedline function. Then, add points to the line within a loop to create an animation. Set the axis limits before the loop to prevent the limits from changing.

在这里插入图片描述

clear clc
vx=10*sqrt(3);vy=10;g=9.8;
h=25/sqrt(3);
T=25/vx;
t=0:0.01:T;
x1=vx.*t;
x2=0.*t+25;
x3=vx.*t;
y1=vy.*t-(1/2).*g*t.^2;
y2=h-(1/2).*g*t.^2;
y3=vy.*t;
figure
for i=1:(length(x1)-1)
    axis([0 40 0 15])
    X1=x1(i:i+1);
    X2=x2(i:i+1);
    X3=x3(i:i+1);
    Y1=y1(i:i+1);
    Y2=y2(i:i+1);
    Y3=y3(i:i+1);
    plot(X1,Y1,'k','linewidth',1);
    hold on
    plot(X2,Y2,'g','linewidth',3);
    plot(X3,Y3,'b','linewidth',1);
    pause(0.01)
end

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值