function hysteresis()
% Demo of the hysteresis loop
% Filip Piekniewski 2009
ff=figure;
hold on;
N=20000;
tau=400/N;
tspan=0:tau:400;
X=-2:0.01:2;
x=-1;
u=0;
UU=zeros(N,1);
i=1;
for t=tspan
    x=x+tau*(-(x-1)*x*(x+1)-u);
    u=u+tau*(0.005*x);
    UU(i)=u;
    if (mod(t,10)==0)
        cla;
        plot(X,-(X-1).*X.*(X+1)-u,'linewidth',2);
        plot(x, 0,'or','markersize',10);
        plot([-2 2],[0 0],'k');
        axis([-2 2 -3 3]);
        drawnow;
        % Uncomment to print frames of the animation:
        % print(ff, '-r150', '-dpng', ['hysteresis', num2str(i),'.png']);  
        % Prints eps:
        % print(ff, '-depsc', ['hysteresis', num2str(i),'.eps']);        
    end; 
    i=i+1;
end;      
figure;
plot(tspan,UU);
