Handling/CompAlgSystems4GolemUpToShotNo31150/gnuplot/scripts/ComplexFigVAcharWithFits/jhivfitting.m

function o=jhivfitting(V,I,Vc)
plots=0;
IsTeVf=[0.1 10 0];
if length(V)<3 o=jhjamie([],[]); return; end
falling=diff(I)./diff(V)<0; falling(end+1)=falling(end); % taking into account only part where dI/dV<0
minI=min(I);
nn=round(length(Vc)/30); if nn==0 nn=1; end
for i=1:nn:length(Vc)
 ii=V<Vc(i) & V<IsTeVf(3)+3*IsTeVf(2) & [falling  | V<IsTeVf(3)];% & I>minI/2  
 o=jhjamie(V(ii),I(ii)); 
 if all(~isnan(o.IsTeVf)) IsTeVf=IsTeVf/2+o.IsTeVf/2; end
 O.R2adj(i)=o.R2adj;
 O.Terr(i)=o.err(2)./o.IsTeVf(2);
 O.T(i)=o.IsTeVf(2);
end
if ~exist('O') o=jhjamie([],[]); return; end
[tmp,i]=max(O.T./[1-O.R2adj]./O.Terr); % making sure the output is only the most credible fit
 ii=V<Vc(i) & V<IsTeVf(3)+1.5*IsTeVf(2) &  [falling  | V<IsTeVf(3)];% & I>minI/2;  
 if plots hold on; plot(V,I); end
 o=jhjamie(V(ii),I(ii),plots); 
%hold on
%plot(Vc,O.R2adj,Vc,1./O.Terr,Vc,O.T)
if plots hold off; end