function varargout = pendulumGUI(varargin)
% PENDULUMGUI MATLAB code for pendulumGUI.fig
%      PENDULUMGUI, by itself, creates a new PENDULUMGUI or raises the existing
%      singleton*.
%
%      H = PENDULUMGUI returns the handle to a new PENDULUMGUI or the handle to
%      the existing singleton*.
%
%      PENDULUMGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PENDULUMGUI.M with the given input arguments.
%
%      PENDULUMGUI('Property','Value',...) creates a new PENDULUMGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pendulumGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pendulumGUI_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 pendulumGUI

% Last Modified by GUIDE v2.5 16-Sep-2014 11:03:28

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pendulumGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @pendulumGUI_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 pendulumGUI is made visible.
function pendulumGUI_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 pendulumGUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

tinit=0; %kezdeti idõ

global Xinit;
global Yinit;
global Max_Time;
global b;
global wk;
global wb;
global X;
global Y;
global Z;
global contents;
global egyenlet_be;
global energia_be;


x=-10:0.05:10;
y=-4:0.05:4;
[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)
 
%GUI-ról beveszem az adatokat
 Xinit=str2double(cellstr(get(handles.kezdx,'String'))); %szöveget beveszem az ablakból és számmá alakítom
 Yinit=str2double(cellstr(get(handles.kezdy,'String')));
 Max_Time=str2double(get(handles.tmax,'String'));
 b=str2double(cellstr(get(handles.csillapit,'String')));
 wk=str2double(cellstr(get(handles.rezonans_wk,'String')));
 wb=str2double(cellstr(get(handles.rezonans_wb,'String')));
 
 contents = cellstr(get(handles.egyenletvalaszto,'String')); %szöveget beveszem az ablakból
 egyenlet_be=contents{get(handles.egyenletvalaszto,'Value')}; %adott sorát nézem
 
 contents = cellstr(get(handles.energiavalaszto,'String'));
 energia_be=contents{get(handles.energiavalaszto,'Value')};

rajzol(Xinit,Yinit,Max_Time,b,wk,wb,X,Y,Z,contents,egyenlet_be,energia_be,handles);

% --- Outputs from this function are returned to the command line.
function varargout = pendulumGUI_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;


% --- Executes on selection change in egyenletvalaszto.
function egyenletvalaszto_Callback(hObject, eventdata, handles)
% hObject    handle to egyenletvalaszto (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns egyenletvalaszto contents as cell array
%        contents{get(hObject,'Value')} returns selected item from egyenletvalaszto


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

% Hint: popupmenu 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 selection change in energiavalaszto.
function energiavalaszto_Callback(hObject, eventdata, handles)
% hObject    handle to energiavalaszto (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns energiavalaszto contents as cell array
%        contents{get(hObject,'Value')} returns selected item from energiavalaszto


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

% Hint: popupmenu 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 kezdx_Callback(hObject, eventdata, handles)
% hObject    handle to kezdx (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 kezdx as text
%        str2double(get(hObject,'String')) returns contents of kezdx as a double
tinit=0; %kezdeti idõ

global Xinit;
global Yinit;
global Max_Time;
global b;
global wk;
global wb;
global X;
global Y;
global Z;
global contents;
global egyenlet_be;
global energia_be;


x=-10:0.05:10;
y=-4:0.05:4;
[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)
Xinit=str2double((get(handles.kezdx,'String')));
%Xinit=str2double(cellstr(get(handles.kezdx,'String')));
%Xinit=get(handles.kezdx,'String');
if (~isnan(Xinit))
    rajzol(Xinit,Yinit,Max_Time,b,wk,wb,X,Y,Z,contents,egyenlet_be,energia_be,handles);
else
    disp('Érvénytelen bemenet.');
end

% --- Executes during object creation, after setting all properties.
function kezdx_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kezdx (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 kezdy_Callback(hObject, eventdata, handles)
% hObject    handle to kezdy (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 kezdy as text
%        str2double(get(hObject,'String')) returns contents of kezdy as a double


% --- Executes during object creation, after setting all properties.
function kezdy_CreateFcn(hObject, eventdata, handles)
% hObject    handle to kezdy (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 csillapit_Callback(hObject, eventdata, handles)
% hObject    handle to csillapit (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 csillapit as text
%        str2double(get(hObject,'String')) returns contents of csillapit as a double


% --- Executes during object creation, after setting all properties.
function csillapit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to csillapit (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 rezonans_wk_Callback(hObject, eventdata, handles)
% hObject    handle to rezonans_wk (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 rezonans_wk as text
%        str2double(get(hObject,'String')) returns contents of rezonans_wk as a double


% --- Executes during object creation, after setting all properties.
function rezonans_wk_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rezonans_wk (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 tmax_Callback(hObject, eventdata, handles)
% hObject    handle to tmax (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 tmax as text
%        str2double(get(hObject,'String')) returns contents of tmax as a double


% --- Executes during object creation, after setting all properties.
function tmax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tmax (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 button press in rajzol.
function rajzol_Callback(hObject, eventdata, handles)
% hObject    handle to rajzol (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%alap paraméterek:
tinit=0; %kezdeti idõ

global Xinit;
global Yinit;
global Max_Time;
global b;
global wk;
global wb;
global X;
global Y;
global Z;
global contents;
global egyenlet_be;



x=-10:0.05:10;
y=-4:0.05:4;
[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)
 
%GUI-ról beveszem az adatokat
 Xinit=str2double(cellstr(get(handles.kezdx,'String'))); %szöveget beveszem az ablakból és számmá alakítom
 Yinit=str2double(cellstr(get(handles.kezdy,'String')));
 Max_Time=str2double(get(handles.tmax,'String'));
 b=str2double(cellstr(get(handles.csillapit,'String')));
 wk=str2double(cellstr(get(handles.rezonans_wk,'String')));
 wb=str2double(cellstr(get(handles.rezonans_wb,'String')));
 
 contents = cellstr(get(handles.egyenletvalaszto,'String')); %szöveget beveszem az ablakból
 egyenlet_be=contents{get(handles.egyenletvalaszto,'Value')}; %adott sorát nézem
 
 contents = cellstr(get(handles.energiavalaszto,'String'));
 energia_be=contents{get(handles.energiavalaszto,'Value')};
 
%egyenletek: melyiket jelöltem a GUI-n, attól függ melyik diffegyenletet számolom
if strcmp(egyenlet_be, 'ddx+x=0')
    egyenlet=@(t, xy) [xy(2); -xy(1)]; % LC kör, alap rugó egyenlet, stabil egyensúlyi pálya
elseif strcmp(egyenlet_be, 'ddx+b*dx+x=0')
    egyenlet=@(t, xy) [xy(2); -b*xy(2)-xy(1)]; %RLC kör, rugó csillapítás, lecseng
elseif strcmp(egyenlet_be, 'ddx+x=cos(wk*t)')
    egyenlet=@(t, xy) [xy(2); -xy(1)+cos(wk*t)]; %más gerjesztés, rezonáns rugó?, összetett periódus
elseif strcmp(egyenlet_be, 'ddx+wb*x=cos(t)')
    egyenlet=@(t, xy) [xy(2); -wb*xy(1)+cos(t)]; % rugó ???, összetett periódus
elseif strcmp(egyenlet_be, 'ddx+b*dx+x=cos(t)')
    egyenlet=@(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
elseif strcmp(egyenlet_be, 'ddx+sin(x)=0')
    egyenlet=@(t, xy) [xy(2); -sin(xy(1))]; %inga (hajóhinta) alap egyenlet
elseif strcmp(egyenlet_be, 'ddx+b*dx+sin(x)=0')
    egyenlet=@(t, xy) [xy(2); -b*xy(2)-sin(xy(1))]; % inga csillapítással, lecseng
elseif strcmp(egyenlet_be, 'ddx+b*dx+sin(x)=cos(t)')
    egyenlet=@(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
end
[t, xy]=ode45(egyenlet, [tinit Max_Time], [Xinit; Yinit]); %diffegy megold az adott egyenlettel

%ábrázolás
plot(handles.xyplot, xy(:,1), xy(:,2), 'b'); %x-y ábrázolás
hold(handles.xyplot, 'on');
if ~strcmp(energia_be, 'nincs')  %kiválasztom, melyik energia függvényt használom
  if strcmp(energia_be, 'rugo')
    Z= 0.5.*Y.^2+0.5*X.^2;
  elseif strcmp(energia_be, 'inga')
    Z= 0.5.*Y.^2-cos(X)+1;
  end
  energiaszintek=[0, 0.1 0.25, 0.5, 1, 1.4, 2, 2.4, 3, 4]; %ezeket az energia szintvonalakat ábrázolom
  contour(handles.xyplot, X,Y,Z, energiaszintek); %energia szintvonalak
end
xlabel(handles.xyplot, 'x');
ylabel(handles.xyplot, 'y');
hold(handles.xyplot, 'off');

plot(handles.typlot, t, xy(:,2), 'b'); %x-t ábrázolás
xlabel(handles.typlot,'t');
ylabel(handles.typlot,'y');

plot(handles.xtplot, xy(:,1), t, 'b'); %t-y ábrázolás
xlabel(handles.xtplot,'x');
ylabel(handles.xtplot,'t');

plot3(handles.xytplot, xy(:,1), xy(:,2), t, 'b'); %x-y-t ábrázolás
xlabel(handles.xytplot,'x');
ylabel(handles.xytplot,'y');
zlabel(handles.xytplot,'t');



function rezonans_wb_Callback(hObject, eventdata, handles)
% hObject    handle to rezonans_wb (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 rezonans_wb as text
%        str2double(get(hObject,'String')) returns contents of rezonans_wb as a double


% --- Executes during object creation, after setting all properties.
function rezonans_wb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rezonans_wb (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 rajzol(Xinit,Yinit,Max_Time,b,wk,wb,X,Y,Z,contents,egyenlet_be,energia_be,handles)
tinit = 0;
% x=-10:0.05:10;
% y=-4:0.05:4;
% [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)
%  
% %GUI-ról beveszem az adatokat
%  Xinit=str2double(cellstr(get(handles.kezdx,'String'))); %szöveget beveszem az ablakból és számmá alakítom
%  Yinit=str2double(cellstr(get(handles.kezdy,'String')));
%  Max_Time=str2double(get(handles.tmax,'String'));
%  b=str2double(cellstr(get(handles.csillapit,'String')));
%  wk=str2double(cellstr(get(handles.rezonans_wk,'String')));
%  wb=str2double(cellstr(get(handles.rezonans_wb,'String')));
%  
%  contents = cellstr(get(handles.egyenletvalaszto,'String')); %szöveget beveszem az ablakból
%  egyenlet_be=contents{get(handles.egyenletvalaszto,'Value')}; %adott sorát nézem
%  
%  contents = cellstr(get(handles.energiavalaszto,'String'));
%  energia_be=contents{get(handles.energiavalaszto,'Value')};
%  
%egyenletek: melyiket jelöltem a GUI-n, attól függ melyik diffegyenletet számolom
if strcmp(egyenlet_be, 'ddx+x=0')
    egyenlet=@(t, xy) [xy(2); -xy(1)]; % LC kör, alap rugó egyenlet, stabil egyensúlyi pálya
elseif strcmp(egyenlet_be, 'ddx+b*dx+x=0')
    egyenlet=@(t, xy) [xy(2); -b*xy(2)-xy(1)]; %RLC kör, rugó csillapítás, lecseng
elseif strcmp(egyenlet_be, 'ddx+x=cos(wk*t)')
    egyenlet=@(t, xy) [xy(2); -xy(1)+cos(wk*t)]; %más gerjesztés, rezonáns rugó?, összetett periódus
elseif strcmp(egyenlet_be, 'ddx+wb*x=cos(t)')
    egyenlet=@(t, xy) [xy(2); -wb*xy(1)+cos(t)]; % rugó ???, összetett periódus
elseif strcmp(egyenlet_be, 'ddx+b*dx+x=cos(t)')
    egyenlet=@(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
elseif strcmp(egyenlet_be, 'ddx+sin(x)=0')
    egyenlet=@(t, xy) [xy(2); -sin(xy(1))]; %inga (hajóhinta) alap egyenlet
elseif strcmp(egyenlet_be, 'ddx+b*dx+sin(x)=0')
    egyenlet=@(t, xy) [xy(2); -b*xy(2)-sin(xy(1))]; % inga csillapítással, lecseng
elseif strcmp(egyenlet_be, 'ddx+b*dx+sin(x)=cos(t)')
    egyenlet=@(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
end
[t, xy]=ode45(egyenlet, [tinit Max_Time], [Xinit; Yinit]); %diffegy megold az adott egyenlettel

%ábrázolás
plot(handles.xyplot, xy(:,1), xy(:,2), 'b'); %x-y ábrázolás
hold(handles.xyplot, 'on');
if ~strcmp(energia_be, 'nincs')  %kiválasztom, melyik energia függvényt használom
  if strcmp(energia_be, 'rugo')
    Z= 0.5.*Y.^2+0.5*X.^2;
  elseif strcmp(energia_be, 'inga')
    Z= 0.5.*Y.^2-cos(X)+1;
  end
  energiaszintek=[0, 0.1 0.25, 0.5, 1, 1.4, 2, 2.4, 3, 4]; %ezeket az energia szintvonalakat ábrázolom
  contour(handles.xyplot, X,Y,Z, energiaszintek); %energia szintvonalak
end
xlabel(handles.xyplot, 'x');
ylabel(handles.xyplot, 'y');
hold(handles.xyplot, 'off');

plot(handles.typlot, t, xy(:,2), 'b'); %x-t ábrázolás
xlabel(handles.typlot,'t');
ylabel(handles.typlot,'y');

plot(handles.xtplot, xy(:,1), t, 'b'); %t-y ábrázolás
xlabel(handles.xtplot,'x');
ylabel(handles.xtplot,'t');

plot3(handles.xytplot, xy(:,1), xy(:,2), t, 'b'); %x-y-t ábrázolás
xlabel(handles.xytplot,'x');
ylabel(handles.xytplot,'y');
zlabel(handles.xytplot,'t');










