% 
% Reads variables from strplt.rxx file, and 
% OMITTING THE SECOND DATA POINT, plots sL vs Imag,
% fits to a 3rd order ploynomial
% Also prints out Int. Strength at 1,10,100A, as
% returned from fit.(G1, G10, G100)
%  27-Feb-2006 Achim Weidemann
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function strplotnfitm2(filename)
%filename='strplt.r27';
ff=['S:\MagData\E163\quad\QE-N3\' filename]

[Imag, sImag, SL, sSL, TF, sTF, THsp, sTHsp]...
 =textread(ff, '%f %f %f %f %f %f %f %f','headerlines',1);
%%%%%%
[nobs, dim1] =size(Imag);
%Imag(1,1)
%make second point the same as first one
Imag(2,1)=Imag(1,1);
SL(2,1)=SL(1,1);    
% remove first point
Imag=Imag(2:nobs);
SL=SL(2:nobs);
p=polyfit(Imag,SL,3);
SLF=polyval(p,Imag);
SLres=SL-SLF;
[nobs, dim1] =size(Imag);
lsq=norm(SLres)/nobs;
pstring = ['3rd o poly: ' num2str(p,5)]; 
qstring = ['sqrt(res^2)/n = ' num2str(lsq,'%11.5g')];
[s, errmsg] = sprintf('%s %s',qstring, pstring);
s
%%%%%%%%%%%%%
figure(1)
subplot(2,1,1)
plot(Imag,SL,'-ob','LineWidth',1);
hold on
plot(Imag,SLF,'+r','LineWidth',1);
title(['Int. Strength vs Current-' filename]);
xlabel('Current / A')
ylabel('Int. Strength / T')
legend1 = legend('Data','Fit', 'Location','SouthEast');
text(0.,2.8,pstring)
text(0.,2.5,qstring)
grid on
subplot(2,1,2)
plot(Imag,SLres,'-+r','LineWidth',1);
title('Residuals');xlabel('Current / A')
legend1 = legend('Residuals', 'Location','NorthEast');
% text(-0.005,60.,qstring) put in above legend
grid on
fig1name=[filename,'Fig1m.jpg'];
%print( '-djpeg', fig1name)
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(2)
subplot(2,1,1)
plot(Imag,SL,'-ob','LineWidth',1)
title(['Int. Strength vs Current (for current<3A)-' filename]);
xlabel('Current / A')
ylabel('Int. Strength / T')
hold on
plot(Imag,SLF,'+r','LineWidth',1);
axis([0.0 3.0 0.0 0.1])
legend1 = legend('Data','Fit','Location', 'NorthWest');
grid on
subplot(2,1,2)
plot(Imag,SLres,'-+r','LineWidth',1);
axis([0.0 3.0 -0.005 0.005])
title('Residuals');xlabel('Current / A')
legend1 = legend('Residuals', 'Location','NorthEast');
grid on
fig2name=[filename,'Fig2.jpg'];
%print( '-djpeg', fig2name)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
G1=polyval(p,1.);
G10=polyval(p,10.);
G100=polyval(p,100);
[s, errmsg] = sprintf(' G1 = %10.8f  G10 = %10.8f G100= %10.8f ',G1,G10,G100);
s
%End