matlab模拟飞机延时gif动图程序

clear
clc
%飞机追踪潜艇航线图
t=linspace(0,1,2000); flag=0;
p=zeros(2000,2);q=ones(2000,2);a=zeros(8,2);b=zeros(8,2);
a(:,1)=0;a(1,2)=6;b(1,2)=0;b(1,1)=pi/2;
for i=1:2000
    theta=sqrt(3)*log(10*t(i)+1);
    p(i,1)=2*exp(sqrt(3)/3*theta); p(i,2)=theta;%飞机的极坐标
    q(i,1)=2+20*t(i); q(i,2)=pi/2;%潜艇的极坐标
    if abs(p(i,1)-q(i,1))<0.01&&abs(p(i,2)-q(i,2))<0.01%判断飞机是否已经找到潜艇
        flag=1;
        break;
    end
end    
if flag==1%判断飞机是否找到潜艇
    disp('已经找到该轨迹!');
    h = polar([0 2*pi], [-6 0],'k.');
    text(2,0,'(2,0)');text(6,0,'A(6,0)');
    text(0,2,'(0,2)');text(0,0,'(0,0)');
    title('飞机追踪潜艇轨迹图');
    hold on;
    n=1;
    im=frame2im(getframe);
    [imind,cm]=rgb2ind(im,256);
    imwrite(imind,cm,'追踪轨迹图.GIF','gif','Loopcount',inf,'DelayTime',0.1);

    for j=2:i
        a(j,2)=a(j-1,2)-4/i;b(j,2)=b(j-1,2)+2/i;b(j,1)=pi/2;
    end
    for j=1:i
        polar(a(j,1),a(j,2),'r*');
        polar(b(j,1),b(j,2),'b*');        
        for k=1:8
            if j==fix(k*i/8)
                n=n+1;
                im=frame2im(getframe);
                [imind,cm]=rgb2ind(im,256);
                imwrite(imind,cm,'追踪轨迹图.GIF','gif','WriteMode','append','DelayTime',0.1); 
            end
        end
    end

    for j=1:i
        polar(p(j,2),p(j,1),'r*');
        polar(q(j,2),q(j,1),'b*');
        for k=1:8
            if j==fix(k*i/8)
                n=n+1;
                im=frame2im(getframe);
                [imind,cm]=rgb2ind(im,256);
                imwrite(imind,cm,'追踪轨迹图.GIF','gif','WriteMode','append','DelayTime',0.1); 
            end
        end
    end
else
    disp('无解');
end

matlab模拟飞机延时gif动图程序_第1张图片

这是当年的一道建模题目,主要在程序中学习一些需要的技巧而已,没必要深究。

你可能感兴趣的:(matlab模拟飞机延时gif动图程序)