% This uses multiplicative algorithm to solve optimal design with D-criterion.
% 
% Jan 12, 2012

function [w fval] = multiplicatived(M,w0,freq,tol,maxiter)

n = sqrt(size(M,1));
M = rearrange(M);
blk{1,1} = 's';
blk{1,2} = n;

w = w0;

[V,D] = svd(smat(blk,M*w));
iMw = V*diag(diag(D).^(-1)/n)*V';
iter = 1;

while iter < maxiter

  dw = M'*svec(blk,iMw);
  
  if mod(iter,freq)==0
    fprintf('iter fval sumw minw term1 %g %g %g %g %g\n', iter, -sum(log(diag(D))), sum(w), min(w), max(dw))
  end
  
  if max(dw) <= (1+tol)   
    break
  end
  
  w = w.*dw;
  
  [V,D] = svd(smat(blk,M*w));
  iMw = V*diag(diag(D).^(-1)/n)*V';
  iter = iter + 1;
end

fval = -sum(log(diag(D)));
fprintf(' termination: iter, fval %g, %g\n',  iter, fval)