function fitnplot(t,x,plotnumber,filename,varname,vartype)
% Given array t,x , fits x(t) to fitfcn
% and plots it and residuals
%
n=length(t)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% What function to fit to:
fitfcn = @(alpha,beta,t)alpha*(1.0-exp(-beta*t));
%
[x0,alpha0,beta0]=fitalpha(t,x)
% x0=x(1) alpha, beta from fit.
Asysmptoticvalue=alpha0+x0
timeconstant=1./beta0
% %%%%%%%%%%%%% Now plot data and fitted curve:
figure(plotnumber)
subplot(2,1,1);
plot(t, x,'*')
xlabel('Time [minutes]');
ylabel(vartype);
hold on
plot(t,fitfcn(alpha0,beta0,t)+x0,'r');
title([filename,varname,'Fit to ',func2str(fitfcn)]);
legend([varname,'data'],'fit','Location','East');
%
subplot(2,1,2);
plot(t,x-fitfcn(alpha0,beta0,t)-x0,'+g')
legend('Residual','Location','SouthEast');
xlabel('Time [minutes]');
ylabel(['\Delta',vartype]);
grid on
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end