canezzy

POWELLZLATNI

Nov 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. %POWELL SA ZLATNIM
  2. function [ x,fx ] = p( fun,x0,tol )
  3. %P Summary of this function goes here
  4. % Detailed explanation goes here
  5. n = length(x0);
  6. e = eye(n);
  7. x = x0; t = x0 + 2*tol;
  8. while abs(t - x) > tol
  9. x = t;
  10. for i = 1 : n
  11. g = @(k) t + k*e(:,i);
  12. c = @(j) fun(g(j));
  13. teta = z(c,-10,10,tol);
  14. t = t + teta * e(:,i);
  15. end
  16. e = [e(:,2:n) t-x];
  17. g = @(k) t + k*e(:,i);
  18. c = @(j) fun(g(j));
  19. teta = z(c,-10,10,tol);
  20. t = t + teta * e(:,i);
  21. end
  22. x = t;
  23. fx = feval(fun,x);
  24.  
  25.  
  26. %ZLATNI
  27. function [ x ] = z( fun,a,b,tol )
  28. %Z Summary of this function goes here
  29. % Detailed explanation goes here
  30. c = (3 - sqrt(5) )/ 2;
  31. x1 = a + c*(b-a);
  32. x2 = a + b - x1;
  33.  
  34. while (b - a) > tol
  35. if feval(fun,x1) > feval(fun,x2)
  36. a = x1;
  37. x1 = x2;
  38. x2 = a + b - x1;
  39. else
  40. b = x2;
  41. x2 = x1;
  42. x1 = a + b - x2;
  43. end
  44. end
  45. if feval(fun,x1) < feval(fun,x2)
  46. x = x1;
  47. else
  48. x = x2;
  49. end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment