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

% Last Modified by GUIDE v2.5 25-Oct-2016 21:16:20

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

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

% Update handles structure
guidata(hObject, handles);

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get the values from the GUI
tmax_abs=str2double(get(handles.tmax_edit,'String'));
s=str2double(get(handles.s_edit,'String'));
r=str2double(get(handles.r_edit,'String'));
b=str2double(get(handles.b_edit,'String'));
x1=str2double(get(handles.x1_edit,'String'));
y1=str2double(get(handles.y1_edit,'String'));
z1=str2double(get(handles.z1_edit,'String'));
x2=str2double(get(handles.x2_edit,'String'));
y2=str2double(get(handles.y2_edit,'String'));
z2=str2double(get(handles.z2_edit,'String'));

% call the draw function
lorenz_draw(tmax_abs,tmax_abs,s,r,b,x1,y1,z1,x2,y2,z2, handles);

global reseted;
reseted=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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


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


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


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


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


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


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


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


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


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


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


% --- Executes on button press in start_button.
function start_button_Callback(hObject, eventdata, handles)
% hObject    handle to start_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global reseted;
reseted=0;
% get the values from the GUI and error handling
ok_input=1;
tmax_temp=str2double(get(handles.tmax_edit,'String'));
if ~isnan(tmax_temp) && tmax_temp>0.1
    tmax_abs=tmax_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for tmax');
    ok_input=0;
end

s_temp=str2double(get(handles.s_edit,'String'));
if ~isnan(s_temp)
    s=s_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for s');
    ok_input=0;
end

r_temp=str2double(get(handles.r_edit,'String'));
if ~isnan(r_temp)
    r=r_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for r');
    ok_input=0;
end

b_temp=str2double(get(handles.b_edit,'String'));
if ~isnan(b_temp)
    b=b_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for b');
    ok_input=0;
end

x1_temp=str2double(get(handles.x1_edit,'String'));
if ~isnan(x1_temp)
    x1=x1_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for x1');
    ok_input=0;
end

y1_temp=str2double(get(handles.y1_edit,'String'));
if ~isnan(y1_temp)
    y1=y1_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for y1');
    ok_input=0;
end

z1_temp=str2double(get(handles.z1_edit,'String'));
if ~isnan(z1_temp)
    z1=z1_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for z1');
    ok_input=0;
end

x2_temp=str2double(get(handles.x2_edit,'String'));
if ~isnan(x2_temp) || isempty(get(handles.x2_edit,'String'))
    x2=x2_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for x2');
    ok_input=0;
end

y2_temp=str2double(get(handles.y2_edit,'String'));
if ~isnan(y2_temp) || isempty(get(handles.y2_edit,'String'))
    y2=y2_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for y2');
    ok_input=0;
end

z2_temp=str2double(get(handles.z2_edit,'String'));
if ~isnan(z2_temp) || isempty(get(handles.z2_edit,'String'))
    z2=z2_temp;
else
    set(handles.error_message_text, 'String', 'invalid input for z2');
    ok_input=0;
end

cont_draw=get(handles.cont_draw_togglebutton,'Value'); % 1 if the button is pushed, else 0

if ok_input==1
set(handles.error_message_text, 'String', '');
%continous drawing: we count and plot the system for more and more time points
%if stop is on reset does not working
if cont_draw==1
   for tmax=0.001:0.1:tmax_abs
       if reseted==1%reset terminated the drawing
           break;
           %lorenz_draw(100,100,10,28,2.66,5,5,5,NaN,NaN,NaN, handles);
       end
       while get(handles.stop_button, 'Value')==1 %pause drawing while stop is on
          pause(1); 
       end
                % call the draw function with valid inputs
                lorenz_draw(tmax,tmax_abs,s,r,b,x1,y1,z1,x2,y2,z2,handles);
                pause(0.1); %time step between plot update
   end
else
    % call the draw function with valid inputs
    lorenz_draw(tmax_abs,tmax_abs,s,r,b,x1,y1,z1,x2,y2,z2,handles); 
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in reset_button.
function reset_button_Callback(hObject, eventdata, handles)
% hObject    handle to reset_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set the values for the input fields to default
global reseted;
reseted=1;
set(handles.tmax_edit, 'String', '100');
set(handles.s_edit, 'String', '10');
set(handles.r_edit, 'String', '28');
set(handles.b_edit, 'String', '2.66');
set(handles.x1_edit, 'String', '5');
set(handles.y1_edit, 'String', '5');
set(handles.z1_edit, 'String', '5');
set(handles.x2_edit, 'String', '');
set(handles.y2_edit, 'String', '');
set(handles.z2_edit, 'String', '');
set(handles.cont_draw_togglebutton, 'Value', 0);
set(handles.stop_button, 'Value', 0);
set(handles.error_message_text, 'String', '');

% call the draw function
lorenz_draw(100,100,10,28,2.66,5,5,5,NaN,NaN,NaN, handles);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on button press in cont_draw_togglebutton.
function cont_draw_togglebutton_Callback(hObject, eventdata, handles)
% hObject    handle to cont_draw_togglebutton (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 cont_draw_togglebutton


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%drawing function
function lorenz_draw(tmax,tmax_abs,s,r,b,x1,y1,z1,x2,y2,z2,handles)
Lorenz_equation=@(t, xyz) [s*(xyz(2)-xyz(1));...
                           r*xyz(1)-xyz(2)-xyz(1)*xyz(3);...
                           xyz(1)*xyz(2)-b*xyz(3)]; % Lorenz system
init1=[x1,y1,z1];
[t, result] = ode45(Lorenz_equation,[0 tmax], init1); %diffequ solution

plot3(handles.xyz_plot, result(:,1),  result(:,2),  result(:,3), 'b'); %x-y-z plot
plot(handles.xt_plot, t, result(:,1), 'b'); %t-x plot
plot(handles.yt_plot, t, result(:,2), 'b'); %t-y plot
plot(handles.zt_plot, t, result(:,3), 'b'); %t-z plot

%peakmap
[peak_value, peak_location]=findpeaks(result(:,3)); %local maximum finder from signal processing toolbox
peak_n1=peak_value(1:end-1); %2 dataset (n and n+1) for the plot
peak_n2=peak_value(2:end);
plot(handles.peakmap_plot, peak_n1, peak_n2, 'b.'); %plot local maximums: the n+1. in the function of the n.

%second trajectory
if ~isnan(x2) && ~isnan(y2) && ~isnan(z2)
    init2=[x2,y2,z2];
    [t2, result2] = ode45(Lorenz_equation,[0 tmax], init2); %diffequ solution
             plot3(handles.xyz_plot, result(:,1),  result(:,2),  result(:,3), 'b', result2(:,1),  result2(:,2),  result2(:,3), 'r'); 
             plot(handles.xt_plot, t, result(:,1), 'b', t2, result2(:,1), 'r'); 
             plot(handles.yt_plot, t, result(:,2), 'b', t2, result2(:,2), 'r'); 
             plot(handles.zt_plot, t, result(:,3), 'b', t2, result2(:,3), 'r'); 
    [peak_value, peak_location]=findpeaks(result2(:,3)); %local maximum finder from signal processing toolbox
    peak_n12=peak_value(1:end-1); %2 dataset (n and n+1) for the plot
    peak_n22=peak_value(2:end);
              plot(handles.peakmap_plot, peak_n1, peak_n2, 'b.', peak_n12, peak_n22, 'r.');
end

xl=[-20,20];
yl=[-20 20];
zl=[0,50];

xlabel(handles.xyz_plot,'x');
ylabel(handles.xyz_plot,'y');
zlabel(handles.xyz_plot,'z');
xlim(handles.xyz_plot, xl);
ylim(handles.xyz_plot, yl);
zlim(handles.xyz_plot, zl);

xlabel(handles.xt_plot,'t');
ylabel(handles.xt_plot,'x');
xlim(handles.xt_plot, [0 tmax_abs]);
ylim(handles.xt_plot, xl);

xlabel(handles.yt_plot,'t');
ylabel(handles.yt_plot,'y');
xlim(handles.yt_plot, [0 tmax_abs]);
ylim(handles.yt_plot, yl);

xlabel(handles.zt_plot,'t');
ylabel(handles.zt_plot,'z');
xlim(handles.zt_plot, [0 tmax_abs]);
ylim(handles.zt_plot, zl);

xlim(handles.peakmap_plot, [30 50]);
ylim(handles.peakmap_plot, [30 50]);
xlabel(handles.peakmap_plot, 'Zn');
ylabel(handles.peakmap_plot, 'Zn+1');
title(handles.peakmap_plot, 'Lorenz peak map');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
