% 
%%
% *Программа GUI для нахождения периодических точек*
function varargout = Periods(varargin)
% PERIODY M-file for Periody.fig
%      PERIODY, by itself, creates koeff_a new PERIODY or raises the existing
%%
% 
% * ITEM1
% * ITEM2
% 
%      singleton*.
% 
%  PREFORMATTED
%  TEXT
% 
%
%      H = PERIODY returns the handle to koeff_a new PERIODY or the handle to
%      the existing singleton*.
%
%      PERIODY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PERIODY.M with the given input arguments.
%
%      PERIODY('Property','Value',...) creates koeff_a new PERIODY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Periody_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Periody_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 Periody

% Last Modified by GUIDE v2.5 09-Feb-2010 15:38:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Periody_OpeningFcn, ...
                   'gui_OutputFcn',  @Periody_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 Periody is made visible.
function Periody_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% varargin   command line arguments to Periody (see VARARGIN)
% Choose default command line output for Periody
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = Periody_OutputFcn(~, ~, handles) 
% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
X=imread('Period_4.jpg');
image(X);

% --- Executes during object creation, after setting all properties.
function n_iter_CreateFcn(hObject, eventdata, handles)
handles.data.n_iter = 40;
guidata(hObject,handles);

function n_iter_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'String') returns contents of n_iter as text
%        str2double(get(hObject,'String')) returns contents of n_iter as koeff_a double
n_iter = str2double(get(hObject, 'String'));
if isnan(n_iter)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new density value
handles.data.n_iter = n_iter;
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function koeff_a_CreateFcn(hObject, eventdata, handles)
handles.data.koeff_a = 3.3;
guidata(hObject,handles);

function koeff_a_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'String') returns contents of koeff_a as text
%        str2double(get(hObject,'String')) returns contents of koeff_a as koeff_a double
koeff_a = str2double(get(hObject, 'String'));
if isnan(koeff_a)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
% Save the new density value
handles.data.koeff_a = koeff_a;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function x_0_CreateFcn(hObject, eventdata, handles)
handles.data.x_0 = 0.9;
guidata(hObject,handles);


function x_0_Callback(hObject, eventdata, handles)
% Hints: get(hObject,'String') returns contents of x_0 as text
%        str2double(get(hObject,'String')) returns contents of x_0 as koeff_a double
x_0 = str2double(get(hObject, 'String'));
if isnan(x_0)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
% Save the new density value
handles.data.x_0 = x_0
guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function pushbutton1_CreateFcn(hObject, eventdata, handles)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes2);
cla;
n = handles.data.n_iter;
a = handles.data.koeff_a;
x0 = handles.data.x_0;

x=0:0.01:1;
hold on;
plot(x,x,'r-'); % График y=x
y=a*x.*(1-x);
plot(x,y,'k');  % График y = ax(1-x)
%axis([-.2 1.2 -.2 1.2])
 xn=x0;
 xn1=a*xn.*(1-xn); % x = ax(1-x) Первая итерация
 for i=1:n
      plot([xn,xn1],[xn1,xn1]); % Гориз. Отрезок от точки графика до y=x
         pause(0.2*(1-i/(n+1)));
      yxn1=xn1; % точка на y=x 
      xn=xn1;       % переобозначим для цикла, теперь начальная точка xn на пересечении y=x 
      xn1=a*xn.*(1-xn); % точка на графике
      plot([yxn1,yxn1],[yxn1,xn1]); % вертикальная линия от y=x до графика
         pause(0.2*(1-i/(n+1)));
 end
hold off;
