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.

golden_section.m 730 B

12345678910111213141516171819202122232425262728293031323334353637
  1. function [a, b, k] = golden_section(fun, alpha, beta, lambda)
  2. %
  3. % Error checking
  4. if lambda <= 0
  5. error ('Convergence criteria not met')
  6. end
  7. % Init variables
  8. gamma = 0.618;
  9. a = alpha;
  10. b = beta;
  11. % calculate x1, x2 of the first iteration, since the following iteration
  12. % will not require to calculate both
  13. k=1;
  14. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  15. x_2 = a(k) + gamma*(b(k) - a(k));
  16. while b(k) - a(k) > lambda
  17. % set new search interval
  18. k = k + 1;
  19. if fun(x_1) < fun(x_2)
  20. a(k) = a(k-1);
  21. b(k) = x_2;
  22. x_2 = x_1;
  23. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  24. else
  25. a(k) = x_1;
  26. b(k) = b(k-1);
  27. x_1 = x_2;
  28. x_2 = a(k) + gamma*(b(k) - a(k));
  29. end
  30. end