Created
October 22, 2015 04:34
-
-
Save normalhuman/5d42d40217beb9e64db1 to your computer and use it in GitHub Desktop.
Bisection method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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