function y=szimplex3(X,k)
				% szimplex(X,k) k lepeses Nelder-Mead
				% minimalizalas, X oszlopai az indulo 
				% szimplex csucsai,>1 valtozo
n=size(X,1);
for j=1:k  %x0,x1,xn,u keresese:
   for i=1:n+1 g(i)=f(X(:,i));end;
   if g(1)>g(2) i0=1; x0=X(:,1);i1=2;x1=X(:,2);in=2;xn=x1;
   else i0=2;x0=X(:,2);i1=1;x1=X(:,1);in=1;xn=x1;
   end;
   for i=3:n+1 if g(i)>g(i0) i0=i;x0=X(:,i);i1=i0;x1=x0;
               elseif g(i0)>=g(i)&g(i)>=g(i1) i1=i;x1=X(:,i);
               elseif g(i)<g(in) in=i;xn=X(:,i);
               end;
   end;%lapkozeppont:
Y=X;Y(:,i0)=[];u=sum(Y')/n;u=u';
				%a fo ciklus:
v=2*u-x0;fv=f(v);f0=g(i0);f1=g(i1);fn=g(in);
   if fv<fn w=3*u-2*x0;fw=f(w);
        if fw<fn X(:,i0)=w;
        else X(:,i0)=v;
        end;
   else if fv<f1 X(:,i0)=v;
        else if fv>f0 w=u+(x0-u)/2;
             else w=u+(v-u)/2;
             end;fw=f(w);
             if fw<f0 X(:,i0)=w;
             else for i=1:n+1 X(:,i)=(X(:,i)+X(:,in))/2;
                  end;
             end;
         end;
    end;
end;y=xn;

