Coding a bisection algorithm using matlab (numerical analysis)

813 Views Asked by At

To code the bisection algorithm. We assume that f at the two end-points a and b are of different signs. We will return the approximated solution when $b_{n}$ − $a_{n}$ < 2ε or when we have found the exact root.

% bisection.m

function root=bisection(a,b,epsilon)
  fa=fun(a);  
  err = (b-a)/2;
  p=a+err;
  fp=fun(p);

  while and((fp∼=0),(err>=epsilon))
    if fa*fp < 0
      b=p;
    else
      a=p;
      fa=fp;
    end;
    err=err/2;
    p=a+err;
    fp=fun(p);
  end;

  root=p;

Modify the code so that we will stop when |f(p)| < ε instead

What i tried

I think that it is form this part of the code onwards that need to be modified

while and((fp∼=0),(err>=epsilon))

So instead of having err>=eplison we make the error less than elipson. Now i know the code makes use of thw while loop and im finding ways on how to stop the while loop in order to stop the code. Could anyone help me in this. Thanks