Skip to content

Instantly share code, notes, and snippets.

@dmadisetti
Last active August 29, 2015 14:06
Show Gist options
  • Save dmadisetti/0e057c97baa5e088fd0c to your computer and use it in GitHub Desktop.
Save dmadisetti/0e057c97baa5e088fd0c to your computer and use it in GitHub Desktop.
% Second stab at matlab
function out = biSect(funct,hi,lo,x)
if hi < lo,
err = MException('MATLAB:ambiguousSyntax','Usage biSect(funct,hi,lo,x). hi cannot be lower rhar low.');
throw(err);
end
new = lo + (hi - lo)/2;
apprx = funct(new);
disp(abs((new - x)/2));
disp(apprx);
% Let's check to see if it hit the needed level
if abs((new - x)/2) < 0.01 || apprx == 0,
out = new;
return;
end
if apprx >= 0,
hi = new;
elseif apprx < 0,
lo = new;
end
out = biSect(funct,hi,lo,new);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment