function [dleft,drighti,bcost] = mainsubg(m,blk,num)
  [nblk,dum] = size(blk);
  nvar = nblk - 1;
    x = zeros(nvar,1);
    bx = zeros(nvar,1);
  blkp = ptrs(blk);
  k = 1;
  [sg,bcost] = subgrad(m,x,blk,blkp);
  x = x - (1/(k*norm(sg)))*sg;
  total = max([80 num*nvar]);
  while k < num*nvar
    [sg,cost] = subgrad(m,x,blk,blkp);
    if cost < bcost
      bcost = cost;
      bx = x;
    end
    k = k + 1;
    x = x - (1/(k*norm(sg)))*sg;
  end
  [nr,nc] = size(m);
  dleft = ones(1,nr);
  drighti = ones(1,nc);
  for i=1:nblk-1
    dleft(blkp(i,2):blkp(i+1,2)-1) = exp(bx(i))*ones(1,blk(i,2));
    drighti(blkp(i,1):blkp(i+1,1)-1) = exp(-bx(i))*ones(1,blk(i,1));
  end
  dleft = diag(dleft);
  drighti = diag(drighti);
%
% Copyright MUSYN INC 1991,  All Rights Reserved
