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.
 
 

65 line
1.8 KiB

  1. function [a, b, k, n] = min_golden_section(fun_expression, alpha, beta, epsilon, lambda)
  2. % Golden section method for finding the local minimum of a function.
  3. %
  4. % fun_expr: (symbolic expression over x) The symbolic expression of the
  5. % objective function
  6. % alpha: (number) The starting point of the interval in which we seek
  7. % for minimum
  8. % beta: (number) The ending point of the interval in which we seek
  9. % for minimum
  10. % epsilon: (number) The epsilon value
  11. % **note:**
  12. % epsilon in not used in this method, but it is part of the
  13. % method calling interface.
  14. % lambda: (number) The lambda value (accuracy)
  15. %
  16. % return:
  17. % a: (vector) Starting points of the interval for each iteration
  18. % b: (vector) Ending points of the interval for each iteration
  19. % k: (number) The number of iterations
  20. % n: (number) The calls of objective function fun_expr
  21. %
  22. % Error checking
  23. if alpha > beta || lambda <= 0
  24. error ('Input criteria not met')
  25. end
  26. % Init variables
  27. gamma = 0.618;
  28. a = alpha;
  29. b = beta;
  30. fun = matlabFunction(fun_expression);
  31. % calculate x1, x2 of the first iteration, since the following iteration
  32. % will not require to calculate both
  33. k=1;
  34. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  35. x_2 = a(k) + gamma*(b(k) - a(k));
  36. f1 = fun(x_1);
  37. f2 = fun(x_2);
  38. while b(k) - a(k) > lambda
  39. % set new search interval
  40. k = k + 1;
  41. if f1 < f2
  42. a(k) = a(k-1);
  43. b(k) = x_2;
  44. x_2 = x_1;
  45. f2 = f1;
  46. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  47. f1 = fun(x_1);
  48. else
  49. a(k) = x_1;
  50. b(k) = b(k-1);
  51. x_1 = x_2;
  52. f1 = f2;
  53. x_2 = a(k) + gamma*(b(k) - a(k));
  54. f2 = fun(x_2);
  55. end
  56. end
  57. % Set objective function calls
  58. n = k+1;