function R=Simplex(B,R,S,t,u,z);
% SIMPLEX Simplex algorithm starting with a simplex table
%   R=Simplex(B,R,S,t,u,z); implements the simplex algorithm starting
%   with a given simplex table consisting of B,R,S,t,u and z and finds
%   the optimal solution.

l=find(u<0);
while length(l)>0
  l=l(1);
  i=find(S(:,l)>0);
  if length(i)<1
    error('solution is unbounded');
  else
    [d,k]=min(t(i)./S(i,l));k=i(k);
    t([1:k-1 k+1:end])=t([1:k-1 k+1:end])-...
                       t(k)*S([1:k-1 k+1:end],l)/S(k,l);
    z=z+t(k)*u(l)/S(k,l);
    t(k)=t(k)/S(k,l);
    u([1:l-1 l+1:end])=u([1:l-1 l+1:end])-...
                       S(k,[1:l-1 l+1:end])*u(l)/S(k,l);
    u(l)=-u(l)/S(k,l);
    S([1:k-1 k+1:end],[1:l-1 l+1:end])=...
                       S([1:k-1 k+1:end],[1:l-1 l+1:end])...
      -S([1:k-1 k+1:end],l)*S(k,[1:l-1 l+1:end])/S(k,l);
    S(k,[1:l-1 l+1:end])=S(k,[1:l-1 l+1:end])/S(k,l);
    S([1:k-1 k+1:end],l)=-S([1:k-1 k+1:end],l)/S(k,l);
    S(k,l)=1/S(k,l);
    d=R(l);R(l)=B(k);B(k)=d;
    l=find(u<0);
    [0 R 0
     B' S  t
     0 u  z]
    pause
  end;
end;