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

% Last Modified by GUIDE v2.5 26-Mar-2016 08:53:14

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

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

% Update handles structure
guidata(hObject, handles);

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

        %sajat rész: kezdeti kirajzolás,
        %a kirajzolás külön függvényben a végén
        kirajzolas(handles);


% --- Outputs from this function are returned to the command line.
function varargout = populacio_dinamika_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 a1_value_Callback(hObject, eventdata, handles)
% hObject    handle to a1_value (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 a1_value as text
%        str2double(get(hObject,'String')) returns contents of a1_value as a double

    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

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

    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

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

    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

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

    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

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

    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

% --- Executes during object creation, after setting all properties.
function c2_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to c2_value (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 c1_value_Callback(hObject, eventdata, handles)
% hObject    handle to c1_value (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 c1_value as text
%        str2double(get(hObject,'String')) returns contents of c1_value as a double
    
    set(handles.esetek, 'Value', 1); %az esetválasztót saját bemenetre állít

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

% Hint: get(hObject,'Value') returns toggle state of trajektoria

% --- Executes on selection change in esetek.
function esetek_Callback(hObject, eventdata, handles)
% hObject    handle to esetek (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 esetek contents as cell array
%        contents{get(hObject,'Value')} returns selected item from esetek

    %saját rész: a legördülõ menübõl kiválasztott esetnek megfelelõ
    % adott paraméter készletet adom át szövegbeviteli mezõknek
    eset=get(handles.esetek, 'Value');    
        if eset==2 %2 faj együtt, ragadozás
            set(handles.a1_value, 'String', '1');
            set(handles.b1_value, 'String', '1');
            set(handles.c1_value, 'String', '1');
            set(handles.a2_value, 'String', '2');
            set(handles.b2_value, 'String', '1');
            set(handles.c2_value, 'String', '-1');
        elseif eset==3 %áldozat kihal, ragadozás
            set(handles.a1_value, 'String', '2');
            set(handles.b1_value, 'String', '1');
            set(handles.c1_value, 'String', '1');
            set(handles.a2_value, 'String', '1');
            set(handles.b2_value, 'String', '1');
            set(handles.c2_value, 'String', '-1');
        elseif eset==4 %Lotka-Volterra oszcilláció, ragadozás
            set(handles.a1_value, 'String', '-2');
            set(handles.b1_value, 'String', '0');
            set(handles.c1_value, 'String', '1');
            set(handles.a2_value, 'String', '2');
            set(handles.b2_value, 'String', '0');
            set(handles.c2_value, 'String', '-1');
        elseif eset==5 %mindig az egyik nyer, versengés
            set(handles.a1_value, 'String', '2');
            set(handles.b1_value, 'String', '1');
            set(handles.c1_value, 'String', '-1');
            set(handles.a2_value, 'String', '1');
            set(handles.b2_value, 'String', '1');
            set(handles.c2_value, 'String', '-1');
        elseif eset==6 %kezdettõl függ melyik nyer, versengés
            set(handles.a1_value, 'String', '3');
            set(handles.b1_value, 'String', '1');
            set(handles.c1_value, 'String', '-2');
            set(handles.a2_value, 'String', '3');
            set(handles.b2_value, 'String', '1');
            set(handles.c2_value, 'String', '-2');
        elseif eset==7 %2 faj együtt van jelen, szimbiózis
            set(handles.a1_value, 'String', '1');
            set(handles.b1_value, 'String', '1');
            set(handles.c1_value, 'String', '1');
            set(handles.a2_value, 'String', '1');
            set(handles.b2_value, 'String', '3');
            set(handles.c2_value, 'String', '1');  
        end
        kirajzolas(handles); %a legördülõ menü változtatására is kirajzol
    


% --- Executes during object creation, after setting all properties.
function esetek_CreateFcn(hObject, eventdata, handles)
% hObject    handle to esetek (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 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)
    
    %saját rész: a rajzol gomb megnyomásása rajzol ki
    kirajzolas(handles);

    
%saját rész    
% kirajzoló függvény:
function kirajzolas(handles)
% általános populációs dinamika modell
%dx=x*(a1-b1*x+c1*y)
%dy=y*(a2-b2*y+c2*x)

%paraméterek:
a1=str2double(get(handles.a1_value, 'String')); % x szaporodási rátája
b1=str2double(get(handles.b1_value, 'String')); % x erõforráskorlátja (természetes halálozása)
c1=str2double(get(handles.c1_value, 'String')); % keresztcsatolás: az y hatása az x-re
a2=str2double(get(handles.a2_value, 'String')); % y szaporodási rátája
b2=str2double(get(handles.b2_value, 'String')); % y erõforráskorlátja (természetes halálozása)
c2=str2double(get(handles.c2_value, 'String')); % keresztcsatolás: az x hatása az y-ra 

%hibakezelés:
if isnan(a1) %hibaüzenet, ha nem számot adok be
    kiirt_szoveg='Hiba: a1 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír
elseif isnan(a2)
    kiirt_szoveg='Hiba: a2 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif isnan(c1)
    kiirt_szoveg='Hiba: c1 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif isnan(c2)
    kiirt_szoveg='Hiba: c2 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif isnan(b1)
    kiirt_szoveg='Hiba: b1 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif b1<0 %hibaüzenet, ha az erõforráskorlát elõjele nem jó
    kiirt_szoveg='Hiba: b1 nem lehet negatív.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif isnan(b2)
    kiirt_szoveg='Hiba: b2 nem szám.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 
elseif b2<0
    kiirt_szoveg='Hiba: b2 nem lehet negatív.';
    set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír 

else %megtörténik a  rajzolás, ha jók a bemenetek:
    
x=0:0.1:4;
y=0:0.1:4;
% 0 gradiensû egyenesek (dx=0; dy=0) meghatározása:
dx0_1=zeros(1, length(y));
dx0_2=(-a1+b1*x)/c1;
dy0_1=zeros(1, length(x));
dy0_2=(a2+c2*x)/b2;

%vektormezõ meghatározása:
[X,Y]=meshgrid(x,y);
dx=X.*(a1-b1*X+c1*Y);
dy=Y.*(a2-b2*Y+c2*X);
%normálás:
dx=dx./ sqrt(dx.^2+dy.^2);
dy=dy./sqrt(dx.^2+dy.^2);

%több trajektória kiszámolása, ha a "trajektória" checkbox be van pipálva,
%tehát igaz (1) az értéke
if get(handles.trajektoria, 'Value')
%idõ intervallum
t_null=0;
t_max=10;
N=50; %egyenesek mentén pontok száma
%az ábra 4 határoló egyeneseirõl indítok trajektóriákat:
%az egyenesek:
sxk=0.1;
syk=0.1;
sxv=0.1;
syv=y(end);

sx2k=0.1;
sy2k=0.1;
sx2v=x(end);
sy2v=0.1;

sx3k=x(end);
sy3k=0.1;
sx3v=x(end);
sy3v=y(end);

sx4k=0.1;
sy4k=y(end);
sx4v=x(end);
sy4v=y(end);

%az egyenesek pontjait megadom és összefûzöm egy vektorba:
sx=[linspace(sxk,sxv,N), linspace(sx2k,sx2v,N), linspace(sx3k,sx3v,N), linspace(sx4k,sx4v,N)];
sy=[linspace(syk,syv,N), linspace(sy2k,sy2v,N), linspace(sy3k,sy3v,N), linspace(sy4k,sy4v,N)];

%trajektóriák kiszámolása és ábrázolása
for i=1:length(sx)
    x_null=sx(i);
    y_null=sy(i);
        egyenlet=@(t,xy)[xy(1)*(a1-b1*xy(1)+c1*xy(2));xy(2)*(a2-b2*xy(2)+c2*xy(1))];
        [t, xy]=ode45(egyenlet, [t_null, t_max], [x_null, y_null]);
        plot(handles.abra, xy(:,1), xy(:,2), 'g');
        hold(handles.abra, 'on');
    %end
end
end

%ábrázolás:
%vektormezõ és a és dx=0, dy=0 egyenesek:
quiver(handles.abra, X,Y,dx,dy, 0.5, 'k');
hold(handles.abra, 'on');
plot(handles.abra, dx0_1, y, 'r', x, dx0_2, 'r', x, dy0_1, 'b', x, dy0_2, 'b');
xlabel(handles.abra, 'x');
ylabel(handles.abra, 'y');
xlim([-0.1, 4.1]);
ylim([-0.1, 4.1]);
hold(handles.abra, 'off');

kiirt_szoveg='Kész.';
set(handles.statusbar, 'String', kiirt_szoveg); %szöveges mezõre kiír
end