MATLABにおいて、アニメーションを描く方法を紹介します。
細かい説明とかは、MATLABの
テクニカルヘルプページ
を参照してください。
% ===== ロジスティックマップのアニメーション ======
a = 4; % ロジスティックマップのパラメータ
x(1) = rand(1,1); % 初期値をランダムで決定
figure; % アニメーションをするfigureを開く
plot(0:1,0:1,'-','LineWidth',2);hold on % 真ん中の45°の青い線
% ======= アニメーションのための値を計算 =======
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); % 順番に並び替える
plot(y(:,1),y(:,2),'r-','LineWidth',2)
% ======= アニメーションの開始 =======
for t = 1:100
if t == 1;plot(xp(t:t+1,1),xp(t:t+1,2),'k-'); % 黒い線をアニメーションで描く
else;plot(xp(t-1:t+1,1),xp(t-1:t+1,2),'k-');end
title(sprintf('%d/%d training steps',t,100)) % タイトルに学習回数を表示
xlabel('x(t)'); ylabel('x(t+1)');
drawnow % drawnowを入れるとアニメーションになる
pause(0.2) % ゆっくりにするためにpauseで少し停止
end
|