% CHUA SZINKRONIZÁCIÓ
% BALADTA, 2012

clear all

%%%%%%%% PARAMÉTEREK %%%%%%%%
sysdim = length(chua_de);
% y0 = 0.1*ones(sysdim,1);
y0 = [-0.6 0  0.3 0.3 0.2 -0.7];
alpha = 10.5; % 9.5-tõl káosz 
beta = 15; 
g1 = 8/7; 
g2 = 3/64;
gcs = 0.5; % csatolási konduktancia alpha 10.5 esetén, gcs 0.5-tel néha szinkronizálódik, néha újra széteseik
params = [alpha beta g1 g2 gcs];
dT = 0.1; % lépésköz
refresh_T = 0.5; % ennyit oldjunk meg egyszerre: refresh_T/dT
hist_len_f = 100; % ennyi látszódjon a múltból: refresh_T/dt*hist_len_f adat
N = 1500; % hány ietráción keresztül menjen a szimuláció: N*refresh_T/dt adatpont
Y = nan(refresh_T/dT*hist_len_f+1,sysdim); 
% Y(:,1) = y0;
t = nan(refresh_T/dT*hist_len_f+1,1);

% diff. egyenket megoldó beállításai
options = odeset('RelTol',1e-3,'AbsTol',1e-6); % defaults
% options = odeset('RelTol',1e-6,'AbsTol',1e-7);


%%
%%%%%%%% SZÁMOLÁS&ÁBRÁZOLÁS %%%%%%%%
doimgwrite = false; % írjuk ki az animáció? (lassít)
figure;
set(gcf,'Color','w')
[t(1:refresh_T/dT+1),Y(1:refresh_T/dT+1,:)] = ode45(@(t,y) chua_de(t,y,params),0:dT:refresh_T,y0,options);

% Chua 1
subplot(7,2,1)
hcs11 = plot(t,Y(:,1));
ylabel('x_1(t)')
subplot(7,2,3)
hcs12 = plot(t,Y(:,2));
ylabel('y_1(t)')
subplot(7,2,5)
hcs13 = plot(t,Y(:,3));
xlabel('Time'); ylabel('z_1(t)')

% Chua 2
subplot(7,2,2)
hcs21 = plot(t,Y(:,4));
ylabel('x_2(t)')
subplot(7,2,4)
hcs22 = plot(t,Y(:,5));
ylabel('y_2(t)')
subplot(7,2,6)
hcs23 = plot(t,Y(:,6));
xlabel('Time'); ylabel('z_2(t)')

% Fázis
% subplot(7,2,7:10)
% hph = plot(Y(:,1),Y(:,2),'b',Y(:,4),Y(:,5),'r');
subplot(7,2,[7 9])
hph(1) = plot(Y(:,1),Y(:,2),'b');
axis([-3 3 -.45 .45])
% hph(1) = plot3(Y(:,1),Y(:,2),Y(:,3),'b');
xlabel('x_1(t)'); ylabel('y_1(t)')
subplot(7,2,[8 10])
hph(2) = plot(Y(:,4),Y(:,5),'b');
axis([-3 3 -.45 .45])
xlabel('x_2(t)'); ylabel('y_2(t)')

% Szinkronizáció
% subplot(7,2,11:14)
% hsyn = plot(Y(:,1),Y(:,4),'b',Y(:,2),Y(:,5),'r');
subplot(7,2,[11 13])
plot([-3 3],[-3 3],'k:'); hold on;
hsyn(1) = plot(Y(:,1),Y(:,4),'b'); hold off
xlabel('x_1(t)'); ylabel('x_2(t)')
subplot(7,2,[12 14])
plot([-.5 .5],[-.5 .5],'k:'); hold on;
hsyn(2) = plot(Y(:,2),Y(:,5),'b'); hold off
xlabel('y_1(t)'); ylabel('y_2(t)')

title_str = sprintf('Coupled Chua Systems\nCoupling conductance: %0.2f',gcs);
hs = annotation('textbox',[0.2 0.94 0.6 0.054],'String',title_str,'HorizontalAlignment','center','LineStyle','none');
pos = get(gcf, 'Position'); 
sc = 1.5; % 1.5
set(gcf,'Position',[pos(1)*0.25 pos(2)*0.25 pos(3)*sc pos(4)*sc])

if doimgwrite
    first = 0;
    frame = getframe(gcf); 
    [mov, cmap] = rgb2ind(frame.cdata, 256, 'nodither'); 
    % mov = rgb2ind(frame.cdata, cmap, 'nodither'); 
    for k = 1:first
        mov(:,:,1,1+k) = mov(:,:,1,1);
    end
    imwrite(mov, cmap, 'chua_animation.gif', 'DelayTime', 0.05, 'LoopCount', inf);
else
    % waitforbuttonpress
    pause(0.01)
end

tic
index = 1;
for n = 1:N
    % index = mod( (n-1),hist_len_f );
    % index = min( index*refresh_T/dT+1 , length(t)-refresh_T/dT );
    if index < length(t)-refresh_T/dT
        index = index+refresh_T/dT;
        set(get(hcs11,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
        set(get(hcs12,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
        set(get(hcs13,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
        set(get(hcs21,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
        set(get(hcs22,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
        set(get(hcs23,'Parent'),'XLim',[t(1) (n+1)*refresh_T])
    else
        t(1:length(t)-refresh_T/dT) = t(1+refresh_T/dT:length(t));
        Y(1:length(t)-refresh_T/dT,:) = Y(1+refresh_T/dT:length(t),:);
        set(get(hcs11,'Parent'),'XLim',[t(1) t(end)+refresh_T])
        set(get(hcs12,'Parent'),'XLim',[t(1) t(end)+refresh_T])
        set(get(hcs13,'Parent'),'XLim',[t(1) t(end)+refresh_T])
        set(get(hcs21,'Parent'),'XLim',[t(1) t(end)+refresh_T])
        set(get(hcs22,'Parent'),'XLim',[t(1) t(end)+refresh_T])
        set(get(hcs23,'Parent'),'XLim',[t(1) t(end)+refresh_T])
    end
    [t(index:index+refresh_T/dT),Y(index:index+refresh_T/dT,:)] = ...
        ode45(@(t,y) chua_de(t,y,params),n*refresh_T:dT:(n+1)*refresh_T,Y(index,:),options);
    % Chua 1
    set(hcs11,'XData',t,'YData',Y(:,1));
    set(hcs12,'XData',t,'YData',Y(:,2));
    set(hcs13,'XData',t,'YData',Y(:,3));
    % Chua 2
    set(hcs21,'XData',t,'YData',Y(:,4));
    set(hcs22,'XData',t,'YData',Y(:,5));
    set(hcs23,'XData',t,'YData',Y(:,6));
    % Fázis
    set(hph(1),'XData',Y(:,1),'YData',Y(:,2));
    % set(hph(1),'XData',Y(:,1),'YData',Y(:,2),'ZData',Y(:,3));
    set(hph(2),'XData',Y(:,4),'YData',Y(:,5));
    % Szinkronizáció
    set(hsyn(1),'XData',Y(:,1),'YData',Y(:,4))
    set(hsyn(2),'XData',Y(:,2),'YData',Y(:,5))
    
    if doimgwrite
        frame = getframe(gcf); 
        mov = rgb2ind(frame.cdata, cmap, 'nodither');
        imwrite(mov, cmap, 'chua_animation.gif', 'WriteMode', 'append', 'DelayTime', 0.05);
    else
        drawnow
        % waitforbuttonpress
        % pause(0.01)
    end
end
toc

if doimgwrite
    last = 5;
    for k = 1:last
        imwrite(mov, cmap, 'chua_animation.gif','WriteMode', 'append', 'DelayTime', 0.05);
    end
end
