% MATLAB script to fit G-V relation (related to Supp. Figure 5)

V = -120:10:120; % Voltage range

% TailCurr is the vector holding the instantaneous tail current
% amplitude immediately after the test pulses at the corresponding voltage. 
% Here, one example of tail current from F0R/H5E + KCNE1 is shown
TailCurr = [
0.3523437471 	
0.3265624926 	
0.3312499928 	
0.3289062533 	
0.3289062462 	
0.3320312487 	
0.3656249987 	
0.4054687537 	
0.4984375011 	
0.6828124981 	
1.0593749948 	
1.6437499823 	
2.3578125479 	
3.0867187775 	
3.7484375071 	
4.2945312089 	
4.7132812142 	
5.0757812460 	
5.3226563068 	
5.4921874835 	
5.5679687421 	
5.6499999346 	
5.6109375919 	
5.5226562381 	
5.5117187685
];

TailCurr = transpose(TailCurr); 

% Normalize tail current
TailCurr = TailCurr - min(TailCurr);
[maxI, maxIndex] = max(abs(TailCurr));
Gnorm = TailCurr./TailCurr(maxIndex);

% Define Boltzmann fit, param(1) is the slope Vs, param(2) is V1/2
% Fit Gnorm with this Boltzmann fit
boltzmann = @(param, v) ((1) ./ ( 1 + exp(-param(1) .* (v - param(2)))));
p0 = [1/25, -30]; % Initial parameters for the Botlzmann fit

fit = nlinfit(V, Gnorm, boltzmann, p0);

% Print out the fitted parameters
fit(1) % print Vs fit
fit(2) % print V1/2 fit
