Wednesday, May 9, 2012

Bisection Method using Matlab



function[y]=f(x)
y=(x*x*x)-(2*x)-5;

Save the above function as f.m.



function bisection(a,b)
if(f(b)<f(a))
    m=a;
    a=b;
    b=m;
end
if((f(a)>0 && f(b)>0)||(f(a)<0 && f(b)<0))
    fprintf('\nNo root present!!!\n');
    return;
end
c=1;
while((abs(a-b)>0.001)&&c~=50)
    c=c+1;
    m=(a+b)/2;
    if(f(m)>0)
        b=m;
    end
    if(f(m)<0)
        a=m;
    end
end
  fprintf('\nRoot=%f\n',m);

Arguments sent to the function are:- bisection(lower approx root,upper approx root)

Reactions:

0 comments:

Post a Comment