%Program 1.1 Bisection Method
%Computes approximate solution of f(x)=0
%Input: inline function f; a,b such that f(a)*f(b)<0, 
%     and tolerance tol
%Output: Approximate solution xc
% 3.576749988831580
% 79.999999994442874
function xc = bisectCool(a,b,tol,K)
%f =@(x) max(max(coolForBisect(0,4,0,4,20,20,x,1.68,.005))) -80 ;

%f =@(x) max(max(coolForBisect(0,4,0,4,10,10,x,3.85,.005))) -80 ;

%f =@(x) max(max(coolForBisect(0,4,0,4,20,20,x,K,.005))) -79.9999999 ;

                                            %H is different here
f =@(x) max(max(coolForBisect(0,4,0,4,20,20,x,K,.1))) -79.9999999 ;

% for plotting

x = a:.1:b;
y = arrayfun(f,x);

hold on;
plot([a,b],[80,80],'g');
plot(x,(y+80));



if sign(f(a))*sign(f(b)) >= 0
  error('f(a)f(b)<0 not satisfied!') %ceases execution
end
fa=f(a);
fb=f(b);
k = 0;
while (b-a)/2>tol
  c=(a+b)/2;
  fc=f(c);
  if fc == 0              %c is a solution, done
    break
  end
  if sign(fc)*sign(fa)<0  %a and c make the new interval
    b=c;fb=fc;
  else                    %c and b make the new interval
    a=c;fa=fc;
  end
end
xc=(a+b)/2;               %new midpoint is best estimate