THMMY's "Optimization Techniques" course assignments.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

min_bisection.m 1.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. function [a, b, k, n] = min_bisection(fun_expr, alpha, beta, epsilon, lambda)
  2. % Bisection 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 (distance from midpoint)
  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. % k: (number) The number of iterations
  17. % n: (number) The calls of objective function fun_expr
  18. %
  19. % Error checking
  20. if alpha > beta || 2*epsilon >= lambda || lambda <= 0
  21. error ('Input criteria not met')
  22. end
  23. % Init
  24. a = alpha;
  25. b = beta;
  26. n = 0;
  27. fun = matlabFunction(fun_expr);
  28. % wrapper call count function
  29. function r = count_fun(x)
  30. n = n + 1;
  31. r = fun(x);
  32. end
  33. k=1;
  34. while b(k) - a(k) > lambda
  35. % bisect [a,b]
  36. mid = (a(k) + b(k)) / 2;
  37. x_1 = mid - epsilon;
  38. x_2 = mid + epsilon;
  39. % set new search interval
  40. k = k + 1;
  41. if count_fun(x_1) < count_fun(x_2)
  42. a(k) = a(k-1);
  43. b(k) = x_2;
  44. else
  45. a(k) = x_1;
  46. b(k) = b(k-1);
  47. end
  48. end
  49. end