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

% Last Modified by GUIDE v2.5 31-Aug-2012 15:44:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @IB_ODE_GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @IB_ODE_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
% End initialization code - DO NOT EDIT

% --- Executes just before IB_ODE_GUI is made visible.
function IB_ODE_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 IB_ODE_GUI (see VARARGIN)

% Choose default command line output for IB_ODE_GUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes IB_ODE_GUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
clear global
global counter
global check
global check1
global check2
global check3
global check4
global check5
global check6

set(handles.y_dash,'String','y')
% f_dash_Callback(hObject, eventdata, handles)
set(handles.stepsize,'String','0.1')
% stepsize_Callback(hObject, eventdata, handles)
set(handles.x0,'String','0')
% x0_Callback(hObject, eventdata, handles)
set(handles.y0,'String','1')
% y0_Callback(hObject, eventdata, handles)
set(handles.sol_exact,'String','exp(x)')
% sol_exact_Callback(hObject, eventdata, handles)
user_interaction(handles);

check = 'gr';
counter = 1;
axis off
check1 = 1; %FE is selected as preset
check2 = 0;
check3 = 0;
check4 = 0;
check5 = 0;
check6 = 0;

hold on;
ax=handles.legendax;
set(ax,'Visible','off');
set(ax,'NextPlot','add');
axis(ax,'off');

h=cell(7,2);
h1=plot(ax,0,0, '-','Color','red','LineWidth',2,'MarkerEdgeColor','k', 'MarkerFaceColor','k', 'MarkerSize',10);
h{1,2}='Exact';
h2=plot(ax,0,0, 'k o-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',13);
h{2,2}='FE';
h{3,1}=plot(ax,0,0, 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 1], 'MarkerSize',11);
h{3,2}='BE';
h{4,1}=plot(ax,0,0, 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','blue', 'MarkerSize',9);
h{4,2}='Trap';
h{5,1}=plot(ax,0,0, 'k h-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','red', 'MarkerSize',9);
h{5,2}='RK4';
h{6,1}=plot(ax,0,0, 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','white', 'MarkerSize',7);
h{6,2}='AB2';
h{7,1}=plot(ax,0,0, 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[255 127 0]/256, 'MarkerSize',5);
h{7,2}='Heun';
cellfun(@(h)(set(h,'Visible','off')),h(:,1));
leg=legend(ax,h(:,2),'Position',[0 0 1 1]);
set(leg,'Box','off');
set(leg,'Color','none');
hold off;

pushbutton1_Callback(handles.pushbutton1,eventdata,handles);

end

% --- Outputs from this function are returned to the command line.
function varargout = IB_ODE_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;
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 counter
global h
global f
global x_old
global y_old_FE
global y_old_BE
global y_old_Tr
global y_old_RK4
global y_old_AB2
global y_old_heun
global check1
global check2
global check3
global check4
global check5
global check6

n = length(x_old);
try
    vec = [check1, check2, check3, check4, check5, check6];
    if max(vec) == 1
        axis on
        [x_new,y_new_FE] = theta_method(f,x_old(n), y_old_FE(n), h,1); %FEuler
        [~,y_new_BE] = theta_method(f,x_old(n), y_old_BE(n), h,0); %BEuler
        [~,y_new_Tr] = theta_method(f,x_old(n), y_old_Tr(n), h,0.5); %Trapezoidal
        [~,y_new_RK4] = RK4(f,x_old(n), y_old_RK4(n), h); %RK4
        [~,y_new_AB2] = AB2(f, x_old(n), y_old_AB2(n),h); %AB2
        [~,y_new_heun] = heun(f, x_old(n), y_old_heun(n), h);%Heun
        x_old = [x_old, x_new];

        y_old_FE = [y_old_FE, y_new_FE];
        y_old_BE = [y_old_BE, y_new_BE];
        y_old_Tr = [y_old_Tr, y_new_Tr];
        y_old_RK4 = [y_old_RK4, y_new_RK4];
        y_old_AB2 = [y_old_AB2, y_new_AB2];
        y_old_heun = [y_old_heun, y_new_heun];

        plotter(handles)
        counter = counter + 1;
    end
catch err
    
end
end

function [x_new,y_new] = theta_method(f,x_old, y_old, h, theta)
% General theta method
% Theta must be in [0,1]. 

x_new = x_old + h;

%Solve the implicit equation w.r.t.  y_new (represented by the variable z).
if theta == 1 % Forward Euler
    y_new = y_old + h*f(x_old,y_old);
else % Implicit
    options = optimset('TolFun',1e-12,'Display','off');
    y_new = fsolve(@(z) y_old - z + h*(theta*f(x_old, y_old) + (1 - theta)*f(x_new, z)),0,options);
end
end

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


% --- Executes during object creation, after setting all properties.
function stepsize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to stepsize (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
end


% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)
global iterhistory
switch get(eventdata.NewValue,'Tag')
    case 'radiobutton1' %From beginning
        iterhistory = 1;
        plotter(handles)
    case 'radiobutton2' %Last five iterations
        iterhistory = 2;
        plotter(handles)
end
end



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

% --- Executes during object creation, after setting all properties.
function y_dash_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y_dash (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
end


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

% --- Executes during object creation, after setting all properties.
function sol_exact_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sol_exact (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
end


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

% --- Executes during object creation, after setting all properties.
function x0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x0 (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
end


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

% --- Executes during object creation, after setting all properties.
function y0_CreateFcn(hObject, eventdata, handles)
% hObject    handle to y0 (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
end


% --- Executes on button press in reset_button.
function reset_button_Callback(hObject, eventdata, handles)
% hObject    handle to reset_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global counter
counter = 1;
user_interaction(handles);

end

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


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

h = get(hObject,'string');
switch h
    case 'Plot error'
        set(hObject,'string','Plot graph');
        check = 'err';
        plotter(handles)
    case 'Plot graph'
        set(hObject,'string','Plot error');
        check = 'gr';
        plotter(handles)
end
end

function user_interaction(handles) % data input & error checking
try
    get_user_input(handles);
    clear_error(handles);
    
    delete(allchild(handles.axes1));
    axis(handles.axes1,'off');
catch err
    manage_error(err,handles);
    
    delete(allchild(handles.axes1));
    axis(handles.axes1,'off');
end
end

function clear_error(handles) % after user corrects their error
    set(allchild(handles.methods),'Enable','on');
    set(allchild(handles.uipanel1),'Enable','on');
    set(handles.pushbutton1,'Enable','on');
    set(handles.reset_button,'Enable','on');
    set(handles.errplotbutton,'Enable','on');
end

function manage_error(err,handles) % deal with user error
    set(allchild(handles.methods),'Enable','off');
    set(allchild(handles.uipanel1),'Enable','off');
    set(handles.pushbutton1,'Enable','off');
    set(handles.reset_button,'Enable','off');
    set(handles.errplotbutton,'Enable','off');
    h=msgbox(err.message,'Error','error','modal');
    waitfor(h);
end

function get_user_input(handles)
    global x_old;
    str = get(handles.x0,'string');
    x_0 = str2double(str);
    assert( ~isnan(x_0) && isreal(x_0), 'Invalid initial x value!');
    x_old = x_0;

    global y_old_FE
    global y_old_BE
    global y_old_Tr
    global y_old_RK4
    global y_old_AB2
    global y_old_heun

    str = get(handles.y0,'string');
    y_0 = str2double(str);
    assert( ~isnan(y_0) && isreal(y_0), 'Invalid initial y value!'); 
    y_old_FE = y_0;
    y_old_BE = y_0;
    y_old_Tr = y_0;
    y_old_RK4 = y_0;
    y_old_AB2 = y_0;
    y_old_heun = y_0;
    
    global h
    str = get(handles.stepsize, 'String');
    val = str2double(str);
    assert( ~isnan(val) && isreal(val) && val>0,...
        'Invalid initial x value!');
    h=val;
   
    global y_exact
    str = get(handles.sol_exact,'string');
    if(isempty(str))
        y_exact=[];
    else
        func = inline(str,'x');
        func = @(x)(func(x)); % may not be needed
        func(1); % triggers exception if function is invalid
        y_exact=func;
    end
       
    global f
    str = get(handles.y_dash,'string');
    assert(~isempty(str),'y'' = f cannot be empty!');
    func = inline(str,'x','y');
    func = @(x,y)(func(x,y)); % may not be needed
    func(1,1); % triggers exception if function is invalid
    
    f=func;
end

function plotter(handles)
global x_old
global y_old_FE
global y_old_BE
global y_old_Tr
global y_old_RK4
global y_old_AB2
global y_old_heun
global check
global iterhistory
global y_exact
global check1
global check2
global check3
global check4
global check5
global check6

axes(handles.axes1);
n = length(x_old);
if n>1
    if iterhistory == 2 % Show last 5 iterations
        m = max(n-5,1);
        cla
        hold on
        if strcmp(check,'gr')
            if ~isempty(y_exact)
                plot(x_old(m:n),y_exact(x_old(m:n)), '-','Color','red','LineWidth',2,'MarkerEdgeColor','k', 'MarkerFaceColor','k', 'MarkerSize',10)
            end
            if check1 == 1
                plot(x_old(m:n),y_old_FE(m:n), 'k o-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',13)
            end
            if check2 == 1
                plot(x_old(m:n),y_old_BE(m:n), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 1], 'MarkerSize',11)
            end
            if check3 == 1
                plot(x_old(m:n),y_old_Tr(m:n), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','blue', 'MarkerSize',9)
            end
            if check4 == 1
                plot(x_old(m:n),y_old_RK4(m:n), 'k h-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','red', 'MarkerSize',9)
            end
            if check5 == 1
                plot(x_old(m:n),y_old_AB2(m:n), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','white', 'MarkerSize',7)
            end
            if check6 == 1
                plot(x_old(m:n),y_old_heun(m:n), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[255 127 0]/256, 'MarkerSize',5)
            end
                hold off
            axis image
            axis square
            %legendax('Analytic solution','FE','BE','Trap','RK4','AB2','Heun','Location','SouthOutside');
        elseif strcmp(check,'err')
            if check1 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_FE(m:n)), 'k o-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',13)
            end
            if check2 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_BE(m:n)), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 1], 'MarkerSize',11)
            end
            if check3 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_Tr(m:n)), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','blue', 'MarkerSize',9)
            end
            if check4 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_RK4(m:n)), 'k h-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','red', 'MarkerSize',9)
            end
            if check5 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_AB2(m:n)), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','white', 'MarkerSize',7)
            end
            if check6 == 1
                plot(x_old(m:n),abs(y_exact(x_old(m:n)) - y_old_heun(m:n)), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[255 127 0]/256, 'MarkerSize',5)
            end
            hold off
            axis image
            axis square
            %legendax('Error FE','Error BE','Error Trap','Error RK4','Error AB2','Error Heun','Location','SouthOutside');
        end
    else
        cla
        hold on
        if strcmp(check,'gr')
            disp(y_exact);
            if ~isempty(y_exact)
                plot(x_old,y_exact(x_old), '-','Color','red','LineWidth',2,'MarkerEdgeColor','k', 'MarkerFaceColor','k', 'MarkerSize',10)
            end
            if check1 == 1
                plot(x_old,y_old_FE, 'k o-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',13)
            end
            if check2 == 1
                plot(x_old,y_old_BE, 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 1], 'MarkerSize',11)
            end
            if check3 == 1
                plot(x_old,y_old_Tr, 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','blue', 'MarkerSize',9)
            end
            if check4 == 1
                plot(x_old,y_old_RK4, 'k h-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','red', 'MarkerSize',9)
            end
            if check5 == 1
                plot(x_old,y_old_AB2, 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','white', 'MarkerSize',7)
            end
            if check6 == 1
                plot(x_old,y_old_heun, 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[255 127 0]/256, 'MarkerSize',5)
            end
            hold off
            axis image
            axis square
            %legendax('Analytic solution','FE','BE','Trap','RK4','AB2','Heun','Location','SouthOutside');
        elseif strcmp(check,'err')
            if check1 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_FE), 'k o-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[.49 1 .63], 'MarkerSize',13)
            end
            if check2 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_BE), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[1 0 1], 'MarkerSize',11)
            end
            if check3 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_Tr), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','blue', 'MarkerSize',9)
            end
            if check4 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_RK4), 'k h-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','red', 'MarkerSize',9)
            end
            if check5 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_AB2), 'k d-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor','white', 'MarkerSize',7)
            end
            if check6 == 1
                plot(x_old,abs(y_exact(x_old)-y_old_heun), 'k s-','LineWidth',1,'MarkerEdgeColor','k', 'MarkerFaceColor',[255 127 0]/256, 'MarkerSize',5)
            end
            hold off
            axis image
            axis square
            %legendax('Error FE','Error BE','Error Trap','Error RK4','Error AB2','Error Heun','Location','SouthOutside');
        end
    end
end
end

function[x,y] = RK4(f,x_old, y_old_RK4,h)
n = length(x_old);
k1 = h * feval(f, x_old(n), y_old_RK4(n) );
k2 = h * feval(f, x_old(n) + h/2, y_old_RK4(n) + k1/2 );
k3 = h * feval(f, x_old(n) + h/2, y_old_RK4(n) + k2/2 );
k4 = h * feval(f, x_old(n) + h, y_old_RK4(n) + k3 );
y = y_old_RK4(n) + ( k1 + 2*k2 + 2*k3 + k4 ) / 6;
x = x_old(n) + h;
end

function[x,y] = AB2(f, x_old, y_old_AB2,h)
global counter
global f_0
if counter == 1
    f_0 = feval (f, x_old,y_old_AB2 );
    x_tilde = y_old_AB2 + (2*h/3) * f_0;
    y = y_old_AB2 + (h/4) * f_0 + (3*h/4) * feval (f, x_old + (2*h/3), x_tilde );
    x = x_old + h;
elseif counter > 1
    f_1 = feval (f, x_old, y_old_AB2);
    y = y_old_AB2 + (3*h/2) * f_1 - h/2 * f_0;
	x = x_old + h;
	f_0 = f_1;
end
end

function [x,y] = heun(f, x_old, y_old_heun, h)
    temp = feval(f, x_old, y_old_heun);
	x_tilde = y_old_heun + h*temp;
    y = y_old_heun + (h/2)*(temp + feval(f, x_old + h, x_tilde));
	x = x_old + h;
end


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

% Hint: get(hObject,'Value') returns toggle state of checkbox3
global check1
check1 = get(hObject,'value');
plotter(handles)
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox4
global check2
check2 = get(hObject,'value');
plotter(handles)
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox5
global check3
check3 = get(hObject,'value');
plotter(handles)
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox6
global check4
check4 = get(hObject,'value');
plotter(handles)
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox7
global check5
check5 = get(hObject,'value');
plotter(handles)
end

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

% Hint: get(hObject,'Value') returns toggle state of checkbox8
global check6
check6 = get(hObject,'value');
plotter(handles)
end


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

% Hint: place code in OpeningFcn to populate legendax
end
