MatLab:更新两个独立的动画人物,将重量和误差可视化

MatLab:更新两个独立的动画人物,将重量和误差可视化

问题描述:

我正在尝试想象一个我必须做的小型模拟。对于这项任务来说,这不是必要的,而是经常发生:在我继续之前,我遇到了问题并想要解决方案。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 

您创建重叠的情节隐藏动画新轴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