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.

75 lines
2.1 KiB

  1. function [a, b, N, nn] = 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. fun = matlabFunction(fun_expression);
  30. % calculate number of iterations
  31. N = 0;
  32. while fibonacci(N) < (b(1) - a(1)) / lambda
  33. N = N + 1;
  34. end
  35. % calculate x1, x2 of the first iteration, since the following iteration
  36. % will not require to calculate both
  37. x_1 = a(1) + (fibonacci(N-2) / fibonacci(N)) * (b(1) - a(1));
  38. x_2 = a(1) + (fibonacci(N-1) / fibonacci(N)) * (b(1) - a(1));
  39. % All but the last calculation
  40. for k = 1:N-2
  41. % set new search interval
  42. if fun(x_1) < fun(x_2)
  43. a(k+1) = a(k);
  44. b(k+1) = x_2;
  45. x_2 = x_1;
  46. x_1 = a(k+1) + (fibonacci(N-k-2) / fibonacci(N-k)) * (b(k+1) - a(k+1));
  47. else
  48. a(k+1) = x_1;
  49. b(k+1) = b(k);
  50. x_1 = x_2;
  51. x_2 = a(k+1) + (fibonacci(N-k-1) / fibonacci(N-k)) * (b(k+1) - a(k+1));
  52. end
  53. end
  54. % Last calculation
  55. x_2 = x_1 + epsilon;
  56. if fun(x_1) < fun(x_2)
  57. a(N) = a(N-1);
  58. b(N) = x_1;
  59. else
  60. a(N) = x_1;
  61. b(N) = b(N-1);
  62. end
  63. % Set objective function calls
  64. nn = 2*N -2;