diff --git a/bisection.m b/bisection.m new file mode 100644 index 0000000000000000000000000000000000000000..bf28d53a4f65f68139a73acfd07b4b3171c2895c --- /dev/null +++ b/bisection.m @@ -0,0 +1,23 @@ +function p = bisection(f,a,b) + +% provide the equation you want to solve with R.H.S = 0 form. +% Write the L.H.S by using inline function +% Give initial guesses. +% Solves it by method of bisection. +% A very simple code. But may come handy + +if f(a)*f(b)>0 + disp('Wrong choice bro') +else + p = (a + b)/2; + err = abs(f(p)); + while err > 1e-7 + if f(a)*f(p)<0 + b = p; + else + a = p; + end + p = (a + b)/2; + err = abs(f(p)); + end +end