Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.95 KB | None | 0 0
  1. function [xMin,yMin] = Powell(f, x0, epsilon)
  2.  
  3. u = eye(2);
  4. xPrev = [inf; inf];
  5. x(:, 1) = x0;
  6. n = numel(x0);
  7.  
  8.     while(abs(xPrev - x(:, 1)) > epsilon)
  9.         xPrev = x(:, 1);
  10.  
  11.         for i = 1:n
  12.             g = @(theta)f(x(:, i) + theta*u(:,i));
  13.             theta = ZlatniPresek(g, -100, 100, epsilon);
  14.             x(:, i+1) =  x(:, i) + theta * u(:,i);
  15.         end
  16.  
  17.         u3 = x(:, 3) - x(:, 1);
  18.  
  19.         g = @(theta)f(x(:, 3) + theta*u3);
  20.         theta = ZlatniPresek(g, -100, 100, epsilon);
  21.         x(:, 1) =  x(:, 3) + theta * u3;
  22.  
  23.         u(:,1) = u(:,2);
  24.         u(:,2) = u3;
  25.     end
  26. xMin = x(:, 1);
  27. yMin = f(x(:, 1));
  28. end
  29.  
  30. function [xMin] = ZlatniPresek(f, a, b, e)
  31.  
  32. c = (3 - sqrt(5))/2;
  33.     while(abs((b-a) > e))
  34.         x1 = a + c*(b - a);
  35.         x2 = a + b - x1;
  36.  
  37.         if(f(x1) <= f(x2))
  38.             b = x2;
  39.             xMin = x1;
  40.         else
  41.             a = x1;
  42.             xMin = x2;
  43.         end
  44.     end
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement