Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x = Powell( f,x0,err )
- %UNTITLED Summary of this function goes here
- % Detailed explanation goes here
- d1 = [1;0];
- d2 = [0;1];
- xlast = x0;
- while 1
- ftemp = @(x) f((xlast + x*d1)');
- xcur = xlast + amaterMin(ftemp,-100,100,0.01)*d1;
- ftemp = @(x) f((xcur + x*d2)');
- xnew = xcur + amaterMin(ftemp,-100,100,0.01)*d2;
- d3 = xnew - xlast;
- ftemp = @(x) f((xnew+x*d3)');
- xnewnew = xnew + amaterMin(ftemp,-100,100,0.01)*d3;
- if xlast - xnew < [err;err]
- x = xnew;
- return;
- end
- xlast = xnewnew;
- d1 = d2;
- d2 = d3;
- end
- function rez = amaterMin(fun,a,b,step)
- x1 = a;
- x2 = a + step;
- x3 = a + step + step;
- for xi = a:step:b
- if (fun(x1) > fun(x2) && fun(x2) < fun(x3))
- rez = x2;
- return;
- end
- x1 = x1+step;
- x2 = x2+step;
- x3 = x3+step;
- end
- if fun(a) >= fun(b)
- rez = b;
- else
- rez = a;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement