2017-01-19 14 views
0

私は偏微分方程式の解のアニメーションGIFを生成したいと思います。つまり、gifは特定の時間に解決策を表示する必要があります。MATLABでアニメーションGIFを作成するにはどうすればよいですか?

example

現在、私はすべての時間をプロットした映像を作ることができます。 以下は私のプログラム全体です。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は明らかにアニメ化されていない

this

です。

注:この質問を投稿するのに適した場所があると思われる場合は、私に指示してください。私の問題はMATLABプログラミング言語であり、数学ではないので、これは私の質問を投稿するのに最適な場所だと思いました。

答えて

3

getframeの最初の入力は、スクリーンショットを撮るfigureのハンドルです。あなたが書いたように、数字を実際に参照しています。これは、作成した最初の図を参照していて、ループ内で更新していません。

あなたは、あなたの代わりにその数字を使用するようにgetframeをお伝えしたいと思いますので、あなたの最後のループの前に右図の作成にの数値ハンドルを割り当てました。

さらに、新しいプロットオブジェクトを連続的に作成するのではなく、1つのプロットオブジェクトを作成し、XDataおよびYDataを更新します。 plotを呼び出す問題は遅いです。 xとyのラベルとxとyの制限など、すべてのaxes設定を完全にリセットします。私の主な問題を解決する助け

% 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 

enter image description here

+0

。軸の寸法を(垂直方向に)固定し、軸にラベルを付けるにはどうすればよいですか? – AzJ

+0

@AzJこれを達成する方法を示すために更新されました – Suever

関連する問題