THMMY's "Optimization Techniques" course assignments.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

34 lines
551 B

  1. function [a, b, k] = bisection(fun, alpha, beta, epsilon, lambda)
  2. %
  3. % Detailed explanation goes here
  4. %
  5. %
  6. % Error checking
  7. if 2*epsilon >= lambda
  8. error ('Convergence criteria not met')
  9. end
  10. % Init output vectors
  11. a = alpha;
  12. b = beta;
  13. k=1;
  14. while b(k) - a(k) > lambda
  15. % bisect [a,b]
  16. mid = (a(k) + b(k)) / 2;
  17. x_1 = mid - epsilon;
  18. x_2 = mid + epsilon;
  19. % set new search reange
  20. k = k + 1;
  21. if fun(x_1) < fun(x_2)
  22. a(k) = a(k-1);
  23. b(k) = x_2;
  24. else
  25. a(k) = x_1;
  26. b(k) = b(k-1);
  27. end
  28. end