function varargout = Diffusion(varargin)
% DIFFUSION M-file for Diffusion.fig
%      DIFFUSION, by itself, creates a new DIFFUSION or raises the existing
%      singleton*.
%
%      H = DIFFUSION returns the handle to a new DIFFUSION or the handle to
%      the existing singleton*.
%
% How to use the GUI:
%    - Use the sliders to choose the number of particles, M, as
%      well as the total number of steps, N_max.
%    - Click on the Go button to start the simulation. It will stop
%      automatically after Nmax steps.
%    
% Developed by Joceline Lega, Department of Mathematics, University
% of Arizona, Spring 2005.
% 
% Edit the above text to modify the response to help Diffusion

% Last Modified by GUIDE v2.5 19-Apr-2006 15:23:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Diffusion_OpeningFcn, ...
                   'gui_OutputFcn',  @Diffusion_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 Diffusion is made visible.
function Diffusion_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 Diffusion (see VARARGIN)
global N_size
% Choose default command line output for Diffusion
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Diffusion wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.figure1,'Name','Diffusion at the Microscopic Level');
% set(handles.figure1,'Menubar','figure');
axes(handles.axes1)
M = round(max(get(handles.slider_M,'Value'),1));
N_max = round(max(get(handles.slider_N,'Value'),1));

N_size=40;
part=[zeros(1,M);zeros(1,M)];
plot(part(1,:),part(2,:),'o','MarkerFaceColor','r','MarkerSize',3,'MarkerEdgeColor','none'),...
    axis([-N_size N_size -N_size N_size]);
hold on
plot(part(1,1),part(2,1),'o','MarkerFaceColor','b','MarkerSize',3,'MarkerEdgeColor','none');
hold off
datacursormode on


% --- Outputs from this function are returned to the command line.
function varargout = Diffusion_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 during object creation, after setting all properties.
function slider_M_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_M (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_M_Callback(hObject, eventdata, handles)
% hObject    handle to slider_M (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
M = get(handles.slider_M,'Value');
set(handles.value_M,'String',round(max(M,1)));

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider_N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_N (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on slider movement.
function slider_N_Callback(hObject, eventdata, handles)
% hObject    handle to slider_N (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
N = get(handles.slider_N,'Value');
set(handles.value_N,'String',round(max(N,1)));

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

M = round(max(get(handles.slider_M,'Value'),1));
N_max = round(max(get(handles.slider_N,'Value'),1));

part=[zeros(1,M);zeros(1,M)];
move=mod(floor(4*rand(1,M)),4);

axes(handles.axes1)
plot(part(1,:),part(2,:),'o','MarkerFaceColor','r','MarkerSize',3,'MarkerEdgeColor','none'),...
    axis([-N_size N_size -N_size N_size]);
hold on
plot(part(1,1),part(2,1),'o','MarkerFaceColor','b','MarkerSize',3,'MarkerEdgeColor','none');
hold off
pause(1)

warning off MATLAB:divideByZero;
L(1)=0;

for N = 1:N_max,
    ym=isinf(1./move);
    xp=isinf(1./(move-1));
    yp=isinf(1./(move-2));
    xm=isinf(1./(move-3));
    part(1,:) = part(1,:) + xp(1,:) - xm(1,:);
    part(2,:) = part(2,:) + yp(1,:) - ym(1,:);
    move=mod(floor(4*rand(1,M)),4);
    plot(part(1,:),part(2,:),'o','MarkerFaceColor','r','MarkerSize',3,'MarkerEdgeColor','none'),...
        axis([-N_size N_size -N_size N_size]);
    hold on
    plot(part(1,1),part(2,1),'o','MarkerFaceColor','b','MarkerSize',3,'MarkerEdgeColor','none');
    hold off
    pause(0.5)
    dist=part(1,:).^2+part(2,:).^2;
    L(N+1)=mean(dist);
end

axes(handles.axes2)
plot(0:1:N,L,'LineWidth',2);
xlabel('Number of steps','Fontname','Times New Roman','Fontsize',12);
ylabel('\langle L ^2 \rangle','Fontname','Times New Roman','Fontsize',12);
xlim([0 N_max])
ylim([0 N_max])


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


