function varargout = Variable_Volume_Two_BUN_Measurements(varargin)

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

% Last Modified by GUIDE v2.5 21-May-2009 12:39:53

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

% Choose default command line output for Variable_Volume_Two_BUN_Measurements

handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Variable_Volume_Two_BUN_Measurements_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;



function C1_Callback(hObject, eventdata, handles)
% hObject    handle to C1 (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 C1 as text
%        str2double(get(hObject,'String')) returns contents of C1 as a double
if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function C1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to C1 (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 C2_Callback(hObject, eventdata, handles)
% hObject    handle to C2 (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 C2 as text
%        str2double(get(hObject,'String')) returns contents of C2 as a double
if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function C2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to C2 (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 C3_Callback(hObject, eventdata, handles)
% hObject    handle to C3 (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 C3 as text
%        str2double(get(hObject,'String')) returns contents of C3 as a double
if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function C3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to C3 (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 Rd_Callback(hObject, eventdata, handles)
% hObject    handle to Rd (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 Rd as text
%        str2double(get(hObject,'String')) returns contents of Rd as a double
if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Rd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Rd (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 Ru_Callback(hObject, eventdata, handles)
% hObject    handle to Ru (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 Ru as text
%        str2double(get(hObject,'String')) returns contents of Ru as a double
if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Ru_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Ru (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 Td_Callback(hObject, eventdata, handles)
% hObject    handle to Td (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 Td as text
%        str2double(get(hObject,'String')) returns contents of Td as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Td_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Td (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 Ti_Callback(hObject, eventdata, handles)
% hObject    handle to Ti (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 Ti as text
%        str2double(get(hObject,'String')) returns contents of Ti as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Ti_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Ti (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 N_Callback(hObject, eventdata, handles)
% hObject    handle to N (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 N as text
%        str2double(get(hObject,'String')) returns contents of N as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function N_CreateFcn(hObject, eventdata, handles)
% hObject    handle to N (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 Vd_Callback(hObject, eventdata, handles)
% hObject    handle to Vd (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 Vd as text
%        str2double(get(hObject,'String')) returns contents of Vd as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Vd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Vd (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 Kd_Callback(hObject, eventdata, handles)
% hObject    handle to Kd (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 Kd as text
%        str2double(get(hObject,'String')) returns contents of Kd as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function Kd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Kd (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 G_Callback(hObject, eventdata, handles)
% hObject    handle to G (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 G as text
%        str2double(get(hObject,'String')) returns contents of G as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

% --- Executes during object creation, after setting all properties.
function G_CreateFcn(hObject, eventdata, handles)
% hObject    handle to G (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 Kr_Callback(hObject, eventdata, handles)
% hObject    handle to Kr (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 Kr as text
%        str2double(get(hObject,'String')) returns contents of Kr as a double

if isnan(str2double(get(hObject,'string')))
    errordlg('You must enter a numeric value','Bad Input','modal')
end

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


% --- Executes on button press in solve.
function solve_Callback(hObject, eventdata, handles)
% hObject    handle to solve (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%clc;
%%%%%%%%%%%%%%USER-INPUTS%%%%%%%%%%%%%%%
C1=str2double(get(handles.C1,'String'))/100;%pre dialysis plasma conc, mg/mL
C2=str2double(get(handles.C2,'String'))/100;%post dialysis plasma conc, mg/mL
Rd=str2double(get(handles.Rd,'String'));%amount toxin removed during dialysis,mg 
Ru=str2double(get(handles.Ru,'String'));%amount toxin removed by kidney after dialysis, mg
Bi=str2double(get(handles.Bi,'String')); % Volume Lost Rate in Liter/day
Td=str2double(get(handles.Td,'String'));%dialysis time, min
Ti=2880-Td; %inter-dialysis time, min

maxrun=str2double(get(handles.maxrun,'String')); % prevent program from overflowing

Bi=Bi*1000/24/60; %covert Bi from L/day to ml/min;

if (Bi==0)
    Bi=0.00001; %since the formula reach singularity at Bi=0, an approximation is used here
end

%determine sample collection day
schedule=get(handles.popupmenu1,'Value');  
T1=0;
T7=10080; %total min in a week

switch (schedule)
    case 1  %mid
        T2=T1+Td; T3=T2+Ti;
        T4=T3+Td; T5=T4+Ti+1440;
        T6=T5+Td; 
        Bd1=-(2*1440-Td)*Bi/Td; 
        Bd3=-(2*1440-Td)*Bi/Td;
        Bd5=-(3*1440-Td)*Bi/Td;
        %time in min, Bd1 in ml/min (usually negative)
        %Bd1=rate of volume lost from T1 to T2
       
    case 2  %late
        T2=T1+Td; T3=T2+Ti+1440;
        T4=T3+Td; T5=T4+Ti;
        T6=T5+Td; 
        Bd1=-(2*1440-Td)*Bi/Td;
        Bd3=-(3*1440-Td)*Bi/Td;
        Bd5=-(2*1440-Td)*Bi/Td;
        
    case 3  %early
        T2=T1+Td; T3=T2+Ti;
        T4=T3+Td; T5=T4+Ti;
        T6=T5+Td; 
        Bd1=-(3*1440-Td)*Bi/Td;
        Bd3=-(2*1440-Td)*Bi/Td;
        Bd5=-(2*1440-Td)*Bi/Td;
    
    case 4  %after long interval
        T2=T1+Td; T3=T2+Ti+1440;
        T4=T3+Td; T5=T4+Ti;
        T6=T5+Td; 
        Bd1=-(4*1440-Td)*Bi/Td;
        Bd3=-(3*1440-Td)*Bi/Td;
        Bd5=0;  %arbitrary value, doesn't really matter
        
    case 5  %before long interval
        T2=T1+Td; T3=T2+Ti;
        T4=T3+Td; T5=T4+Ti;
        T6=T5+Td; 
        Bd1=-(3*1440-Td)*Bi/Td;
        Bd3=0;
        Bd5=-(4*1440-Td)*Bi/Td;
end;

%initial guess value
%Vd=str2double(get(handles.Vd,'String'))*1000; %volume of distribution of the toxin RIGHT BEFORE DIALYSIS, mL
%Kd=str2double(get(handles.Kd,'String'));%dialysis clearance, mL/min
%G=str2double(get(handles.G,'String')); %mg/min 
%Kr=str2double(get(handles.Kr,'String'));%residual kidney removal, ml/min

%You Li's scheme of initial guesses
Vd1=(Rd+C2*Bd1*Td)/(C1-C2); %ml,using definition Rd=C1*Vd-C2*Vi, for the linear case
%Vd3=(Rd+C2*Bd3*Td)/(C1-C2);
%Vd5=(Rd+C2*Bd5*Td)/(C1-C2);
%Vd is the volume at t=0 before dialysis begins
Kd=Rd/(((C1+C2)/2)*Td); %ml/min, assuming the concentration vs/ time curve follows an exponential shape
G=(Ru+Rd)/2880; %mg/min, assuming generation rate = total removal rate 
Kr=Ru/(((C1+C2)/2)*2880); %ml/min, simply assuming Kr is much smaller than Kd
Vi=Vd1+Bd1*Td; %ml, volume at Td, right after dialysis ends

%define volume variation rate
%Bi=(f-1)*Vd/Td; % or (Vi-Vd)/Td
%Bi=(1-f)*Vd/Ti; % or (Vd-Vi)/Ti

%3days/week and 2days/week are treated separated

if (schedule<=3) 
%initial guess on concentrations based on the guessed Vd, Kd, G, Kr values
       C3=C2*((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi)));
       
       Bd=Bd3;
       Vd=Vi-Bd3*Td;
       C4=C3*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
        
       C5=C4*((Vi+Bi*(T5-T4))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T5-T4))/Vi)^(-((Kr+Bi)/Bi)));
       
       Bd=Bd5;
       Vd=Vi-Bd5*Td;
       C6=C5*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
       
       C7=C6*((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi)));

%Iteration begins here
accuracy = str2double(get(handles.N,'String'));  %high accuracy requires more computing time
increment=0.1; %initial increment 10%, decreases on each round, this is will make G reach equilibrium faster
run=0;
aTi=0;
while (abs((C1-C7)/C1)>accuracy | run<20) 
while (abs((C1-C7)/C1)>increment | run<20)
    %Vd=(Rd+Ru-G*T3)/(C1-C3);
    
    %renew Vd
    Bd=Bd1;
    Vd1=Bd*Td/((((C1*(Kr+Kd+Bd)-G)/(C2*(Kr+Kd+Bd)-G)).^((Bd/(Kr+Kd+Bd))))-1);
    %Vdfun=@(Vd) (f^((Kd+Kr)*Td/((f-1)*Vd)+1)-(G-C1*(Kd+Kr +(f-1)*Vd/Td))/(G-C2*(Kd+Kr+(f-1)*Vd/Td)));
    %Vd=fzero(Vdfun, Vd);
    
    %Cdfun=@(t) (C1*exp(-(Kr+Kd)/Vd*t)+G/(Kr+Kd)*(1-exp(-(Kr+Kd)/Vd*t)));
    %Cdfun=@(t) (C1*((Vd+(1-Bi)*Vd/Td*t)/Vd)^(-((Kr+Kd+((1-Bi)*Vd/Td))/((1-Bi)*Vd/Td)))+(G/(Kr+Kd+((1-Bi)*Vd/Td)))*(1-(Vd+((1-Bi)*Vd/Td)*t)/Vd)^(-((Kr+Kd+((1-Bi)*Vd/Td))/((1-Bi)*Vd/Td))));  
    
    %renew Kd, Kr
    Bd=Bd1;
    Vd=Vd1;
    Cdfun=@(t) (C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd)*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)))); 
    %governing eq for conc vs. time during dialysis
    
    %Cufun=@(t) (C2*exp(-Kr/Vd*(t-Td))+G/Kr*(1-exp(-Kr/Vd*(t-Td))));
    %Cufun=@(t) (C2*((Vd+(Bi-1)*Vd/(T3+aTi-T2)*(t-Td))/Vd)^(-((Kr+((Bi-1)*Vd/(T3+aTi-T2)))/((Bi-1)*Vd/(T3+aTi-T2))))+(G/(Kr+((Bi-1)*Vd/(T3+aTi-T2))))*(1-(Vd+((Bi-1)*Vd/(T3+aTi-T2))*(t-Td))/Vd)^(-((Kr+((Bi-1)*Vd/(T3+aTi-T2)))/((Bi-1)*Vd/(T3+aTi-T2)))));
    
    Vi=Vd1+Bd1*Td; %updating Vi, since Vi is the variable we're actually interested in finding
    Cufun=@(t) (C2*((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))));
    %Cufun=@(t) (G./(Kr+Bi)-(G-C1.*(Kr+Bi))./((Kr+Bi).*((Vd+Bi.*t)./Vd).^((Kr+Bi)./Bi)));
    %govering eq for conc vs. time between dialysis
    
    Rdfun =@(Kd) (Kd*quadl(Cdfun,0,Td)-Rd);
    %find Kd value that satisfied the integral for Rd 
    
    Rufun =@(Kr) (Kr*quadl(Cdfun,0,Td)+Kr*quadl(Cufun,Td,T3+aTi)-Ru);
    %find Kr value that satisfied the integral for Ru
    
    Kd=fzero(Rdfun,Kd);
   
    Kr=fzero(Rufun,Kr);
     
     %determine C7
     %%%forward eqs
     %C2=(C1*((Vd+Bd*Td)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd).*(1-((Vd+Bd.*Td)/Vd).^(-(Kr+Kd+Bd)/Bd)))); 

     %C3=C2*exp(-Kr/Vd*(T3-T2))+G/Kr*(1-exp(-Kr/Vd*(T3-T2)));
    C3=C2*((Vi+Bi*(T3-T2))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T3-T2))/Vi).^(-((Kr+Bi)/Bi)));    
    
    Bd=Bd3;
    Vd=Vi-Bd3*Td;
    %C4=C3*exp(-(Kr+Kd3)/Vd*Td)+G/(Kr+Kd3)*(1-exp(-(Kr+Kd3)/Vd*Td));
    C4=C3*((Vd+Bd*Td)/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd).^(-((Kr+Kd+Bd)/Bd)));
        
    %C5=C4*exp(-Kr/Vd*(T5-T4))+G/Kr*(1-exp(-Kr/Vd*(T5-T4)));
    C5=C4*((Vi+Bi*(T5-T4))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T5-T4))/Vi).^(-((Kr+Bi)/Bi)));
    
    Bd=Bd5;
    Vd=Vi-Bd5*Td;
    %C6=C5*exp(-(Kr+Kd5)/Vd*Td)+G/(Kr+Kd5)*(1-exp(-(Kr+Kd5)/Vd*Td));
    C6=C5*((Vd+Bd*Td)/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd).^(-((Kr+Kd+Bd)/Bd)));
    
    %C7=C6*exp(-Kr/Vd*(T7-T6))+G/Kr*(1-exp(-Kr/Vd*(T7-T6)));
    C7=C6*((Vi+Bi*(T7-T6))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T6))/Vi).^(-((Kr+Bi)/Bi)));
    %%%%%  
    
    %adjusting G till C7=C1
       if (C7>C1) G=G*(1-increment);%-accuracy/10;
       elseif (C7<C1) G=G*(1+increment);%+accuracy/10;
       end

       %backward eqs
       %C6=(C7-G/Kr*(1-exp(-Kr/Vd*(T7-T6))))/(exp(-Kr/Vd*(T7-T6)));
       %C5=(C6-G/(Kr+Kd)*(1-exp(-(Kr+Kd)/Vd*Td)))/(exp(-(Kr+Kd)/Vd*Td));
       %C4=(C5-G/Kr*(1-exp(-Kr/Vd*(T5-T4))))/(exp(-Kr/Vd*(T5-T4)));
       %C3=(C4-G/(Kr+Kd)*(1-exp(-(Kr+Kd)/Vd*Td)))/(exp(-(Kr+Kd)/Vd*Td));
        
run=run+1;
if (run>=maxrun) 
    error='maximum runs reached. Program ends.'
    return
end

end
increment=increment/10;
end

%output
set(handles.Vd,'String', Vi/1000);
set(handles.Kd,'String', Kd);
set(handles.G,'String', G);
set(handles.Kr,'String', Kr);

%plot concentration versus time graph using the most updated parameter
%unit of concentration are now converted to mg/dl from mg/ml

%Cdfun12=@(t) 100*(C1*exp(-(Kr+Kd)/Vd*(t-T1))+G/(Kr+Kd)*(1-exp(-(Kr+Kd)/Vd*(t-T1))));
%Bi=(Bi-1)*Vd/Td;
Bd=Bd1;
Vd=Vd1;
Cdfun12=@(t) 100*(C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)));

%Cufun23=@(t) 100*(C2*exp(-Kr/Vd*(t-T2))+G/Kr*(1-exp(-Kr/Vd*(t-T2))));
Cufun23=@(t) 100*(C2*((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))));
           
Bd=Bd3;
Vd=Vi-Bd3*Td;
%Cdfun34=@(t) 100*(C3*exp(-(Kr+Kd3)/Vd*(t-T3))+G/(Kr+Kd3)*(1-exp(-(Kr+Kd3)/Vd*(t-T3))));
Cdfun34=@(t) 100*(C3*((Vd+Bd*(t-T3))/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*(t-T3))/Vd).^(-((Kr+Kd+Bd)/Bd))));
    
%Cufun45=@(t) 100*(C4*exp(-Kr/Vd*(t-T4))+G/Kr*(1-exp(-Kr/Vd*(t-T4))));
Cufun45=@(t) 100*(C4*((Vi+Bi*(t-T4))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T4))/Vi).^(-((Kr+Bi)/Bi))));
    
Bd=Bd5;
Vd=Vi-Bd5*Td;
%Cdfun56=@(t) 100*(C5*exp(-(Kr+Kd5)/Vd*(t-T5))+G/(Kr+Kd5)*(1-exp(-(Kr+Kd5)/Vd*(t-T5))));
Cdfun56=@(t) 100*(C5*((Vd+Bd*(t-T5))/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*(t-T5))/Vd).^(-((Kr+Kd+Bd)/Bd))));
%C6=C5*exp(-(Kr+Kd)/Vd*Td)+G/(Kr+Kd)*(1-exp(-(Kr+Kd)/Vd*Td));

Cufun67=@(t) 100*(C6*((Vi+Bi*(t-T6))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T6))/Vi).^(-((Kr+Bi)/Bi))));

%calculate time average concentration
TAC_d=(quadl(Cdfun12,T1,T2)+quadl(Cdfun34,T3,T4)+quadl(Cdfun56,T5,T6))/((T2-T1)+(T4-T3)+(T6-T5));
TAC_w=(quadl(Cdfun12,T1,T2)+quadl(Cufun23,T2,T3)+quadl(Cdfun34,T3,T4)+quadl(Cufun45,T4,T5)+quadl(Cdfun56,T5,T6)+quadl(Cufun67,T6,T7))/T7;

set(handles.edit14,'String', TAC_d);
set(handles.edit15,'String', TAC_w);

axes(handles.axes1)
fplot(Cdfun12,[T1,T2],1e-6)
hold on;
fplot(Cufun23,[T2,T3],1e-6)
hold on;

fplot(Cdfun34,[T3,T4],1e-6)
hold on;
fplot(Cufun45,[T4,T5],1e-6)
hold on;

fplot(Cdfun56,[T5,T6], 1e-6)
hold on;
fplot(Cufun67,[T6,T7],1e-6)

xlabel('time(min)') 
ylabel('Plasma Solute Concentration (mg/dl)')
axis([T1,T7,0, +Inf]);

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%for 2days/week 3-4 
if (schedule==4)
    %calculate initial C7
       C3=C2*((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi)));
       
       Bd=Bd3;
       Vd=Vi-Bd3*Td;
       C4=C3*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
        
       C7=C4*((Vi+Bi*(T7-T4))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T4))/Vi)^(-((Kr+Bi)/Bi)));
       
%Iteration begins here
accuracy = str2double(get(handles.N,'String'));  %high accuracy requires more computing time
increment=0.1; %initial increment 10%, decreases on each round, this is will make G reach equilibrium faster
run=0;
aTi=0;
while (abs((C1-C7)/C1)>accuracy | run<20)
while (abs((C1-C7)/C1)>increment | run<20)
    %renew Vd
    %Vdfun=@(Vd) (f^((Kd+Kr)*Td/((f-1)*Vd)+1)-(G-C1*(Kd+Kr +(f-1)*Vd/Td))/(G-C2*(Kd+Kr+(f-1)*Vd/Td)));
    %Vd=fzero(Vdfun, Vd);
    Bd=Bd1;
    Vd1=Bd*Td/((((C1*(Kr+Kd+Bd)-G)/(C2*(Kr+Kd+Bd)-G)).^((Bd/(Kr+Kd+Bd))))-1);
    
    %renew Kd, Kr
    Bd=Bd1;
    Vd=Vd1;
    Cdfun=@(t) (C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd)*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)))); 
    
    Vi=Vd1+Bd1*Td;
    Cufun=@(t) (C2*((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))));
    
    Rdfun =@(Kd) (Kd*quadl(Cdfun,0,Td)-Rd);
    Rufun =@(Kr) (Kr*quadl(Cdfun,0,Td)+Kr*quadl(Cufun,Td,T3+aTi)-Ru);
    Kd=fzero(Rdfun,Kd);
    Kr=fzero(Rufun,Kr);
     

    %determine C7
     C3=C2*((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T3-T2))/Vi)^(-((Kr+Bi)/Bi)));
       
     Bd=Bd3;
     Vd=Vi-Bd3*Td;
     C4=C3*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
        
     C7=C4*((Vi+Bi*(T7-T4))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T4))/Vi)^(-((Kr+Bi)/Bi)));
     
     %adjusting G till C7=C1
       if (C7>C1) G=G*(1-increment);%-accuracy/10;
       elseif (C7<C1) G=G*(1+increment);%+accuracy/10;
       end

run=run+1;
if (run>=maxrun) 
    error='maximum runs reached. Program ends.'
    return
end

end
increment=increment/10;
end

%output
set(handles.Vd,'String', Vi/1000);
set(handles.Kd,'String', Kd);
set(handles.G,'String', G);
set(handles.Kr,'String', Kr);

%plot concentration versus time graph using the most updated parameter
%unit of concentration are now converted to mg/dl from mg/ml
Bd=Bd1;
Vd=Vd1;
Cdfun12=@(t) 100*(C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)));

Cufun23=@(t) 100*(C2*((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))));
           
Bd=Bd3;
Vd=Vi-Bd3*Td;
Cdfun34=@(t) 100*(C3*((Vd+Bd*(t-T3))/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*(t-T3))/Vd).^(-((Kr+Kd+Bd)/Bd))));
    
Cufun47=@(t) 100*(C4*((Vi+Bi*(t-T4))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T4))/Vi).^(-((Kr+Bi)/Bi))));
   
%calculate time average concentration
TAC_d=(quadl(Cdfun12,T1,T2)+quadl(Cdfun34,T3,T4))/((T2-T1)+(T4-T3));
TAC_w=(quadl(Cdfun12,T1,T2)+quadl(Cufun23,T2,T3)+quadl(Cdfun34,T3,T4)+quadl(Cufun47,T4,T7))/T7;

set(handles.edit14,'String', TAC_d);
set(handles.edit15,'String', TAC_w);

axes(handles.axes1)
fplot(Cdfun12,[T1,T2],1e-6)
hold on;
fplot(Cufun23,[T2,T3],1e-6)
hold on;

fplot(Cdfun34,[T3,T4],1e-6)
hold on;
fplot(Cufun47,[T4,T7],1e-6)
hold on;

xlabel('time(min)') 
ylabel('Plasma Solute Concentration (mg/dl)')
axis([T1,T7,0, +Inf]);

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%for 2days/week 4-3 
if (schedule==5)
    %calculate initial C7
       C5=C2*((Vi+Bi*(T5-T2))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T5-T2))/Vi)^(-((Kr+Bi)/Bi)));
       
       Bd=Bd5;
       Vd=Vi-Bd5*Td;
       C6=C5*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
       
       C7=C6*((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi)));
       
%Iteration begins here
accuracy = str2double(get(handles.N,'String'));  %high accuracy requires more computing time
increment=0.1; %initial increment 10%, decreases on each round, this is will make G reach equilibrium faster
run=0;
aTi=0;
while (abs((C1-C7)/C1)>accuracy | run<20)
while (abs((C1-C7)/C1)>increment | run<20)
    %renew Vd
    %Vdfun=@(Vd) (f^((Kd+Kr)*Td/((f-1)*Vd)+1)-(G-C1*(Kd+Kr +(f-1)*Vd/Td))/(G-C2*(Kd+Kr+(f-1)*Vd/Td)));
    %Vd=fzero(Vdfun, Vd);
    Bd=Bd1;
    Vd1=Bd*Td/((((C1*(Kr+Kd+Bd)-G)/(C2*(Kr+Kd+Bd)-G)).^((Bd/(Kr+Kd+Bd))))-1);
    
    %renew Kd, Kr
    Bd=Bd1;
    Vd=Vd1;
    Cdfun=@(t) (C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd)*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)))); 
    
    Vi=Vd1+Bd1*Td;
    Cufun=@(t) (C2*((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-Td))/Vi).^(-((Kr+Bi)/Bi))));
    
    Rdfun =@(Kd) (Kd*quadl(Cdfun,0,Td)-Rd);
    Rufun =@(Kr) (Kr*quadl(Cdfun,0,Td)+Kr*quadl(Cufun,Td,T5)-Ru);
    Kd=fzero(Rdfun,Kd);
    Kr=fzero(Rufun,Kr);
     

    %determine C7
       C5=C2*((Vi+Bi*(T5-T2))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T5-T2))/Vi)^(-((Kr+Bi)/Bi)));
       
       Bd=Bd5;
       Vd=Vi-Bd5*Td;
       C6=C5*((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*Td)/Vd)^(-((Kr+Kd+Bd)/Bd)));
       
       C7=C6*((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(T7-T6))/Vi)^(-((Kr+Bi)/Bi)));
     
     %adjusting G till C7=C1
       if (C7>C1) G=G*(1-increment);%-accuracy/10;
       elseif (C7<C1) G=G*(1+increment);%+accuracy/10;
       end

run=run+1;
if (run>=maxrun) 
    error='maximum runs reached. Program ends.'
    return
end

end
increment=increment/10;
end

%output
set(handles.Vd,'String', Vi/1000);
set(handles.Kd,'String', Kd);
set(handles.G,'String', G);
set(handles.Kr,'String', Kr);

%plot concentration versus time graph using the most updated parameter
%unit of concentration are now converted to mg/dl from mg/ml
Bd=Bd1;
Vd=Vd1;
Cdfun12=@(t) 100*(C1*((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*t)/Vd).^(-(Kr+Kd+Bd)/Bd)));

Cufun25=@(t) 100*(C2*((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T2))/Vi).^(-((Kr+Bi)/Bi))));
           
Bd=Bd5;
Vd=Vi-Bd5*Td;
Cdfun56=@(t) 100*(C5*((Vd+Bd*(t-T5))/Vd).^(-((Kr+Kd+Bd)/Bd))+(G/(Kr+Kd+Bd))*(1-((Vd+Bd*(t-T5))/Vd).^(-((Kr+Kd+Bd)/Bd))));
    
Cufun67=@(t) 100*(C6*((Vi+Bi*(t-T6))/Vi).^(-((Kr+Bi)/Bi))+(G/(Kr+Bi))*(1-((Vi+Bi*(t-T6))/Vi).^(-((Kr+Bi)/Bi))));
   
%calculate time average concentration
TAC_d=(quadl(Cdfun12,T1,T2)+quadl(Cdfun56,T5,T6))/((T2-T1)+(T6-T5));
TAC_w=(quadl(Cdfun12,T1,T2)+quadl(Cufun25,T2,T5)+quadl(Cdfun56,T5,T6)+quadl(Cufun67,T6,T7))/T7;

set(handles.edit14,'String', TAC_d);
set(handles.edit15,'String', TAC_w);

axes(handles.axes1)
fplot(Cdfun12,[T1,T2],1e-6)
hold on;
fplot(Cufun25,[T2,T5],1e-6)
hold on;

fplot(Cdfun56,[T5,T6],1e-6)
hold on;
fplot(Cufun67,[T6,T7],1e-6)
hold on;

xlabel('time(min)') 
ylabel('Plasma Solute Concentration (mg/dl)')
axis([T1,T7,0, +Inf]);
end

hold off;
datacursormode on;
     
     


% --- 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)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- 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





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


% --- Executes during object creation, after setting all properties.
function edit14_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit14 (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 edit15 (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 edit15 as text
%        str2double(get(hObject,'String')) returns contents of edit15 as a double


% --- Executes during object creation, after setting all properties.
function edit15_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit15 (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 Bi_Callback(hObject, eventdata, handles)
% hObject    handle to Bi (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 Bi as text
%        str2double(get(hObject,'String')) returns contents of Bi as a double


% --- Executes during object creation, after setting all properties.
function Bi_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Bi (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 maxrun_Callback(hObject, eventdata, handles)
% hObject    handle to maxrun (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 maxrun as text
%        str2double(get(hObject,'String')) returns contents of maxrun as a double


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


