function f = fitfun2(lam,Data)
%FITFUN2  Used  by DATDEMO to return errors in fitting data to a function.
%	FITFUN2 is used by DATDEMO.  FITFUN2(lam,Data) returns the error
%	between the data and the values computed by the current
%	function of lam.  FITFUN2 assumes a function of the form
%
%	  y =  c(1)*exp(-lam(1)*t) + ... + c(n)*exp(-lam(n)*t)
%
%	with n linear parameters and n nonlinear parameters.

t = Data(:,1); y = Data(:,2);
A = zeros(length(t),length(lam));
for j = 1:size(lam)
   A(:,j) = exp(-lam(j)*t);
end
c = A\y;
z = A*c;
f = z-y;

% Statements to plot progress of fitting:
plot(t,z,t,y,'o')
xt = max(t)/3;
yt = max(y)/2;
text(xt,1.4*yt,['lambda = ' num2str(lam(1)) '  ' num2str(lam(2))])
text(xt,1.2*yt,['(c = ' num2str(c(1)) '  ' num2str(c(2)),')'])
text(xt,1.0*yt,['err norm = ' sprintf('%g',norm(f))])
