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.
 
 

37 line
826 B

  1. function [a, b, k] = min_golden_section(fun_expression, alpha, beta, epsilon, 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. fun = matlabFunction(fun_expression);
  12. % calculate x1, x2 of the first iteration, since the following iteration
  13. % will not require to calculate both
  14. k=1;
  15. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  16. x_2 = a(k) + gamma*(b(k) - a(k));
  17. while b(k) - a(k) > lambda
  18. % set new search interval
  19. k = k + 1;
  20. if fun(x_1) < fun(x_2)
  21. a(k) = a(k-1);
  22. b(k) = x_2;
  23. x_2 = x_1;
  24. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  25. else
  26. a(k) = x_1;
  27. b(k) = b(k-1);
  28. x_1 = x_2;
  29. x_2 = a(k) + gamma*(b(k) - a(k));
  30. end
  31. end