Numerical Sheet#1 Bisection Method

function [ x,n ] = findRootBisection(f,a,b,epson,loops)

    f_a = feval(f,a);
    f_b = feval(f,b);

    if(f_a*f_b >0)
        error('Cant find Root with f(a)*f(b) > 0 ! ');
    end

    for i = 1:loops
       c = (a+b)/2;
       f_c = feval(f,c);

       if(abs(f_c)< epson || abs(a-b)<epson)
           break;
       elseif(f_c*f_b > 0)
           b=c;
           f_b=f_c;
       elseif(f_a*f_c > 0)
           a=c;
           f_a=f_c;
       end

    end

    x=(a+b)/2;
    n =i;
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: