Advertisement
Guest User

zadatakMatlab

a guest
Dec 1st, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.09 KB | None | 0 0
  1. function x = Powell( f,x0,err )
  2. %UNTITLED Summary of this function goes here
  3. %   Detailed explanation goes here
  4.  
  5. d1 = [1;0];
  6. d2 = [0;1];
  7. xlast = x0;
  8.  
  9. while 1
  10.     ftemp = @(x) f((xlast + x*d1)');
  11.     xcur = xlast + amaterMin(ftemp,-100,100,0.01)*d1;
  12.    
  13.     ftemp = @(x) f((xcur + x*d2)');
  14.     xnew = xcur + amaterMin(ftemp,-100,100,0.01)*d2;
  15.    
  16.     d3 = xnew - xlast;
  17.    
  18.     ftemp = @(x) f((xnew+x*d3)');
  19.     xnewnew = xnew + amaterMin(ftemp,-100,100,0.01)*d3;
  20.    
  21.     if xlast - xnew < [err;err]
  22.         x = xnew;
  23.         return;
  24.     end
  25.    
  26.     xlast = xnewnew;
  27.     d1 = d2;
  28.     d2 = d3;
  29.    
  30. end
  31.  
  32.     function rez = amaterMin(fun,a,b,step)
  33.         x1 = a;
  34.         x2 = a + step;
  35.         x3 = a + step + step;
  36.         for xi = a:step:b
  37.             if (fun(x1) > fun(x2) && fun(x2) < fun(x3))
  38.                 rez = x2;
  39.                 return;
  40.             end  
  41.             x1 = x1+step;
  42.             x2 = x2+step;
  43.             x3 = x3+step;
  44.         end
  45.         if fun(a) >= fun(b)
  46.             rez = b;
  47.         else
  48.             rez = a;
  49.         end
  50.     end
  51.  
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement