function rtanalysis(filename)
%
ff = fullfile('C:\Documents and Settings\achim\My Documents\MagMeasurements\QM-001\',...
           filename)
% time(min), imag(A), rmag(mohm), T0(C), T1(C), T2(C)
[timem, imag, rmag, t0,t1,t2] =...
      textread(ff, '%f %f %f %f %f %f');
n=length(timem)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% What to fit to: data
data=t1-t1(1)*ones(size(t1));
% What 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,timem);
ErrorVector = FittedCurve-data;
sse = sum(ErrorVector.^2);
end
startpoint= [data(n),0.02]
estimates=fminsearch(@sse, startpoint)
Asysmptoticvalue=estimates(1)+t1(1)
timeconstant=1./estimates(2)
% %%%%%%%%%%%%% Now plot data and fitted curve:
figure(1)
subplot(2,1,1);
plot(timem, data,'*')
xlabel('Time [minutes]');
ylabel('\Delta Temperature[deg C]');
hold on
plot(timem,fitfcn(estimates(1),estimates(2),timem),'r');
title([filename,'Fit to ',func2str(fitfcn)]);
legend('t1-t1(0) data','fit','Location','East');
subplot(2,1,2);
plot(timem,data-fitfcn(estimates(1),estimates(2),timem),'+g')
legend('Residual','Location','SouthEast');
xlabel('Time [minutes]');
ylabel('\Delta Temperature[deg C]');
grid on
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%End of Fit to Temperature %%%%
%%%%%% Now calculate various quantities using the resistance:
% rmag, imag   vmag=rmag*imag
vmag=rmag.*imag/1000.;
figure(2)
%plot(timem,vmag)
%hold on
plot(diff(vmag))
title([filename,' diff-vmag']);
grid on
%figure(3)
%subplot(2,1,1);
%plot(timem,rmag);
%subplot(2,1,2);
%plot(timem,vmag)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
rmin0=min(rmag)
rmin=434.8057 % mOhm, measured in run 39
% with  t0= 23.86, t1=23.97, t2=23.537 
% Ave of last 9 measurements at .27 and .446 A
%%%%%%  rmin should be replaced by room-temp resistance.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% What to fit to: rdata
rdata=rmag-rmin0*ones(size(rmag));
% What function to fit to:
rfitfcn = @(ralpha,rbeta,t)ralpha*(1.0-exp(-rbeta*t));
%Calculate sum of squares for difference btw data and fitfcn:
function rsse= rsse(params) 
ralpha = params(1);
rbeta  = params(2);
rFittedCurve = rfitfcn(ralpha,rbeta,timem);
rErrorVector = rFittedCurve-rdata;
rsse = sum(rErrorVector.^2);
end
rstartpoint= [rmag(n),0.02]
restimates=fminsearch(@rsse, rstartpoint)
AsysmptoticR=restimates(1)+rmin0
timeconstant=1./restimates(2)
%%%%%%%%%%%%%%%%%%%%%%%Now plot data and fitted curve:
figure(3)
subplot(2,1,1);
plot(timem, rdata,'+')
hold on
plot(timem,fitfcn(restimates(1),restimates(2),timem),'r');
title([filename,'Fit to resistance ',func2str(rfitfcn)]);
legend('rmag-rmag(1) R data','fit','Location','East');
xlabel('Time [minutes]');
ylabel('\Delta Resistance[mOhm]');
subplot(2,1,2);
plot(timem,rdata-rfitfcn(restimates(1),restimates(2),timem),'+g')
legend('Residual','Location','SouthEast');
xlabel('Time [minutes]');
ylabel('\Delta Resistance[mOhm]');
grid on
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
thottest = (rmag-rmin*ones(size(rmag)))./(rmin*0.00393) + t2 ;
figure(4)
plot(timem,thottest,'*k');
hold on
plot(timem, ...
(rfitfcn(restimates(1),restimates(2),timem)...
 +(rmin0-rmin)*ones(size(rmag)))./(rmin*0.00393) + t2, '-r');  
plot(timem,t1,'+b')
legend('HottestTemp fr. R','fit', 'CoilSurface T ','Location','East');
xlabel('Time [minutes]');
ylabel('Temperature[deg C]');
grid on
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
rmax=max(rmag)
deltaTx=(rmax -rmin)/(rmin*0.00393)
rlast=rmag(n)
deltaTl=(rlast-rmin)/(rmin*0.00393)
rasym=AsysmptoticR
deltaTa=(rasym-rmin)/(rmin*0.00393)

Tavex = deltaTx+t2(n)
Tavel = deltaTl+t2(n)
Tavea = deltaTa+t2(n)
tlow=max(t1)
Thottestx =  2.*Tavex - tlow 
Thottestl =  2.*Tavel - tlow
Thottesta =  2.*Tavea - tlow
%
%%%%%%%%%%%%End of rtanalysis
end  