function varargout = Heat_Equation_WL(varargin)
% HEAT_EQUATION_WL M-file for Heat_Equation_WL.fig
%      HEAT_EQUATION_WL, by itself, creates a new HEAT_EQUATION_WL or raises the existing
%      singleton*.
%
%      H = HEAT_EQUATION_WL returns the handle to a new HEAT_EQUATION_WL or the handle to
%      the existing singleton*.
%
% How to use the GUI
%    - Use the sliders in the "Initial conditions" panel to choose the
%    height and the width of the "top hat" initial condition.
%    - Adjust the diffusion coefficient.
%    - Use the slider under the plot window to see how the solution
%    evolves in time.
%    - The solution at time t is shown in red and the initial
%    condition in blue.
% 
% Developed by Joceline Lega, Mathematics Department, University
% of Arizona, Spring 2007.
% 

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

global x

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Heat_Equation_WL wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.figure1,'Name',' Heat Equation on the Whole Line');
axes(handles.axes1)
x = -10:0.1:10;
plot_init(hObject, eventdata, handles);


% --- Outputs from this function are returned to the command line.
function varargout = Heat_Equation_WL_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 slider movement.
function slider_width_Callback(hObject, eventdata, handles)
% hObject    handle to slider_width (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
w = get(handles.slider_width,'Value');
if w < 0.1
    w = 0.1;
    set(handles.slider_width,'Value',w);
end
set(handles.text_width,'String',w);
plot_init(hObject, eventdata, handles);
set(handles.slider_time,'Value',0);
set(handles.text_time,'String','t = 0');


% --- Executes during object creation, after setting all properties.
function slider_width_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_width (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_height_Callback(hObject, eventdata, handles)
% hObject    handle to slider_height (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
plot_init(hObject, eventdata, handles);
set(handles.slider_time,'Value',0);
set(handles.text_time,'String','t = 0');
h = get(handles.slider_height,'Value');
set(handles.text_height,'String',h);


% --- Executes during object creation, after setting all properties.
function slider_height_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_height (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_time_Callback(hObject, eventdata, handles)
% hObject    handle to slider_time (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x y0

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
t = get(handles.slider_time,'Value');
set(handles.text_time,'String',['t = ' num2str(t)]);
c = get(handles.slider_d,'Value');
w = get(handles.slider_width,'Value');
h = get(handles.slider_height,'Value');
if t == 0
    plot_init(hObject, eventdata, handles);
else
    y = h*(erf((w-x)/(2*c*sqrt(t)))-erf((-w-x)/(2*c*sqrt(t))))/2;
    plot(x,y,'r','LineWidth',2)
    hold on
    plot(x,y0)
    hold off
    xlabel('Position')
    xlim([-10 10])
    ylim([0 2.1])
end

% --- Executes during object creation, after setting all properties.
function slider_time_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_time (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider_d_Callback(hObject, eventdata, handles)
% hObject    handle to slider_d (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
c = get(handles.slider_d,'Value');
if c == 0
    c = 0.1;
    set(handles.slider_d,'Value',c);
end
set(handles.text_d,'String',c);
plot_init(hObject, eventdata, handles);
set(handles.slider_time,'Value',0);
set(handles.text_time,'String','t = 0');


% --- Executes during object creation, after setting all properties.
function slider_d_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_d (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.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


function plot_init(hObject, eventdata, handles)
global x y0

y0 = zeros(size(x));
w = get(handles.slider_width,'Value');
h = get(handles.slider_height,'Value');
y_m = floor(10*w);
y0(101-y_m:101+y_m) = h;
plot(x,y0,'LineWidth',2)
xlabel('Position')
xlim([-10 10])
ylim([0 2.1])

