Skip to content

Instantly share code, notes, and snippets.

@normalhuman
Created October 22, 2015 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save normalhuman/5d42d40217beb9e64db1 to your computer and use it in GitHub Desktop.
Save normalhuman/5d42d40217beb9e64db1 to your computer and use it in GitHub Desktop.
Bisection method
f = @(x) 55*cos(x)-x;
a = 0;
b = 8;
fa = f(a);
fb = f(b);
if sign(fa) == sign(fb)
error('No bracket');
end
while(b-a > 1e-9)
c = (a+b)/2;
fc = f(c);
if sign(f(c)) == sign(f(b))
b = c; fb = fc;
else
a = c; fa = fc;
end
end
disp(sprintf('Root found at x = %.9f\n', a));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment