function [x0,alpha,beta]=fitalpha(t,x)
% given an array t,x, this function
% returns the x0,alpha,beta from a fit to
%  x0+ alpha*(1.0-exp(-beta*t))
%  where x0 is the first entry in x
%
x0=x(1);
n=size(x)  % this is (number 1) for number*1 array
xn=x(n(1));
% Data to fit to:
data=x-x(1)*ones(size(x));
% function to fit to:
fitfcn = @(alpha,beta,t)alpha*(1.0-exp(-beta*t));
%Calculate sum of squares for difference btw data and fitfcn:
function sse= sse(params) 
alpha=params(1);
beta=params(2);
FittedCurve = fitfcn(alpha,beta,t);
ErrorVector = FittedCurve-data;
sse = sum(ErrorVector.^2);
end
%%%%%%%%%%%%%%
startpoint= [xn , 0.02]
estimates=fminsearch(@sse, startpoint)
alpha=estimates(1);
beta=estimates(2);
%%%%%%%%%%%%%%%%%%
end