# Perform Implicit Curve Fitting Using Python in Origin

This blog demonstrates how to use Python in Origin to perform implicit curve fitting. Both a system fitting function and a user-defined fitting function will be used as examples. The Python script is provided in the blog and please follow the steps to try it out.

## Origin Built-in Implicit function

In this example, we will fit the dataset Ellipse.dat with Ellipse function above. This is a system fitting function in Origin that can be directly called for fitting.

1. select from Origin menu Connectivity:Open Untitled.py to Open an empty Python file in code builder.

2. Copy paste the script below and click the Run button (F5) to execute.

``````import originpro as op

wks = op.new_sheet()
fn=op.path('e') + 'Samples\Curve Fitting\Ellipse.dat'
wks.from_file(fn, False)

# Build the model and fit
model = op.NLFit('Ellipse')
model.set_range(f'{wks.lt_range(False)}!(1,2,3,4)') # Set data for fitting
model.fit()
model.report(True) # Generate result sheet``````

A report sheet should be added to the workbook, activate it and check the fitted result.

## User-Defined Implicit Function

To Fit the same data to a function of heart curve, follow the steps below to define the function:

1 . Select Tools: Fitting Function Builder from the Origin menu.

2. In the Fitting Function Builder dialog’s Goal page, click Next.

3. In the Name and Type page, select Implicit from Select or create a Category drop-down list, type heart in the Function Name field, and select Implicit in Function Model group. And click Next.

4. In the Variables and Parameters page, type x, y in the Variable field and xc, yc, r in the Parameter field. Click Next.

5. In the Expression Function page, type the following script in the Function Body edit box: `(x-xc)^2 +(y-yc-((x-xc)*(x-xc))^(1/3))^2-r^2`. Click the running man button to check the function. Click Finish button to create the function.

7. Now let’s fit with this function following the same routine in the first example. Select menu Connectivity:Open Untitled.py to go to code builder.

8. Change the fitting function name to heart in the script and execute.

``````import originpro as op

wks = op.new_sheet()
fn=op.path('e') + 'Samples\Curve Fitting\Ellipse.dat'
wks.from_file(fn, False)

# Build the model and fit
model = op.NLFit('Heart')
model.set_range(f'{wks.lt_range(False)}!(1,2,3,4)') # Set data for fitting
model.fit()
model.report(True) # Generate result sheet``````

9. Check the fitted result in the report sheet.

Note: To close the loop of fitted curve, please click the green lock and select Change Parameters…. In NLFit dialog, in the left panel select Fitted Curves, in the right panel change Fitted Curves Plot->Data Type of y->Range Margin(%) to 50 and click Fit button.

#### About Yiming Chen

View all posts by Yiming Chen →

### One Comment on “Perform Implicit Curve Fitting Using Python in Origin”

1. Andrey Kazak says:

Great feature!