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

function [w fval] = multiplicativec(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));
swd = sum(diag(D).^(-1));
iK = V*diag(diag(D).^(-2)/swd)*V';
iter = 1;

while iter < maxiter

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

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