【问题标题】:How do i produce an animated GIF in MATLAB?如何在 MATLAB 中制作动画 GIF?
【发布时间】:2017-01-19 21:17:18
【问题描述】:

我想制作一个关于偏微分方程解的动画 gif。那就是gif应该在特定时间显示解决方案。

目前我只能制作所有时间都被绘制的图片。 以下是我的整个程序,figure(3) 是我制作 gif 的尝试。

clear all;
close all;
%%%%%%%%%%%%
% For slide 27 of Diffusion 1D
% The equation to be graphed in latex form is
% u(x,t)=\frac{1}{L}+\frac{2}{L}\sum^{\infty}_{n=1}cos(\frac{n\pi x_0}{L})cos(\frac{n\pi x}{L})e^{-k(\frac{n\pi}{L})^2t}
%%%%%%%%%%%%

%define constants
%note that the constants listed in the file are arbitrary
L = 2; %length of the rod
k= 0.01; % Diffusivity, which is assume to be constant but can be a function of x
x0 = 1; %location of the inital condition i.e. f(x)=delta(x-x0)
tmax= 50; %maximum amount of time the simulation runs
nmax = 200; % maximum value for n, increase to accuracy
tgrid = 21; %The number of points to be evaluated in the time domain
xgrid = 51; %The number of points to be evaluated in the space domain

%initialize variables
u=zeros(tgrid,xgrid); %preallocate array used for storing values of the solution
t=linspace(0,tmax,tgrid);%We assume that time is evenly distributed
x=linspace(0,L,xgrid); %We assume that space is evenly distributed

%Plotting variables
figure(1);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
%Calculation,
for i=1:tgrid
    for j=1:xgrid
        seriesSum=0;
        %Calculate the fourier series up to nmax for each point u(x,t)
        for n= 1:nmax
            seriesSum= seriesSum + cos(n*pi*x0/L)*cos(n*pi*x(j)/L)*exp(-k*t(i)*(n*pi/L)^2);
        end
        %Finish calcuation for solution at a specific point
        u(i,j)= 1/L+(2/L)*seriesSum;
    end
    %After we have calculated all points at time t, we graph it for time t
    plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol.png')%Save figure as png in current directory

%run a second loop that does not include the initial condition to get a
%better view of the long term behaviour.
%Plotting variables
figure(2);
hold on;
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
for i=2:tgrid
    plot(x,u(i,:),'linewidth',4);
end
saveas(gcf,'PDE_sol_without_inital.png')%Save figure as png in current directory

%Create a gif verison of figure 2
figure(3);
axis([0 L -inf inf]);
xlabel('x');
ylabel('u(x,t)');
filename = 'PDE_sol.gif';
for i=2:tgrid
    plot(x,u(i,:),'linewidth',4);
    drawnow
    frame = getframe(1);
    im = frame2im(frame);
    [imind,cm] = rgb2ind(im,256);

    if i == 2;
        imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
    else
        imwrite(imind,cm,filename,'gif','WriteMode','append');
    end
end

我得到的输出 gif 是

这显然不是动画。

注意:如果您认为有更好的地方可以发布此问题,请指导我。由于我的问题是 MATLAB 编程语言而不是所涉及的数学,我认为这是发布我的问题的最佳地点。

【问题讨论】:

    标签: matlab matlab-figure pde


    【解决方案1】:

    getframe 的第一个输入是您要截屏的figure 的句柄。正如您所写的那样,您正在抓取图 1,它实际上是指您创建的第一个图,您没有在循环中更新。

    您已为您在最后一个循环之前创建的图形分配了一个数字句柄 3,因此您需要告诉 getframe 使用 那个 图形而是。

    此外,我会创建一个绘图对象并更新XDataYData,而不是不断创建新的绘图对象。连续调用plot 的问题在于它很慢并且它会完全重置所有axes 设置,例如x 和y 标签以及x 和y 限制。

    % Store the handle to the figure in hfig
    hfig = figure(3);
    
    % Create the initial plot object
    hplot = plot(NaN, NaN, 'LineWidth', 4);
    
    axis([0 L 0 2]);
    xlabel('x');
    ylabel('u(x,t)');
    
    filename = 'PDE_sol.gif';
    
    for i=2:tgrid
        % Update the plot appearance
        set(hplot, 'XData', x, 'YData', u(i,:));
        drawnow
    
        % Get a screenshot of THIS figure
        frame = getframe(hfig);
        im = frame2im(frame);
    
        [imind,cm] = rgb2ind(im,256);
    
        if i == 2;
            imwrite(imind,cm,filename,'gif', 'Loopcount',inf);
        else
            imwrite(imind,cm,filename,'gif','WriteMode','append');
        end
    end
    

    【讨论】:

    • 这有助于解决我的主要问题。如何固定轴尺寸(垂直)并标记轴?
    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2021-07-19
    • 2020-06-02
    • 2012-06-07
    • 2019-05-24
    • 2014-04-10
    • 1970-01-01
    相关资源
    最近更新 更多