MATLABで、アニメーションの途中のfigureを表示させる方法を紹介します。
例として、ロジスティックマップのアニメーションを使って説明します。
% ===== ロジスティックマップのアニメーション ======
a = 4; % ロジスティックマップのパラメータ
x(1) = rand(1,1); % 初期値をランダムで決定
% ======= アニメーションのための値を計算 =======
y = []; xp = [];
for t = 1:100
x(t+1) = a * x(t)*(1-x(t)); % ロジスティックマップの計算
if t == 1
xp = [xp; x(t) 0; x(t) x(t+1)]; % xp : アニメーションの黒い線
else
xp = [xp; x(t) x(t); x(t) x(t+1)]; % xp : アニメーションの黒い線
end
y = [y; x(t) x(t+1)]; % マップの値
end
% ロジスティックマップを1つの線で描く
y = sortrows(y,1); % 順番に並び替える
% ======= アニメーションの開始 =======
step = [1 20 30 50 100]; % 表示させたいfigureのステップ
s = 1;
for t = 1:100
if t == step(s);
figure;
plot(0:1,0:1,'-','LineWidth',2);hold on % 真ん中の45°の青い線
plot(y(:,1),y(:,2),'r-','LineWidth',2); % ロジスティックマップの赤い線
if t == 1;plot(xp(t:t+1,1),xp(t:t+1,2),'k-'); % 黒い線をアニメーションで描く
else;plot(xp(1:t+1,1),xp(1:t+1,2),'k-');end
title(sprintf('%d/%d training steps',t,100)) % タイトルに学習回数を表示
xlabel('x(t)'); ylabel('x(t+1)');
s = s + 1; % sを+1する
end
end
|