%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         1.gyakorlat         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Nemlin. din. rsz-ek alapjai %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        2014.09.16/17        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Paraméterek:
Xinit= -9;                 % x kezdeti értéke       %
Yinit= 2;                  % y kezdeti értéke       %
tinit=0;                   % kezdeti idõ            % EZEKET A PARAMÉTEREKET ÁLLÍTHATJUK
Max_Time= 500;             % szimuláció ideje       % AZ ÜGYESEK GUI-T IS ÍRHATNAK HOZZÁ
b=0.1;                     % csillapítás            %
 
x =-10:0.05:10;            % Lehet módosítani, de felesleges
y =-4:0.05:4;              % Lehet módosítani, de felesleges
[X,Y] = meshgrid(x,y); %hálót készít x,y vektorok alapján (az energiák kiszámításához az adott pontokban)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          Feladatok          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         1. egyenlet         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

egyenlet_1=@(t, xy) [xy(2); -xy(1)]; % LC kör, alap rugó egyenlet, stabil egyensúlyi pálya

energia_rugo=@(X,Y) 0.5.*Y.^2+0.5*X.^2;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         2.egyenlet          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

egyenlet_2=@(t, xy) [xy(2); -b*xy(2)-xy(1)]; %RLC kör, rugó csillapítás, lecseng


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         3.egyenlet          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

egyenlet_3=@(t, xy) [xy(2); -xy(1)+cos(t)]; %RC kör vagy rugó gerjesztéssel, elszáll


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         4.egyenlet          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

w=0.5; %w=1.5;

egyenlet_4=@(t, xy) [xy(2); -xy(1)+cos(w*t)]; %más gerjesztés, rezonáns rugó?, összetett periódus



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         5.egyenlet          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

egyenlet_5=@(t, xy) [xy(2); -2*xy(1)+cos(t)]; % rugó, összetett periódus

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         6-10.egyenlet          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


egyenlet_6=@(t, xy) [xy(2); -b*xy(2)-xy(1)+cos(t)];     % RLC kör vagy csillapított rugó gerjesztéssel, stabil priodikus pályára áll be tranziens után

egyenlet_7=@(t, xy) [xy(2); -sin(xy(1))];               %inga (hajóhinta) alap egyenlet
energia_inga=@(X,Y) 0.5.*Y.^2-cos(X)+1;

egyenlet_8=@(t, xy) [xy(2); -b*xy(2)-sin(xy(1))];       % inga csillapítással, lecseng

% 2. pont, ha kell
Xinit2= -0.2007;
Yinit2= 0.3;
egyenlet_9=@(t, xy) [xy(2); -sin(xy(1))-b*xy(2)+cos(t)]; %inga gerjesztéssel és csillapítással, káosz, érzékeny függés a kezdeti értékektõl 


Z = energia_inga(X,Y);                                  %energiát kiszámol, Z-ben tárol

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%             ODE45              %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[t, xy]=ode45(egyenlet_9, [tinit Max_Time], [Xinit; Yinit]); %diffegyenletet kiszámol
%[t2, xy2]=ode45(egyenlet_7, [tinit Max_Time], [Xinit2; Yinit2]);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         Kirajzoltatás          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      x-y szerint, energia      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure;
subplot(2,2,1);                                             % ábrák elhelyezése egy mezõn belül, számolás balról jobbra, fentrõl le
hold on;                                                    % több ábra egymásra
energiaszintek=[0, 0.1 0.25, 0.5, 1, 1.4, 2, 2.4, 3, 4];    % ezeken a szinteken számolom ki az energiát
contour(X,Y,Z, energiaszintek);                             % energia szintvonalak ábrázolása
plot(xy(:,1), xy(:,2), 'b');                                % x-y ábrázolás
%plot(xy2(:,1), xy2(:,2), 'r');                             % a 2. ponthoz
xlabel('x');
ylabel('y');
hold off;                                                   % ez után már felülír ábrát

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           x-t szerint          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(2,2,3);
%hold on;                                                   % a 2. ponthoz
plot(xy(:,1), t, 'b');                                      % x-t ábrázolás
%plot(xy2(:,1), t2, 'r');                                   % a 2. ponthoz
xlabel('x');
ylabel('t');
%hold off;                                                  % a 2. ponthoz

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           t-y szerint          %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(2,2,2);
%hold on;
plot(t, xy(:,2), 'b');                                      % t-y ábrázolás
%plot(t2, xy2(:,2), 'r');
xlabel('t');
ylabel('y');
%hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%       x-y-t 3D-s ábrázolás     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(2,2,4);
%hold on;
plot3(xy(:,1), xy(:,2), t, 'b');                            % x-y-t 3D-s ábrázolás
%plot3(xy2(:,1), xy2(:,2), t2, 'r');
xlabel('x');
ylabel('y');
zlabel('t');
%hold off,
