function varargout = Heat_Equation(varargin)
% 
% How to use the GUI:
%    - Choose one type of boundary conditions. An initial condition
%    that satisfies the selected boundary conditions is shown in blue
%    in the top right plot window.
%    - Click on button #1 to identify the Fourier basis associated
%    with the selected boundary conditions. The first 4 modes are
%    listed and plotted in blue, as functions of x.
%    - Click on button #2 to compute the coefficients of the first
%    4 modes in the Fourier series expansion of the initial condition.
%    Each of these modes is plotted in red as a function of x. Their
%    superposition is shown in red in the top right plot window, and
%    can be compared to the initial condition.
%    - Move the slider to see how each of the 4 modes evolves in time.
%    The corresponding truncated Fourier series is shown in red in the
%    top plot window.
% 
% 
% Developed by Zhiying Sun, Mathematics Department, University
% of Arizona, Spring 2007.
% 
global x
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Heat_Equation_OpeningFcn, ...
    'gui_OutputFcn',  @Heat_Equation_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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Heat_Equation_OpeningFcn(hObject, eventdata, handles, varargin)
global x f ic lambda f0 s

set(handles.figure1,'Name',' One-dimensional Heat Equation');
s=1;
x=0:0.02:1;
f0=0;
f=[.8 1.8 -1.9];
lambda=1*pi/1.*[1 2 3];
ic=f0;
for i=1:3
    ic=ic+f(i)*sin(lambda(i)*x);
end
ic=ic+0.5*sin(lambda(1)*4*x);
axes(handles.axes1)
plot(handles.axes1, x, ic)
axis([0 1 -4 4])
title('Initial Condition U(x,0) = f(x)')

axes(handles.axes6)
axis([0 1 -2 2])
axes(handles.axes2)
axis([0 1 -2 2])
axes(handles.axes3)
axis([0 1 -2 2])
axes(handles.axes4)
axis([0 1 -2 2])

axes(handles.axes7)
text(0.05,0,'Heat equation U_t=U_{xx} on the domain [0, 1]',...
    'FontSize',14,'Interpreter','tex')
axis off
handles.output = hObject;
guidata(hObject, handles);
function varargout = Heat_Equation_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function slider1_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s modes
t=get(hObject,'Value');
set(handles.time, 'String', ['t = ' num2str(t)])

if s==1
    for i=1:3
        y(i,:)=f(i)*sin(lambda(i)*x).*exp(-lambda(i)^2*t);
        a(i)=f(i)*exp(-lambda(i)^2*t);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    title(num2str(f0))
    axis([0 1 -2 2])

    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(a(1)),' ',char(modes(2))])
    axis([0 1 -2 2])

    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(a(2)),' ',char(modes(3))])
    axis([0 1 -2 2])

    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(a(3)),' ',char(modes(4))])
    axis([0 1 -2 2])
    
elseif s==2
    for i=1:3
        y(i,:)=f(i)*cos(lambda(i)*x).*exp(-lambda(i)^2*t);
        a(i)=f(i)*exp(-lambda(i)^2*t);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    title(num2str(f0))
    axis([0 1 -2 2])

    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(a(1)),' ',char(modes(2))])
    axis([0 1 -2 2])

    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(a(2)),' ',char(modes(3))])
    axis([0 1 -2 2])

    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(a(3)),' ',char(modes(4))])
    axis([0 1 -2 2])
    
elseif s==3
    for i=1:3
        y(i,:)=f(i)*sin(0.5*(2*i-1)*lambda(1)*x).*exp(-(0.5*(2*i-1)*lambda(1))^2*t);
        a(i)=f(i)*exp(-(0.5*(2*i-1)*lambda(1))^2*t);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    title(num2str(f0))
    axis([0 1 -2 2])

    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(a(1)),' ',char(modes(2))])
    axis([0 1 -2 2])

    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(a(2)),' ',char(modes(3))])
    axis([0 1 -2 2])

    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(a(3)),' ',char(modes(4))])
    axis([0 1 -2 2])
end

axes(handles.axes1)
plot(x,ic,'b')
hold on
plot(x,f0+y(1,:)+y(2,:)+y(3,:),'r')
hold off
axis([0 1 -4 4])
pause(0.5)


function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton1_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s modes
if s==1
    for i=1:3
        y(i,:)=sin(lambda(i)*x);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)))
    axis([0 1 -2 2])
    title('0')
    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:))
    title('sin(1\pi x)')
    axis([0 1 -2 2])
    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:))
    title('sin(2\pi x)')
    axis([0 1 -2 2])
    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:))
    title('sin(3\pi x)')
    axis([0 1 -2 2])
    modes = {'0';'sin(1\pi x)';'sin(2\pi x)';'cos(3\pi x)'};

elseif s==2
    for i=1:3
        y(i,:)=cos(lambda(i)*x);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)))
    axis([0 1 -2 2])
    title('1')
    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:))
    title('cos(1\pi x)')
    axis([0 1 -2 2])
    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:))
    title('cos(2\pi x)')
    axis([0 1 -2 2])
    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:))
    title('cos(3\pi x)')
    axis([0 1 -2 2])
    modes = {'1';'cos(1\pi x)';'cos(2\pi x)';'cos(3\pi x)'};
elseif s==3
    for i=1:3
        y(i,:)=sin(0.5*(2*i-1)*lambda(1)*x);
    end
    axes(handles.axes6)
    plot(handles.axes6,x,f0*ones(1,size(x,2)))
    axis([0 1 -2 2])
    title('0')
    axes(handles.axes2)
    plot(handles.axes2,x,y(1,:))
    title('sin(1\pi x/2)')
    axis([0 1 -2 2])
    axes(handles.axes3)
    plot(handles.axes3,x,y(2,:))
    title('sin(3\pi x/2)')
    axis([0 1 -2 2])
    axes(handles.axes4)
    plot(handles.axes4,x,y(3,:))
    title('sin(5\pi x/2)')
    axis([0 1 -2 2])
    modes = {'0';'sin(1\pi x/2)';'sin(3\pi x/2)';'sin(5\pi x/2)'};
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function pushbutton2_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s modes

if s==1
    for i=1:3
        y(i,:)=f(i)*sin(lambda(i)*x);
    end
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    axes(handles.axes6)
    axis([0 1 -2 2])
    title(char(modes(1)))

    axes(handles.axes2)
    hold on
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(f(1)),' ',char(modes(2))])
    axes(handles.axes2)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes3)
    hold on
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(f(2)),' ',char(modes(3))])
    axes(handles.axes3)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes4)
    hold on
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(f(3)),' ',char(modes(4))])
    axes(handles.axes4)
    axis([0 1 -2 2])
    hold off

elseif s==2
    for i=1:3
        y(i,:)=f(i)*cos(lambda(i)*x);
    end
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    axes(handles.axes6)
    axis([0 1 -2 2])
    title(num2str(f0))

    axes(handles.axes2)
    hold on
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(f(1)),' ',char(modes(2))])
    axes(handles.axes2)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes3)
    hold on
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(f(2)),' ',char(modes(3))])
    axes(handles.axes3)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes4)
    hold on
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(f(3)),' ',char(modes(4))])
    axes(handles.axes4)
    axis([0 1 -2 2])
    hold off

elseif s==3
    for i=1:3
        y(i,:)=f(i)*sin(0.5*(2*i-1)*lambda(1)*x);
    end
    plot(handles.axes6,x,f0*ones(1,size(x,2)),'r')
    axes(handles.axes6)
    axis([0 1 -2 2])
    title(char(modes(1)))

    axes(handles.axes2)
    hold on
    plot(handles.axes2,x,y(1,:),'r')
    title([num2str(f(1)),' ',char(modes(2))])
    axes(handles.axes2)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes3)
    hold on
    plot(handles.axes3,x,y(2,:),'r')
    title([num2str(f(2)),' ',char(modes(3))])
    axes(handles.axes3)
    axis([0 1 -2 2])
    hold off

    axes(handles.axes4)
    hold on
    plot(handles.axes4,x,y(3,:),'r')
    title([num2str(f(3)),' ',char(modes(4))])
    axes(handles.axes4)
    axis([0 1 -2 2])
    hold off
end

ysum=0;
% for i=1:3
%     ysum=ysum+y(i,:);
% end

axes(handles.axes1)
plot(x,ic,'b-')
hold on
plot(x,f0+y(1,:)+y(2,:)+y(3,:),'r')
axis([0 1 -4 4])
hold off

pause(0.5)

% set(handles.text17,'ForegroundColor','r')
% set(handles.text18,'ForegroundColor','r')
% set(handles.text19,'ForegroundColor','r')
% set(handles.text20,'ForegroundColor','r')


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s
if get(hObject,'Value')==0
    set(hObject, 'Value',1)
end
s=1;
f0=0;
f=[.8 1.8 -1.9];
lambda=1*pi/1.*[1 2 3];
ic=f0;
for i=1:3
    ic=ic+f(i)*sin(lambda(i)*x);
end
ic=ic+0.5*sin(lambda(1)*4*x);
axes(handles.axes1)
plot(handles.axes1, x, ic)
axis([0 1 -4 4])
title('Initial Condition U(x,0) = f(x)')
axes(handles.axes6)
%     plot(handles.axes6,x,y(1,:))
title('')
cla
axes(handles.axes2)
%     plot(handles.axes2,x,y(1,:))
title('')
cla
axes(handles.axes3)
%     plot(handles.axes3,x,y(1,:))
title('')
cla
axes(handles.axes4)
%     plot(handles.axes4,x,y(1,:))
title('')
cla
set(handles.slider1, 'Value', 0)
set(handles.time, 'String', 't = 0')

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s
if get(hObject,'Value')==0
    set(hObject, 'Value',1)
end
s=2;
f0=1.7;
f=[.4 -0.8 -0.9];
lambda=1*pi/1.*[1 2 3];
ic=f0;
for i=1:3
    ic=ic+f(i)*cos(lambda(i)*x);
end
ic=ic+0.5*cos(lambda(1)*4*x);
axes(handles.axes1)
plot(handles.axes1, x, ic)
axis([0 1 -4 4])
title('Initial Condition U(x,0) = f(x)')
axes(handles.axes6)
%     plot(handles.axes6,x,y(1,:))
title('')
cla
axes(handles.axes2)
%     plot(handles.axes2,x,y(1,:))
title('')
cla
axes(handles.axes3)
%     plot(handles.axes3,x,y(1,:))
title('')
cla
axes(handles.axes4)
%     plot(handles.axes4,x,y(1,:))
title('')
cla
set(handles.slider1, 'Value', 0)
set(handles.time, 'String', 't = 0')

% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
global x f ic lambda f0 s
if get(hObject,'Value')==0
    set(hObject, 'Value',1)
end
s=3;
f0=0;
f=[1.4 1.8 -0.9];
lambda=1*pi/1.*[1 2 3];
ic=f0;
for i=1:3
    ic=ic+f(i)*sin(0.5*(2*i-1)*lambda(1)*x);
end
ic=ic+0.5*sin(0.5*(2*4-1)*lambda(1)*x);
axes(handles.axes1)
plot(handles.axes1, x, ic)
axis([0 1 -4 4])
title('Initial Condition U(x,0) = f(x)')
axes(handles.axes6)
%     plot(handles.axes6,x,y(1,:))
title('')
cla
axes(handles.axes2)
%     plot(handles.axes2,x,y(1,:))
title('')
cla
axes(handles.axes3)
%     plot(handles.axes3,x,y(1,:))
title('')
cla
axes(handles.axes4)
%     plot(handles.axes4,x,y(1,:))
title('')
cla
set(handles.slider1, 'Value', 0)
set(handles.time, 'String', 't = 0')


%too fast: not correct...
%clear graph


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


