[GENERAL INFORMATION]
Function Name=TwoSiteBind
Brief Description=Two site binding curve. 
Function Source=fgroup.Binding2
Function Type=Built-in
Function Form=Expression
Number Of Parameters=4
Number Of Independent Variables=1
Number Of Dependent Variables=1


[FITTING PARAMETERS]
Naming Method=User-Defined
Names=Bmax1, Bmax2, k1, k2
Meanings=first top asymptote, second top asymptote, first median, second median
Lower Bounds=--(I,Off),--(I,Off),0(X,Off),0(X,Off)
Upper Bounds=--(I,Off),--(I,Off),--(I,Off),--(I,Off)
Number Of Significant Digits=


[FORMULA]

y = Bmax1 * x / (k1 + x) + Bmax2 * x / (k2 + x)


[CONSTRAINTS]


[Parameters Initialization]
sort(x_y_curve);
smooth(x_y_curve);
// compute the base-line
double x0=min(x_data);
double y0=yatxmin(x_y_curve);	
double k=(yatxmax(x_y_curve) - y0)/(max(x_data) - x0);
	
int ilower=x_y_curve.GetLowerIndex();
int iupper=x_y_curve.GetUpperIndex();
double diff_max1,diff_max2,diff_xi;
diff_max1=diff_max2=fabs(y_data[ilower+1]-y_data[ilower]);
int idiff_max1,idiff_max2;
idiff_max1=idiff_max2=ilower;	
for(int i=ilower;i<iupper-1;i++)
{ // look up the two point with the max and 2nd-max y-difference to
  // the max and 2nd-max y-difference to next point
  double yi_offset,yi1_offset;
  yi_offset=y_data[i]-y0-k*(x_data[i]-x0);
  yi1_offset=y_data[i+1]-y0-k*(x_data[i+1]-x0);
  if( yi_offset*yi1_offset < 0 )
  { // the two points are in different side of the base-line
	diff_xi=fabs(y_data[i+1]-y_data[i]);
	if(diff_xi>diff_max1)
	{
		// pass max1 down to max2
		idiff_max2=idiff_max1;
		diff_max2=diff_max1;
		// set max1 to the new max
		diff_max1=diff_xi;
		idiff_max1=i;
	}
	else if(diff_xi>diff_max2)
	{
		diff_max2=diff_xi;
		idiff_max2=i;
	}
  }
}
double x1,x2;
x1=x_data[idiff_max1];
x2=x_data[idiff_max2];
k2=(x1<x2) ? -x1 : -x2;
k1=(x1<x2) ? -x2 : -x1;
x_y_curve = x_y_curve*(k1+x_data)*(k2+x_data);
double coeff[3];
fitpoly(x_y_curve,2,coeff);
Bmax1 = (coeff[1] - k1*coeff[2])/(k2 - k1);
Bmax2 = coeff[2] - Bmax1;

[CONSTANTS]


[INITIALIZATIONS]



[AFTER FITTING]



[INDEPENDENT VARIABLES]
x=


[DEPENDENT VARIABLES]
y=


[CONTROLS]
General Linear Constraints=Off
Initialization Scripts=Off
Scripts After Fitting=Off
Number Of Duplicates=N/A
Duplicate Offset=N/A
Duplicate Unit=N/A


