Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. function m = bisection(f, low, high, tol, maxit)
  2. disp('Bisection Method');
  3.  
  4. % Evaluate both ends of the interval
  5.  
  6. y1 = feval(f, low);
  7. y2 = feval(f, high);
  8. i = 0;
  9. j = 0;
  10.  
  11. % Display error and finish if signs are not different
  12. if y1 * y2 > 0
  13. disp('Have not found a change in sign. Will not continue...');
  14. m = 'Error'
  15. return
  16. end
  17.  
  18. % Work with the limits modifying them until you find
  19. % a function close enough to zero.
  20. disp('Iter low high root');
  21. while (abs(high - low) >= tol) && (j<=maxit)
  22.  
  23.  
  24.  
  25.  
  26. j = j + 1;
  27. i = i + 1;
  28. % Find a new value to be tested as a root
  29. m = (high + low)/2;
  30. y3 = feval(f, m);
  31. if y3 == 0
  32. fprintf('Root at x = %f \n\n', m);
  33. return
  34. end
  35. fprintf('%2i \t %f \t %f \t %f \n', i-1, low, high, m);
  36.  
  37. % Update the limits
  38. if y1 * y3 > 0
  39. low = m;
  40. y1 = y3;
  41. else
  42. high = m;
  43. end
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement