function varargout = Plotter(varargin)
% PLOTTER M-file for Plotter.fig
%      PLOTTER, by itself, creates a new PLOTTER or raises the existing
%      singleton*.
%
%      H = PLOTTER returns the handle to a new PLOTTER or the handle to
%      the existing singleton*.
%
% How to use the GUI:
%    - Use the "Setup" panel to define the two functions to be plotted,
%      as well as upper and lower limits for the x-, y-, and r-axes.
%      Here r is a control parameter that affects the behavior
%      of the functions f1 and f2. These functions should depend on one
%      variable, called x.
%    - Use the slider in the "Control parameter" panel to change the 
%      value of r and see how it affects the graphs of f1 and f2.
%    - The "3D" button shows a three-dimensional plot of f1 and f2 as
%      functions of x and r.
%    
% Developed by Joceline Lega, Department of Mathematics, University
% of Arizona, Spring 2006 and Fall 2008.
% 

% Edit the above text to modify the response to help Plotter

% Last Modified by GUIDE v2.5 21-Oct-2008 15:07:09

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

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Plotter wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.figure1,'Name','Simple Plotter');
r_min=get(handles.slider1,'Min');
r_max=get(handles.slider1,'Max');
replot(hObject,eventdata,handles)

% --- Outputs from this function are returned to the command line.
function varargout = Plotter_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 slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global x fct fct2
global x_min x_max y_min y_max

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
r=get(handles.slider1,'Value');
set(handles.text1,'String',['r = ' num2str(r)]);

plot(x,eval(fct).*ones(size(x)),'LineWidth',2)
ylim([y_min y_max]);
xlim([x_min x_max]);
if ~isempty(fct2)
    hold on
    plot(x,eval(fct2).*ones(size(x)),'r','LineWidth',2)
    legend(fct,fct2)
    hold off
else
    legend(fct)
end
xlabel('x')

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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 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 x fct fct2
global x_min x_max y_min y_max
global r_min r_max

step=(r_max-r_min)/100;
r=r_min:step:r_max;
[X,R]=meshgrid(x,r);
h1=eval(regexprep(regexprep(fct,'r','R'),'x','X')).*ones(size(X));
h2=eval(regexprep(regexprep(fct2,'r','R'),'x','X')).*ones(size(X));
figure(1)
surf(R,X,h1,100*ones(size(X))),shading interp
hold on;
surf(R,X,h2,zeros(size(X))),shading interp
hold off;
xlabel('r')
ylabel('x')
legend(fct,fct2)
colormap(winter)
axis square
% zlim([y_min y_max])

function edit_x_min_Callback(hObject, eventdata, handles)
% hObject    handle to edit_x_min (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,'String') returns contents of edit_x_min as text
%        str2double(get(hObject,'String')) returns contents of edit_x_min as a double

replot(hObject,eventdata,handles)

% --- Executes during object creation, after setting all properties.
function edit_x_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_x_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit_x_max_Callback(hObject, eventdata, handles)
% hObject    handle to edit_x_max (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,'String') returns contents of edit_x_max as text
%        str2double(get(hObject,'String')) returns contents of edit_x_max as a double

replot(hObject,eventdata,handles)


% --- Executes during object creation, after setting all properties.
function edit_x_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_x_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


function edit_y_min_Callback(hObject, eventdata, handles)
% hObject    handle to edit_y_min (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,'String') returns contents of edit_y_min as text
%        str2double(get(hObject,'String')) returns contents of edit_y_min as a double

replot(hObject,eventdata,handles)


% --- Executes during object creation, after setting all properties.
function edit_y_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_y_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit_y_max_Callback(hObject, eventdata, handles)
% hObject    handle to edit_y_max (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,'String') returns contents of edit_y_max as text
%        str2double(get(hObject,'String')) returns contents of edit_y_max as a double

replot(hObject,eventdata,handles)


% --- Executes during object creation, after setting all properties.
function edit_y_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_y_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit_function_Callback(hObject, eventdata, handles)
% hObject    handle to edit_function (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,'String') returns contents of edit_function as text
%        str2double(get(hObject,'String')) returns contents of edit_function as a double

replot(hObject,eventdata,handles)


% --- Executes during object creation, after setting all properties.
function edit_function_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_function (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


function edit_function_2_Callback(hObject, eventdata, handles)
% hObject    handle to edit_function_2 (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,'String') returns contents of edit_function_2 as text
%        str2double(get(hObject,'String')) returns contents of edit_function_2 as a double

replot(hObject,eventdata,handles)


% --- Executes during object creation, after setting all properties.
function edit_function_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_function_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


function replot(hObject,eventdata,handles)

global x fct fct2
global x_min x_max y_min y_max

x_min=str2num(get(handles.edit_x_min,'String'));
x_max=str2num(get(handles.edit_x_max,'String'));
y_min=str2num(get(handles.edit_y_min,'String'));
y_max=str2num(get(handles.edit_y_max,'String'));
step=(x_max-x_min)/100;
fct=get(handles.edit_function,'String');
fct2=get(handles.edit_function_2,'String');
x=x_min:step:x_max;
r=get(handles.slider1,'Value');
plot(x,eval(fct).*ones(size(x)),'LineWidth',2)
ylim([y_min y_max]);
xlim([x_min x_max]);
if ~isempty(fct2)
    hold on
    plot(x,eval(fct2).*ones(size(x)),'r','LineWidth',2)
    legend(fct,fct2)
    hold off
else
    legend(fct)
end
xlabel('x')



function edit_r_min_Callback(hObject, eventdata, handles)
% hObject    handle to edit_r_min (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,'String') returns contents of edit_r_min as text
%        str2double(get(hObject,'String')) returns contents of edit_r_min as a double

global r_min r_max

r_min=str2num(get(handles.edit_r_min,'String'));
v=max((r_max-r_min)/2,r_min);
v=min(v,r_max);
set(handles.slider1,'Value',v);
set(handles.slider1,'Min',r_min);
set(handles.text1,'String',['r = ' num2str(v)]);
replot(hObject,eventdata,handles)

% --- Executes during object creation, after setting all properties.
function edit_r_min_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_r_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit_r_max_Callback(hObject, eventdata, handles)
% hObject    handle to edit_r_max (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,'String') returns contents of edit_r_max as text
%        str2double(get(hObject,'String')) returns contents of edit_r_max as a double

global r_min r_max

r_max=str2num(get(handles.edit_r_max,'String'));
v=max((r_max-r_min)/2,r_min);
v=min(v,r_max);
set(handles.slider1,'Value',v);
set(handles.slider1,'Max',r_max);
set(handles.text1,'String',['r = ' num2str(v)]);
replot(hObject,eventdata,handles)

% --- Executes during object creation, after setting all properties.
function edit_r_max_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_r_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


function edit15_Callback(hObject, eventdata, handles)
% hObject    handle to edit_x_min (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,'String') returns contents of edit_x_min as text
%        str2double(get(hObject,'String')) returns contents of edit_x_min as a double


% --- Executes during object creation, after setting all properties.
function edit15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_x_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit_x_maz_Callback(hObject, eventdata, handles)
% hObject    handle to edit_x_max (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,'String') returns contents of edit_x_max as text
%        str2double(get(hObject,'String')) returns contents of edit_x_max as a double


% --- Executes during object creation, after setting all properties.
function edit_x_maz_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_x_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit17_Callback(hObject, eventdata, handles)
% hObject    handle to edit_y_min (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,'String') returns contents of edit_y_min as text
%        str2double(get(hObject,'String')) returns contents of edit_y_min as a double


% --- Executes during object creation, after setting all properties.
function edit17_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_y_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit18_Callback(hObject, eventdata, handles)
% hObject    handle to edit_y_max (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,'String') returns contents of edit_y_max as text
%        str2double(get(hObject,'String')) returns contents of edit_y_max as a double


% --- Executes during object creation, after setting all properties.
function edit18_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_y_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit19_Callback(hObject, eventdata, handles)
% hObject    handle to edit_function (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,'String') returns contents of edit_function as text
%        str2double(get(hObject,'String')) returns contents of edit_function as a double


% --- Executes during object creation, after setting all properties.
function edit19_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_function (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit20_Callback(hObject, eventdata, handles)
% hObject    handle to edit_function_2 (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,'String') returns contents of edit_function_2 as text
%        str2double(get(hObject,'String')) returns contents of edit_function_2 as a double


% --- Executes during object creation, after setting all properties.
function edit20_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_function_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit21_Callback(hObject, eventdata, handles)
% hObject    handle to edit_r_min (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,'String') returns contents of edit_r_min as text
%        str2double(get(hObject,'String')) returns contents of edit_r_min as a double


% --- Executes during object creation, after setting all properties.
function edit21_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_r_min (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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



function edit22_Callback(hObject, eventdata, handles)
% hObject    handle to edit_r_max (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,'String') returns contents of edit_r_max as text
%        str2double(get(hObject,'String')) returns contents of edit_r_max as a double


% --- Executes during object creation, after setting all properties.
function edit22_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_r_max (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit 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


