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.
 
 

55 line
1.3 KiB

  1. function [a, b, N] = min_fibonacci(fun_expression, alpha, beta, epsilon, lambda)
  2. %
  3. % Use Binet's formula instead of matlab's recursive fibonacci
  4. % implementation
  5. fib = @(n) ( ((1 + sqrt(5))^n - (1 - sqrt(5))^n) / (2^n * sqrt(5)) );
  6. % Error checking
  7. if lambda <= 0 || epsilon <= 0
  8. error ('Convergence criteria not met')
  9. end
  10. % Init variables
  11. a = alpha;
  12. b = beta;
  13. fun = matlabFunction(fun_expression);
  14. % calculate number of iterations
  15. N=0;
  16. while fibonacci(N) < (b(1) - a(1)) / lambda
  17. N = N + 1;
  18. end
  19. % calculate x1, x2 of the first iteration, since the following iteration
  20. % will not require to calculate both
  21. x_1 = a(1) + (fib(N-2) / fib(N)) * (b(1) - a(1));
  22. x_2 = a(1) + (fib(N-1) / fib(N)) * (b(1) - a(1));
  23. % All but the last calculation
  24. for k = 1:N-2
  25. % set new search interval
  26. if fun(x_1) < fun(x_2)
  27. a(k+1) = a(k);
  28. b(k+1) = x_2;
  29. x_2 = x_1;
  30. x_1 = a(k+1) + (fib(N-k-2) / fib(N-k)) * (b(k+1) - a(k+1));
  31. else
  32. a(k+1) = x_1;
  33. b(k+1) = b(k);
  34. x_1 = x_2;
  35. x_2 = a(k+1) + (fib(N-k-1) / fib(N-k)) * (b(k+1) - a(k+1));
  36. end
  37. end
  38. % Last calculation
  39. x_2 = x_1 + epsilon;
  40. if fun(x_1) < fun(x_2)
  41. a(N) = a(N-1);
  42. b(N) = x_1;
  43. else
  44. a(N) = x_1;
  45. b(N) = b(N-1);
  46. end