Advertisement
ruhan008

Bisection

Aug 22nd, 2024
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.34 KB | None | 0 0
  1. function x = bisection(a, b, n)
  2.    
  3.     c = f(a);
  4.     d = f(b);
  5.  
  6.     if c * d > 0
  7.         error('The function has same sign on both the sides')
  8.     end
  9.        
  10.     for i = 1 : n
  11.  
  12.         x = (a + b) / 2;
  13.  
  14.         if f(a) * f(x) < 0
  15.             b = x;
  16.         elseif f(b) * f(x) < 0
  17.             a = x;
  18.         end
  19.     end
  20. end
  21.  
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement