%CHECK2 computes numerical derivates for lqse.m and compares them to 
%       analytical derivatives.  The parameter vectors param and ind
%       and the data matrix, ZVAR, are assumed to be in the workspace.


n = 20;
[l,lt,ld,ldt] = lqse_1b(n,n,param,ind);
del = diag(max(abs(param)*1e-3,1e-8));
clear d
for i=1:length(param);
  [jnk,lp]=lqse_1b(n,n,param+del(:,i),ind);
  [jnk,lm]=lqse_1b(n,n,param-del(:,i),ind);
  if length(lp)>1;
    d(:,i)=(vec(lp)-vec(lm))/(2*del(i,i));
    [ldt(:,i),d(:,i),ldt(:,i)./d(:,i)]
  else
    d(i,1)=(lp-lm)/(2*del(i,i));
    ldt=ldt(:);d=d(:);
    [ldt(1:i),d(1:i),ldt(1:i)./d(1:i)]
  end
end;
