# 4.4 NLFit and Peak Analyzer

Python functions can be used for performing nonlinear curve fitting. Peak functions defined with Python can also be used in Peak Analyzer. A fitting function file (FDF file) will need to be created which includes the Python function and script commands to install any Python packages that are needed for your Python function.

The following example outlines how to create an FDF with Python function.

## Fitting Function for Mass Diffusion

This section outlines the procedure to create a fitting function for the Mass Diffusion equation: using a Python function.

1. First we need to create a new FDF file. Open the Fitting Function Builder tool using the Origin menu Tools: Fitting Function Builder...
2. On the first page, select Create a New Function.
3. On the second page, set Function Name to MassDiffusion, then select Python Function(Vector) for function type.
4. On the third page, set independent variables to x, dependent variables to y, and set parameters to y0, D, L
5. On the fourth page, enter
from mpmath import nsum, exp, inf
import numpy as np

def MassDiffuse(x, y0, D, L):
sm = [float((nsum(lambda ii: 1/(2*ii+1)**2*exp(-D*(2*ii+1)**2*np.pi**2*t/(4*L**2)),[0, inf]))) for t in x]
return [y0*(1-8/np.pi**2*t) for t in sm]


in the Python Function(Vector) edit box. Enter

y = MassDiffuse(x, y0, D, L)


in the Function Body edit box.

6. On the fifth page, enter
if(Python.chk("mpmath numpy") > 1)
return 1;
return 0;


in the Python Package Check(Labtalk Script) edit box. This function requires the mpmath and numpy packages. These need to be installed prior to using the function. To install the packages, you can use the Python Packages tool accessible from the Connectivity menu in Origin. When you share the function with another colleague, the packages can be automatically installed when that user adds the function to their Origin.

7. Click Next button then click Finish button.
8. You can test the fitting function with the following data for X and Y:
0.00	-0.078
0.20	 0.604
0.40	 0.842
0.60	 1.101
0.80	 1.029
1.00	 1.083
1.20	 0.828
1.40	 0.884
1.60	 0.991
1.80	 1.005
2.00	 0.915


Then use the initial parameter values:

y0 = 1, D = 0.5, L = 0.5


and perform the fitting. The final parameter results will be:

Fitted values: y0 = 0.97846, D = 0.37563, L = 0.43781