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.

преди 3 седмици
преди 3 седмици
преди 3 седмици
преди 3 седмици
преди 3 седмици
преди 3 седмици
преди 3 седмици
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. n = 0;
  31. fun = matlabFunction(fun_expression);
  32. % wrapper call count function
  33. function r = count_fun(x)
  34. n = n + 1;
  35. r = fun(x);
  36. end
  37. % calculate x1, x2 of the first iteration, since the following iteration
  38. % will not require to calculate both
  39. k=1;
  40. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  41. x_2 = a(k) + gamma*(b(k) - a(k));
  42. f1 = count_fun(x_1);
  43. f2 = count_fun(x_2);
  44. while b(k) - a(k) > lambda
  45. % set new search interval
  46. k = k + 1;
  47. if f1 <= f2
  48. a(k) = a(k-1);
  49. b(k) = x_2;
  50. x_2 = x_1;
  51. f2 = f1;
  52. x_1 = a(k) + (1 - gamma)*(b(k) - a(k));
  53. f1 = count_fun(x_1);
  54. else
  55. a(k) = x_1;
  56. b(k) = b(k-1);
  57. x_1 = x_2;
  58. f1 = f2;
  59. x_2 = a(k) + gamma*(b(k) - a(k));
  60. f2 = count_fun(x_2);
  61. end
  62. end
  63. end