[Goal]
Welcome to Fitting Function Builder.

This wizard will guide you to create or edit a User-defined fitting function.

[Function Type: Single-line Expression]
---------------------------------------------------------
Function Type
---------------------------------------------------------
Select this option for simple fitting functions that have only one dependent variable.

The function body is limited to one expression, and you need to provide only the right hand side of the equation.

This option provides faster performance than Origin C.

-------------------- Example: --------------------

a + b * exp( -x^c / d )

[Function Type: Equations]
---------------------------------------------------------
Function Type
---------------------------------------------------------
Select this option if you need multiple lines or you have more than one dependent variable.

This option does not support control structures such as loops or if-else statements. You can however use the ternary operator.

This option provides faster performance than Origin C.

-------------------- Example1: --------------------

temp = 10;
y1 = A + exp( (x1 - xc) / temp^2 );
y2 = A + temp / (x2 - xc)^2;

-------------------- Example2: --------------------

B = A / (0.5 * (sig1+sig2) * sqrt(2*pi));
sig = ( x<xc ? sig1:sig2 );
y = y0 + B * exp(-0.5 * ((x-xc)/sig)^2);

[Function Type: Fitting with Integral]
---------------------------------------------------------
Function Type
---------------------------------------------------------
Select this option to define a fitting function that includes integration.

Not implemented yet...

[Function Type: Fitting with Convolution]
---------------------------------------------------------
Function Type
---------------------------------------------------------
Select this option to define a fitting function that includes convolution.

Not implemented yet...

[Function Type: Origin C]
---------------------------------------------------------
Function Type
---------------------------------------------------------
This option is slower than Equations.

Select this option if your fitting function requires loops or control structures that cannot be replaced by the ternary operator.

Note that you need to follow strict C syntax: parameters are case sensitive, and division by an integer is different than division by a double.

-------------------- Example: --------------------

if ( x < -PI )
	y = y0 + exp( -x-a*PI );
else if ( x > PI )
	y = y0 + exp( x-a*PI );
else
	y = cos(x);

[Function Type: LabTalk Script]
---------------------------------------------------------
Function Type
---------------------------------------------------------
This option is much slower than Equations or Origin C.

Select this option if you need control structures or you are not familiar with C language.

-------------------- Example: --------------------

range rr = %C;
int b = %(rr[P1]$);
y = b + a*x;

[Function Type: External DLL]
---------------------------------------------------------
Function Type
---------------------------------------------------------
Select this option to define a fitting function using an external DLL.

Using an external DLL can improve fitting time significantly.

Please use a C/C++ compiler to create the DLL file. Place the DLL file in the same folder as the FDF file. The FDF file is saved by default to the <UFF>\fitfunc\ subfolder.

For more information on creating the DLL, please drag-and-drop the FittingWithExternalDLL.opx file onto Origin. This file is located in the <Origin EXE>\Samples\Curve Fitting\ folder. The file will unpack to <UFF>\Fitting with External DLL\ subfolder. Follow instructions in the "Fitting with External DLL.PDF" file.

[Variables and Parameters]
Enter names of variables, parameters, derived parameters and constants in the edit boxes. Separate multiple names using comma.

-------------------- Example: --------------------

x1, x2

---------------------------------------------------------
Derived Parameters
---------------------------------------------------------
Derived parameters are additional parameters to be computed from the function parameter values after the fitting process ends.

---------------------------------------------------------
Constants
---------------------------------------------------------
Constants are fixed values that can be used either in the function expression or in parameter initialization code.

---------------------------------------------------------
Peak Function
---------------------------------------------------------
Check the Peak Function box if you want to use your function for fitting peaks in the Peak Analyzer or for replica fitting in the NLFit tool.

[Peak Function]
---------------------------------------------------------
Peak Function Specification
---------------------------------------------------------
Parameters for peak function have special Peak Attributes that need to be set on the next page.

The first parameter in the parameter list must be named as "y0" and should be assigned the peak attribute "offset".

Besides "y0", you need to define at least three more parameters, and set peak attribute as "center", "width" and "amplitude/area".

-------------------- Example: --------------------

y = y0+(2*A/PI)*(w/(4*(x-xc)^2 + w^2))

[Set Peak Attribute]
---------------------------------------------------------
Peak Attribute Column
---------------------------------------------------------
For peak functions, the peak attribute for the first "y0" parameter should be set as "offset" . Besides y0, you need to assign peak attributes of "center", "width" and "amplitude/area" to at least three other parameters.

[Quick Check]
---------------------------------------------------------
Quick Check
---------------------------------------------------------
The Quick Check group can help to check the validity of function.

Enter desired values for parameters in the Initial Value column in the table, and enter value for independent variable(s) in the edit box. Then press the button to view output value of dependent variable(s).

If there are errors in your function body or if you specified inappropriate input values for parameters or variables, a missing value along with an appropriate error message will be displayed in the output box.

[Parameter Tab]
---------------------------------------------------------
Parameters Tab
---------------------------------------------------------
Values entered in "Initial Value" column will be used as initial parameter values when fitting. 

Check the "Fixed" check box if a particular parameter should not be varied during fitting. You can control this later in the Nonlinear Fitter dialog as well.

Optionally enter "Unit" for parameters.

[Constant Tab]
---------------------------------------------------------
Constants Tab
---------------------------------------------------------
Constants are fixed values that can be used either in the function expression or in parameter initialization code. The "Value" column should not be empty if you have defined a constant.

[Integrand Tab]
---------------------------------------------------------
Integrand Tab
---------------------------------------------------------
Assign proper values to the integrand you defined in previous page. For example, you can pass fitting parameters to integrand arguments; Or set independent valueables as upper limit, etc.

To assign infinite to integral limits, use inf for infinite.

Once all set for the integrand, click the "Insert" button to insert the proper function, which will actually do integration, into your fitting function body.

[Single Expression]
---------------------------------------------------------
Function Body
---------------------------------------------------------
The function body is limited to one line, and you need to provide only the right hand side of the equation.

-------------------- Example: --------------------

a + b * exp( -x^c / d )

[Multiple Equations]
---------------------------------------------------------
Function Body
---------------------------------------------------------
Enter the function definition, including independent and dependent variables, in the "Function Body" edit box. 

The expression to calculate dependent variables should be an explicit equation.

Use multiple lines if necessary. You can define temporary variables. No control structures such loops or if-else statements are allowed. You can however use the ternary operator.  

-------------------- Example1: --------------------

temp = 10;
y1 = A + exp( (x1 - xc) / temp^2 );
y2 = A + temp / (x2 - xc)^2;

-------------------- Example2: --------------------

B = A / (0.5 * (sig1+sig2) * sqrt(2*pi));
sig = ( x<xc ? sig1:sig2 );
y = y0 + B * exp(-0.5 * ((x-xc)/sig)^2);


[LabTalk Script Function]
---------------------------------------------------------
Function Body
---------------------------------------------------------
Enter the function definition, including independent and dependent variables, in the "Function Body" edit box.

The expression to calculate dependent variables should be an explicit equation.

Use multiple lines if necessary. Temporary variables and control structure can be used in the function body.

-------------------- Example: --------------------

// This example reads value 
// from worksheet header

range rr = %C;
int b = %(rr[P1]$);
y = b + a*x;

---------------------------------------------------------
Use Derivatives
---------------------------------------------------------
Check this check box to specify expressions for derivatives for all parameters.

Specifying derivative expression can increase the speed of fitting. Use "d_VarName" to represent the derivative variables.

-------------------- Example: --------------------

y = a + b*x + exp(c*x);
dy_a = 1;
dy_b = x;
dy_c = x*exp(c*x);

[Origin C Fitting Function]
---------------------------------------------------------
Function Body
---------------------------------------------------------
Enter the function definition, including independent and dependent variables, in the "Function Body" edit box.

The expression to calculate dependent variables should be an explicit equation.

Note that you need to follow strict C syntax: parameters are case sensitive, and division by an integer is different than division by a double.

Use multiple lines if necessary. Temporary variables and control structure can be used in the function body, and NAG functions can be called.

---------------------------------------------------------
Compile Function
---------------------------------------------------------
Press the button on top right of the edit box to open and edit function body in Code Builder. Use the "Compile" button in Code Builder to verify that function compiles without errors.

-------------------- Example: --------------------

if ( x < -PI )
	y = y0 + exp( -x-a*PI );
else if ( x > PI )
	y = y0 + exp( x-a*PI );
else
	y = cos(x);

---------------------------------------------------------
Use Derivatives
---------------------------------------------------------
Check this check box to specify expressions for derivatives for all parameters.

Specifying derivative expression can increase the speed of fitting. Use "d_VarName" to represent the derivative variables.

-------------------- Example: --------------------

y = a + b*x + exp(c*x);
dy_a = 1;
dy_b = x;
dy_c = x*exp(c*x);

[External DLL]
---------------------------------------------------------
DLL File and Annotation
---------------------------------------------------------
The actual fitting function needs to be defined in a DLL file.

Please save the DLL file to the same folder as the FDF file. The default location for saving FDF files is the <UFF>\fitfunc\ subfolder.

Specify the DLL and function name in the edit boxes. Use the "Annotation" edit box to specify the formula to be displayed in the fitter dialog. Note that this formula is for display purposes only.

---------------------------------------------------------
Reference
---------------------------------------------------------
For details, please refer to the Origin help file topic: 

Regression and Curve Fitting-> Nonlinear Curve Fitting-> Using External DLLs

[Parameter Initialization Code]
Optionally define code to be run to compute initial values for fitting function parameters.

---------------------------------------------------------
Initialization Code
---------------------------------------------------------
If you provide initialization code in this edit box, and the "Always run..." check box is checked, values generated by your code will replace the static initial values you entered in the "Variables and Parameters" page. 

The parameter initialization values can be computed by examining the raw data. Many global functions are available in Origin C to analyze the raw data. Please refer to the "Regression and Curve Fitting" chapter of the Origin Help File for more information.

---------------------------------------------------------
Always run Initialization Code ...
---------------------------------------------------------
Uncheck this check box if you do not want the initialization code to run by default. The code can be run later, when desired, in the Nonlinear fitter dialog.

---------------------------------------------------------
Use Origin C Syntax
---------------------------------------------------------
Check this check box to write code using Origin C. There are many global functions available to examine data to generate suitable initial values depending on the data being fit.

If this check box is unchecked, the code will be assumed to be LabTalk script.

[Bounds and Constraints]
Optionally specify lower and/or upper bounds for each parameter. 

Note that you can also specify these values in the Nonlinear fitter dialog during the fitting session.

---------------------------------------------------------
< or <=
---------------------------------------------------------
Double click the cells in the operator column (< or <=) to choose a proper operator. Then enter the lower or upper bound values in the respective edit boxes.

---------------------------------------------------------
Use General Linear Constraints
---------------------------------------------------------
Check this check box if you wish to specify linear constraints on parameters. Separate multiple lines by semicolon.

-------------------- Example: --------------------

a > b;
a + 2 * b >= c * 2 ? d;
a < b < c;
a / 3 < 9;

[Derived Parameters]
Optionally define derived parameters, which are additional parameters to be computed from the function parameter values.

These values will be computed after the fitting process ends, and will be available in the fit report worksheet. 

Note that each derived parameter should be specified in a separate row, and one derived parameter cannot be used to compute another derived parameter.

-------------------- Example: --------------------

Assuming that a function parameter named w exists; you can define two additional derived parameters as follows:

sigma = w/2;
FWHM = sqrt(2*ln(2))*w;

[Before or After Fitting Script]
Optionally enter LabTalk script to be executed before and/or after the fitting process.


[Integrand]
---------------------------------------------------------
Integrand
---------------------------------------------------------
Define the integrand, which will be used in fitting function body.

The integrand should least has:
* The name of the function.
* An integration variable.
* A return value in function body.

Arguments are optional, you can then pass fitting variables and parameters to the integrand. 

For example, define the Integrand like:

Integrand Name:         MyInteg
Integration Variable:    t
Arguments:                 arg
Integrand Function:    return arg*t^2;

Please look into Origin Help document for more information about fitting a integration function.

[Include Integration]
---------------------------------------------------------
Include Integration During Fitting
---------------------------------------------------------
Define the integrand in the next page, and include this integrand in your fitting function.
