Advertisement
UF6

Bisection

UF6
Oct 6th, 2016
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.69 KB | None | 0 0
  1. function p=Bisection(a, b, TOL, N)
  2.  
  3.    if sign(f(a))*sign(f(b))>0
  4.        disp('Error:f(a) and f(b) have the same sign')
  5.        return
  6.    end
  7.    
  8.    i=1;
  9.        
  10.    while i<=N
  11.        p=a+(b-a)/2;
  12.        if abs(f(p))<=eps || (b-a)/2<TOL
  13.            disp(['The error is less than the given tolerance after ' num2str(i) ' iterations'])
  14.            return
  15.        end
  16.        
  17.        if sign(f(a))*sign(f(p))>0
  18.            a=p;
  19.        else
  20.            b=p;
  21.        end
  22.        disp(p)
  23.        i=i+1;
  24.    end
  25.    
  26.    disp(['The method failed after ' num2str(N) ' iterations'])
  27.     function y =f(x)
  28.         y = 2.7*(1+x)^(-361)-1;
  29.        
  30.         function y=f(x)
  31.  y=x^(4)-2*x^(3)-12*x^(2)+16*x-40;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement