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

% Last Modified by GUIDE v2.5 27-Jan-2014 10:48:50

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

% Choose default command line output for potentials_gui
handles.output = hObject;
set(handles.popupmenu1,'String',{'Choose Potential','Finite Well','Infinite Well','Discontinuous Potential', 'Rolling Hills', 'Gravity Well', 'Half Pipe'})

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = potentials_gui_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 popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global field X Y Z fx fy x0 y0 vx vy t pausetime
field=get(handles.popupmenu1,'Value')-1;
N=50; t=0:.1:10; pausetime=.1;
if field==1
    x=linspace(-1/2,1/2,N); x=x(1:end-1); y=x;
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) -cos(pi*X).*cos(pi*Y); set(handles.text3,'String','f(x,y)=-cos(\pi x)cos(\pi y)')
    fx=@(X,Y) pi*sin(pi*X).*cos(pi*Y);
    fy=@(X,Y) pi*cos(pi*X).*sin(pi*Y);
    x0=0;  y0=-.4; vx=0; vy=0;
elseif field==2
    x=linspace(-1,1,N); 
    y=linspace(-1,1,N); 
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) -exp(-X.^2-Y.^2); set(handles.text3,'String','f(x,y)=exp(-x^2-y^2)')
    fx=@(X,Y) -2*X.*Z(X,Y);
    fy=@(X,Y) -2*Y.*Z(X,Y);
    x0=1/2;  y0=-3/4; vx=0; vy=0;
elseif field==3
    x=linspace(-1,1,N); y=linspace(-1,1,N); 
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) (1/2)* log(X.^2+Y.^2); set(handles.text3,'String','f(x,y)=log(x^2+y^2)/2')
    fx=@(X,Y) X./(X.^2+Y.^2);
    fy=@(X,Y) Y./(X.^2+Y.^2);
    x0=1/2;  y0=-1/2; vx=1/2; vy=0; t=0:0.05:10; pausetime=.05;
elseif field==4
    x=linspace(-3,3,N);
    y=linspace(1.5,4,N);
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) X.*cos(5*X)+Y.*sin(5*Y); set(handles.text3,'String','f(x,y)=xcos(5x)+Ysin(5y)')
    fx=@(X,Y) cos(5*X) - 5*X.*sin(5*X);
    fy=@(X,Y) sin(5*Y) + 5*Y.*cos(5*Y);
    x0=1; y0=2.25; vx=0; vy=0;
elseif field==5
    x=linspace(-5,5,N);
    y=linspace(-5,5,N);
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) -10./sqrt(X.^2+Y.^2); set(handles.text3,'String','f(x,y)=-10/(x^2+y^2)')
    fx=@(X,Y) -X.*(Z(X,Y).^3);
    fy=@(X,Y) -Y.*(Z(X,Y).^3);
    x0=2; y0=0; vx=0; vy=-(2*pi)*2^2;
    t=0:.01:10; pausetime=.000000000000001;    
elseif field==6
    x=linspace(-3,3,N);
    y=linspace(-4,4,N);
    [X,Y]=meshgrid(x,y);
    Z=@(X,Y) (X).^4+(1/2)*Y; set(handles.text3,'String','f(x,y)=x^4+y/2')
    fx=@(X,Y) 4*X.^3;
    fy=@(X,Y) 1/2;
    t=0:.1:5; pausetime=.01;
    x0=2.5; y0=2.5; vx=0; vy=0;    
end
set(handles.text5,'String',['(x,y)= (' num2str(x0) ',' num2str(y0) ')'])
set(handles.text7,'String',['(vx,vy)= (' num2str(vx) ',' num2str(vy) ')'])
surf(X,Y,Z(X,Y))
colormap('bone')
shading interp
hold on
plot3(x0,y0,Z(x0,y0),'. r', 'Markersize',20)
hold off


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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 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 t x0 y0 vx vy X Y Z fx fy pausetime
% if strcmp(get(handles.pushbutton1,'String'),'Start')==1
%     stopper=0;
    dxdt=@(t,X) [X(3); X(4); -fx(X(1),X(2)); -fy(X(1),X(2))];
    set(handles.pushbutton1,'String','Simulating')
    [~,sol]=ode45(dxdt,t,[x0; y0; vx; vy]);
    set(handles.pushbutton1,'String','Running')
    for j=1:length(t)
%             if stopper==0
                surf(X,Y,Z(X,Y))
                shading interp
                hold on
                plot3(sol(j,1),sol(j,2),Z(sol(j,1),sol(j,2)),'. r', 'Markersize',20)
                title([num2str(floor(t(end)-t(j))) ' seconds remaining'])
                hold off
                pause(pausetime)
%             end
    end
% elseif strcmp(get(handles.pushbutton1,'String'),'Stop')==1
%     stopper=1;
    set(handles.pushbutton1,'String','Start')
% end



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