2017-11-11 169 views
1

我正在嘗試想象一個我必須做的小型模擬。對於這項任務來說,這不是必要的,而是經常發生:在我繼續之前,我遇到了問題並想要解決方案。MatLab:更新兩個獨立的動畫人物,將重量和誤差可視化

該模擬是一個來自計算神經科學部門的'Oja'算法的超簡單神經網絡。我將該值繪製爲散點圖,並且想要在循環中改變重量的動畫。這本身工作正常(圖2或F2處理) 然後,我決定打印從一個運行到另一個計算爲標準的權重向量的差異。我希望將其作爲隨着時間的推移而變化的一條線(圖1又名f1)。但是,儘管我總是激活圖2,但它會切換回圖1並將其繪製在那裏。

當然,我搜索了互聯網以及stackexchange那裏順便說一句,我發現了很多有關動畫的迷人的東西。只是沒有什麼解決的問題......

兩個問題:

  1. 爲什麼呢?
  2. 以及我必須改變以使其工作?

謝謝。

下面是代碼:

function [t, w, dw]=weight(X) 

w=rand(2,1)*5; %Initialization 

%constants 
n=1; 
alpha=1; 
dt=0.01; 
T=5; 
L=length(X); 
w_old=[0; 0]; 
s=size(w_old) 
t=0; 
limit=0.001; 


%handles for the figures Error and weight animation 
f1= figure 
set(f1,'DoubleBuffer','on', 'Name','Error'); 
ax1 = axes('Position',[0.1 0.1 0.7 0.7]); 

f2=figure 
set(f2, 'Name', 'weights'); 

%normalizing and plot 
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))]; 
scatter(X(:,1),X(:,2)); 

%function handle for the error and the weights animation 
herror = animatedline('Marker','.'); 

hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ... 
    'Marker','o', 'MarkerSize',6, 'LineWidth',2); 
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ... 
    'Color',[0.2 0.2 0.2], 'FontSize',8, ... 
    'HorizontalAlignment','left', 'VerticalAlignment','top'); 

while (t<T) 
    for i=1:L 
     w_old= w; 
     u=X(i,:); 
     v=u*w; 
     w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule 
     figure(f2); 
     hold on; 
     set(hLine, 'XData',w(1), 'YData',w(2)) 
     set(hTxt, 'Position',[w(1) w(2)], ... 
     'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t]))   
     drawnow %# force refresh 
     %#pause(DELAY) 
     hold off; 
     dw=norm(w_old-w); 
     figure(f1) 
     hold on; 
     addpoints(herror, (i*t/dt),dw) 
     drawnow 
     hold off; 
     if dw<limit, break; end 
    end 
    t=t+dt; 

    if ~ishandle(hLine), break; end 
end 
end 

回答

2

您創建重疊的情節隱藏動畫新軸ax1。 在for循環之前的第一個數字中,f2=figure之後你也會插入herror = animatedline('Marker','.');。這是工作代碼:

function [t, w, dw]=weight(X) 
X = randn(20,2); 
w=rand(2,1)*5; %Initialization 

close all 

%constants 
n=1; 
alpha=1; 
dt=0.01; 
T=5; 
L=length(X); 
w_old=[0; 0]; 
s=size(w_old); 
t=0; 
limit=0.001; 


%handles for the figures Error and weight animation 
f1= figure(1); 
hold on; 
set(f1,'DoubleBuffer','on', 'Name','Error'); 
%ax1 = axes('Position',[0.1 0.1 0.7 0.7]); 
%function handle for the error and the weights animation 
herror = animatedline('Marker','.'); 
xlabel('t'); ylabel('Error'); 

f2=figure(2); 
hold on; 
set(f2, 'Name', 'weights'); 
xlabel('W1'); ylabel('W2') 

%normalizing and plot 
X=[X(:,1)-mean(X(:,1)),X(:,2)-mean(X(:,2))]; 
scatter(X(:,1),X(:,2)); 

hLine = line('XData',w(1), 'YData',w(2), 'Color','r', ... 
    'Marker','o', 'MarkerSize',6, 'LineWidth',2); 
hTxt = text(w(1), w(2), sprintf('(%.3f,%.3f)',w(1),w(2)), ... 
    'Color',[0.2 0.2 0.2], 'FontSize',8, ... 
    'HorizontalAlignment','left', 'VerticalAlignment','top'); 

while (t<T) 
    for i=1:L 
     w_old= w; 
     u=X(i,:); 
     v=u*w; 
     w=w+dt*n*(v*u'-alpha*v^2*w); %Oja rule 

     set(hLine, 'XData',w(1), 'YData',w(2)) 
     set(hTxt, 'Position',[w(1) w(2)], ... 
      'String',sprintf('(%.3f,%.3f,%.2f)',[w(1) w(2) t])) 
     drawnow %# force refresh 

     dw=norm(w_old-w); 
     figure(f1); 
     addpoints(herror, (i*t/dt),dw) 
     drawnow 
     if dw<limit; break; end 

    end 
    t=t+dt; 

    if ~ishandle(hLine), break; end 
end 
end 

對我來說,只使用一個窗口和2 subplots,而不是切換窗口來回看起來更自然。

+0

謝謝。只是爲了確認我是否正確:錯誤是由於我在宣佈數字1之後沒有做任何事情? –

+1

@AndreasK。是的,'ax1 =軸('位置',[0.1 0.1 0.7 0.7]);'沒有必要 – m3tho