function [JJ,KK]= simplexI_n(n,m,A,b)
% Simplexova method: I and II-phase ,b arbitrary (some bi0 <0 ), 
%  cost functional xi to maximize, with xi=c.x; last row in A is
%  [-c],A=(m+1)x n;dim b=m;
%  last column in  AS ((n+1)x(m+1) belongs to y variable (first create equations then 
%i0 row multiply by -1;It is phase I (feasible basis),II for optimum
% The case b>=0 has to be solved by simplexII(n,m,A,b,0,1) (last parameter
% is formal only
q=0;br=0;
for i=1:m
    if b(i)<q
        q=b(i); i0=i
    else
        s=0;
    end
end
cu(1,:)=A(m+1,:); cp(1,:)=A(i0,:);bi0=b(i0);
for i=1:m+1
    A(i,:)=A(i,:)-cp(1,:);
end
xi0=-ones(m+1,1); A(i0,:)=-cp(1,:);xi0(m+1)=1;A(m+1,:)=cp(1,:);
b=b-bi0;b(i0)=-bi0; 
AS=[A xi0]; 
[JJ,KK,bb,id]=simplexII(n+1,m,AS,b,1,bi0);
if abs(bb(m+1))< 10^(-6)
    'feasible basis'
    JJ, KK,bb
else 
    'there is no feasible basis'
    return
end 
%phase II
p=n+i0+1;iz=0;oz=0;
for i=1:m+1
    if JJ(i)==p
        iz=i;
    end
end
for i=1:n+1
    if KK(i)==p
        oz=i;
    end
end
E=eye(m+1,m+1);
ASS=[AS E];
for i=1:m+1
 if JJ(i)> p
    JJ(i)=JJ(i)-1;
end
end
for i=1:n+1
 if KK(i)> p
    KK(i)=KK(i)-1;
end
end
ASS(m+1,[1:n+m+2])=zeros(1,n+m+2);
ASS(m+1,[1:n])=cu(1,:);ASS(m+1,n+m+2)=1;
di=0;
bs=b; bs(m+1)=0;J=JJ;K=KK;
if iz==0
    if oz==1
        K1=[KK(2:n+1)];
       else
    K1=[KK(1:oz-1) KK(oz+1:n+1)];
    end
    'entrance data for original problem' 
    AA=[ASS(:,[1:p-1]) ASS(:,[p+1:n+m+2])];
    J=JJ, K1
    di=0;
    while di==0
    [JJ,KK,bb,di]=simplex_k(n+m+1,m,J,K1,AA,bs);
    if di==2
        return
    end
    J=JJ, K1= KK, di
    end
else
    'variable y is in basis'
    br=1;
end
if br==1
    return
end
'otimal basis'
J, K1
    'optimal solution is'
    bb
    'last component is cost function value' 
  