THMMY's "Optimization Techniques" course assignments.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3 KiB

  1. function [a, b, N, n] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
  2. % Fibonacci 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 (the interval of the last step)
  11. % lambda: (number) The lambda value (accuracy)
  12. %
  13. % return:
  14. % a: (vector) Starting points of the interval for each iteration
  15. % b: (vector) Ending points of the interval for each iteration
  16. % N: (number) The number of iterations needed.
  17. % nn: (number) The calls of objective function fun_expr
  18. %
  19. % Error checking
  20. if alpha > beta || lambda <= 0 || epsilon <= 0
  21. error ('Input criteria not met')
  22. end
  23. % Use Binet's formula instead of matlab's recursive fibonacci
  24. % implementation
  25. fibonacci = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
  26. % Init variables
  27. a = alpha;
  28. b = beta;
  29. n = 0;
  30. fun = matlabFunction(fun_expression);
  31. % wrapper call count function
  32. function r = count_fun(x)
  33. n = n + 1;
  34. r = fun(x);
  35. end
  36. % calculate number of iterations
  37. N = 0;
  38. while fibonacci(N) < (b(1) - a(1)) / lambda
  39. N = N + 1;
  40. end
  41. % calculate x1, x2 of the first iteration, since the following iteration
  42. % will not require to calculate both
  43. x_1 = a(1) + (fibonacci(N-2) / fibonacci(N)) * (b(1) - a(1));
  44. x_2 = a(1) + (fibonacci(N-1) / fibonacci(N)) * (b(1) - a(1));
  45. f1 = count_fun(x_1);
  46. f2 = count_fun(x_2);
  47. % All but the last calculation
  48. for k = 1:N-2
  49. % set new search interval
  50. if f1 <= f2
  51. a(k+1) = a(k);
  52. b(k+1) = x_2;
  53. x_2 = x_1;
  54. f2 = f1;
  55. x_1 = a(k+1) + (fibonacci(N-k-2) / fibonacci(N-k)) * (b(k+1) - a(k+1));
  56. f1 = count_fun(x_1);
  57. else
  58. a(k+1) = x_1;
  59. b(k+1) = b(k);
  60. x_1 = x_2;
  61. f1 = f2;
  62. x_2 = a(k+1) + (fibonacci(N-k-1) / fibonacci(N-k)) * (b(k+1) - a(k+1));
  63. f2 = count_fun(x_2);
  64. end
  65. end
  66. % Last calculation
  67. x_2 = x_1 + epsilon;
  68. f2 = count_fun(x_2);
  69. if f1 <= f2
  70. a(N) = a(N-1);
  71. b(N) = x_1;
  72. else
  73. a(N) = x_1;
  74. b(N) = b(N-1);
  75. end
  76. end