Advertisement
lukicdarkoo

Powell method - Multidimensional Optimization

Nov 28th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.63 KB | None | 0 0
  1. function out = powel(f, x0, e)
  2.     [directions, ~] = size(x0);
  3.     u = eye(directions);
  4.    
  5.     t(:, 1) = x0;
  6.     xNew = x0;
  7.    
  8.     while true
  9.         xOld = xNew
  10.        
  11.         % Pravim u(i)num za i = 1..broj pravaca
  12.         for i = 1:directions
  13.             teta = parabola(f, u(:, i), t(:, i), e);
  14.             t(:, i + 1) = t(:, i) + teta * u(:, i);
  15.         end
  16.        
  17.         % Pravim u(broj pravaca + 1)
  18.         u(:, directions + 1) = t(:, directions + 1) - t(:, 1);
  19.         teta = parabola(f, u(:, directions + 1), t(:, directions + 1), e);
  20.         xNew = t(:, directions + 1) + teta * u(:, directions + 1);
  21.         t(:, 1) = xNew;
  22.        
  23.         for i = 1:directions
  24.             u(:, i) = u(:, i + 1);
  25.         end
  26.        
  27.         if abs(xOld(1) - xNew(1)) < e
  28.             out = xNew;
  29.             break
  30.         end
  31.     end
  32. end
  33.  
  34.  
  35. function xopt = parabola(f, direction, add, e)
  36.     x = [0 0.5 1];
  37.  
  38.     while true
  39.         mata = [1 x(1) x(1).^2;
  40.             1 x(2) x(2).^2;
  41.             1 x(3) x(3).^2
  42.         ];
  43.         matb = [f(x(1)*direction + add);
  44.             f(x(2)*direction + add);
  45.             f(x(3)*direction + add)
  46.         ];
  47.    
  48.         abc = linsolve(mata, matb);
  49.         xopt = -abc(2)/(2*abc(3));
  50.              
  51.         x(4) = xopt;
  52.         [vals indexes] = sort([f(direction*x(1) + add)
  53.             f(direction*x(2) + add)
  54.             f(direction*x(3) + add)
  55.             f(direction*x(4) + add)]);
  56.         x = [x(indexes(1)) x(indexes(2)) x(indexes(3))];
  57.        
  58.         if abs(f(direction*x(1) + add) - f(direction*xopt +add)) < e
  59.             break;
  60.         end
  61.     end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement