MatLab Bisection method - updating the limit for each iteration[help]

278 Views Asked by At

So below I have a written function emplyoing a bisection algroithm for finding roots. One issue I have(if you can't find more that will say) is that I don't know how to incorporate a new limit for each loop(iteration).

You can see below where I've written "%update limit".

function [root, iter]= bisectoin(f,a,b,TOL)
i= 0;
while le(abs(a-b),TOL)
    m=(a+b)./2;
    if f(m)==0
        a=m;
        b=m;
    end
    if f(m)*f(b)<0
        a=m;
        %Update limit    
    if f(a)*f(m) <0   
        b=m;
       %Update limit

    end
       i = i+1;       
end
end

Latest version of code

function [root,iter]= bisectoin(f,a,b,TOL)
iter= 0;
while ge(abs(a-b),TOL)
    m=(a+b)./2;
    if f(m)==0
        a=m;
        b=m;
    end
    if f(m)*f(b)<0
        a=m;

    if f(a)*f(m) <0   
        b=m; 
    end
       iter = iter+1;

    end
end
root = m;
end