[General Information]

Function Name=Dhyperbl		; name show up in the select function dialog
Brief Description=Double Rectangular Hyperbola Function
Function Type=Built-in		; source type, built-in, user-defined, external dll
Function Form=Expression	; function form;
Function Source=fgroup.Dhyperbl
Number Of Parameters=5
Number Of Independent Variables=1
Number Of Dependent Variables=1


[Fitting Parameters]

Naming Method=user-defined
Names=P1,P2,P3,P4,P5
Lower bounds=--,--,--,--,--
Upper bounds=--,--,--,--,--
Meanings=unknown,unknown,unknown,unknown,unknown


[Independent Variables]

x = 


[Dependent Variables]

y = 


[Constants]


[Formula]

y = P1*x/(P2 + x) + P3*x/(P4 + x) + P5*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];
P2=(x1<x2) ? -x1 : -x2;
P4=(x1<x2) ? -x2 : -x1;
x_y_curve = x_y_curve*(P2+x_data)*(P4+x_data);
double coeff[4];
fitpoly(x_y_curve,3,coeff);
P5=coeff[3];
P1=(coeff[1]-P2*coeff[2]+P2*P2*P5)/(P4-P2);
P3=coeff[2]-P2*P5-P4*P5-P1;

[Initializations]


[After fitting]


[Controls]
General Linear Constraints=Off
Initialization scripts=Off
Scripts After fitting=Off

