function varargout = Kapitza_inga(varargin)
% KAPITZA_INGA MATLAB code for Kapitza_inga.fig
%      KAPITZA_INGA, by itself, creates a new KAPITZA_INGA or raises the existing
%      singleton*.
%
%      H = KAPITZA_INGA returns the handle to a new KAPITZA_INGA or the handle to
%      the existing singleton*.
%
%      KAPITZA_INGA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in KAPITZA_INGA.M with the given input arguments.
%
%      KAPITZA_INGA('Property','Value',...) creates a new KAPITZA_INGA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Kapitza_inga_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Kapitza_inga_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Kapitza_inga

% Last Modified by GUIDE v2.5 13-Oct-2014 23:39:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Kapitza_inga_OpeningFcn, ...
                   'gui_OutputFcn',  @Kapitza_inga_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Kapitza_inga is made visible.
function Kapitza_inga_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Kapitza_inga (see VARARGIN)

global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;

x=-20:0.05:20;
y=-4:0.05:4;
[X,Y] = meshgrid(x,y); 
Z=0.5.*Y.^2-cos(X)+1;

Xinit=str2double(get(handles.xinit,'String'));
Yinit=str2double(get(handles.yinit,'String'));
Max_Time=str2double(get(handles.MaxTime,'String'));
wk=str2double(get(handles.frekvencia,'String'));
a=str2double(get(handles.aplitudo,'String'));
b=str2double(get(handles.csillapitas,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% Choose default command line output for Kapitza_inga
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Kapitza_inga wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Kapitza_inga_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function xinit_Callback(hObject, eventdata, handles)
% hObject    handle to xinit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xinit as text
%        str2double(get(hObject,'String')) returns contents of xinit as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
Xinit=str2double(get(handles.xinit,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function xinit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xinit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function yinit_Callback(hObject, eventdata, handles)
% hObject    handle to yinit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of yinit as text
%        str2double(get(hObject,'String')) returns contents of yinit as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
Yinit=str2double(get(handles.yinit,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function yinit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yinit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function MaxTime_Callback(hObject, eventdata, handles)
% hObject    handle to MaxTime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of MaxTime as text
%        str2double(get(hObject,'String')) returns contents of MaxTime as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
Max_Time=str2double(get(handles.MaxTime,'String'));
set(handles.idokiir, 'String', ['Time=[0, ', num2str(Max_Time), ']']);
set(handles.idocsuszka, 'Value', Max_Time);

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function MaxTime_CreateFcn(hObject, eventdata, handles)
% hObject    handle to MaxTime (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function aplitudo_Callback(hObject, eventdata, handles)
% hObject    handle to aplitudo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of aplitudo as text
%        str2double(get(hObject,'String')) returns contents of aplitudo as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
a=str2double(get(handles.aplitudo,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function aplitudo_CreateFcn(hObject, eventdata, handles)
% hObject    handle to aplitudo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function frekvencia_Callback(hObject, eventdata, handles)
% hObject    handle to frekvencia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of frekvencia as text
%        str2double(get(hObject,'String')) returns contents of frekvencia as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
wk=str2double(get(handles.frekvencia,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function frekvencia_CreateFcn(hObject, eventdata, handles)
% hObject    handle to frekvencia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function csillapitas_Callback(hObject, eventdata, handles)
% hObject    handle to csillapitas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of csillapitas as text
%        str2double(get(hObject,'String')) returns contents of csillapitas as a double
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
b=str2double(get(handles.csillapitas,'String'));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function csillapitas_CreateFcn(hObject, eventdata, handles)
% hObject    handle to csillapitas (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on slider movement.
function idocsuszka_Callback(hObject, eventdata, handles)
% hObject    handle to idocsuszka (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
Max_Time=get(handles.idocsuszka,'Value');
set(handles.idokiir, 'String', ['Time=[0, ', num2str(Max_Time), ']']);
set(handles.MaxTime, 'String', num2str(Max_Time));

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


% --- Executes during object creation, after setting all properties.
function idocsuszka_CreateFcn(hObject, eventdata, handles)
% hObject    handle to idocsuszka (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in resetgomb.
function resetgomb_Callback(hObject, eventdata, handles)
% hObject    handle to resetgomb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global Xinit;
global Yinit;
global Max_Time;
global wk;
global a;
global b;
global X;
global Y;
global Z;
Xinit=-3;
Yinit=0.5;
Max_Time=100;
wk=1;
a=0;
b=0;
set(handles.xinit, 'String', num2str(Xinit));
set(handles.yinit, 'String', num2str(Yinit));
set(handles.MaxTime, 'String', num2str(Max_Time));
set(handles.frekvencia, 'String', num2str(wk));
set(handles.aplitudo, 'String', num2str(a));
set(handles.csillapitas, 'String', num2str(b));
set(handles.idokiir, 'String', ['Time=[0, ', num2str(Max_Time), ']']);
set(handles.idocsuszka, 'Value', Max_Time);

rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles);


function rajzol(Xinit, Yinit, Max_Time, wk, a, b, X, Y, Z, handles)
egyenlet=@(t, xy) [xy(2); -b*xy(2)-sin(xy(1))+a*cos(wk*t)*sin(xy(1))];

if (~isnan(Xinit) && ~isnan(Yinit) && ~isnan(Max_Time) && Max_Time~=0 && ~isnan(wk) && ~isnan(a) && ~isnan(b)) 
[t, xy]=ode45(egyenlet, [0, Max_Time], [Xinit; Yinit]); %diffegyenletet kiszámol

plot(handles.abra, xy(:,1), xy(:,2), 'b'); %x-y ábrázolás
hold on;
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(handles.abra, X,Y,Z, energiaszintek); %energia szintvonalak ábrázolása
xlabel(handles.abra, 'x');
ylabel(handles.abra, 'y');
hold off;

else
    disp('Érvénytelen bemenet.');
end
