Fit Surface with User Defined Function

Video Image.png Video Text Image.png Website blog icon circle.png Blog Image 33x33px.png


Origin provides a NonLinear Surface Fit tool to fit surface. Besides the built-in surface fitting functions, you can also create your own fitting functions in Fitting Function Builder.

In this tutorial, we will illustrate how to create a user-defined surface fitting function in Fitting Function Builder, and carry out nonlinear surface fit with it using NLFit tool.

Minimum Origin Version Required: Origin 2016 SR0

What You Will Learn

This tutorial will show you how to:

  • Create a user-defined surface fitting function using the Fitting Function Builder.
  • Carry out nonlinear surface fit with user-defined fitting function.


For this tutorial, we will define and use the following fitting function:


in which

z is the dependent variable

x and y are the independent variables

and z_0, A_1, A_{3}, B_1, B_3, C, C_{2}, C_3 are all fitting parameters.

Create a Surface Fitting Function

In this section, we will show how to create a user defined surface fitting function in the Fitting Function Builder. But there is an alternative tool Fitting Function Organizer which also can be used to create user defined surface fitting functions(open it by selecting Tools:Fitting Function Organizer or pressing F9).

  1. Launch Origin and choose Tools:Fitting Function Builder (or press F8) to open the Fitting Function Builder.
  2. In the Goal page, select Create a New Function and click Next.
  3. In the Name and Type page, select Surface Fitting from Select or create a Category drop-down list since Nonlinear Surface Fit tool lanches functions in the Surface Fitting category only.
  4. Enter a name "MyPolynomialSurface" in Function Name and select Expression for Function Type. Click Next.
    Tutorial UserDefSurfaceFitFunc 001.png
  5. In the Variables and Parameters page, enter the variable and parameter names as the image below and then click Next:
    Tutorial UserDefSurfaceFitFunc 002.png
    In the Parameters box, use comma (",") as delimiter.
  6. In the Expression Function page, enter the equation below in Function Body:
  7. Give estimated initial values to the parameters according to this particular data and function.
    Tutorial UserDefSurfaceFitFunc 003.png
    1. You can also give different initial values each time before you actually carry out the fitting.
    2. You Can click the Evaluate button Button Evaluate.png to quick check whether the function is valid(if it is valid, an actual value will be returned for z).
  8. Click Finish to create this user defined fitting function. The .FDF file for it will be stored in the User Files Folder.

    You can always modify the user defined fitting function later, either in the Fitting Function Builder(choose Edit a User-defined Function in Goal page), or in the Fitting Function Organizer.

Carry Out Surface Fitting

  1. With a new workbook active, choose Data: Connect to File: Text/CSV and browse to <Origin Program>\Samples\Curve Fitting and import the file PolynomialSurface.dat (accept defaults in the CSV Import Options dialog).
  2. Highlight column C(Y) and click the Set as Z button on the Mini Toolbar.
  3. Tutorial UserDefSurfaceFitFunc 000.png
  4. Keep worksheet C(Z) activated, then select Analysis:Fitting:Nonlinear Surface Fit to open the NLFit dialog.
  5. In the Function Selection page, you can see that Category is read only and set as Surface. Set Function as MyPolynomialSurface(User).
  6. Click the Fit button to carry out the fit.
  7. A report sheet will be generated, in which the fitted value of all parameters are reported in the parameters table. A FitSurfaceCurve worksheet is added to the source workbook as well, in which the data of fitted surface is stored.
    Tutorial UserDefSurfaceFitFunc 004.png
  8. Activate the source worksheet and highlight col(C). Click the Button 3D Scatter.png button to generate a 3D scatter plot. To add the fitted surface to the source data, double click on the Layer 1 icon. In the Layer Contents dialog that opens, set the drop-down list in the upper-left corner to Worksheets in Folder. Select FitZ of FitSurfaceCurve1 worksheet in the panel below the drop-down, then click the menu button beside "A" in the middle of the dialog and change the plot type to 3D - Surface. Click on the rightward pointing arrow just below it to add the dataset to the right panel.
    Tutorial UserSurfaceDefFitFunc 005.png
    Click OK to add the surface to the source data graph.
    Tutorial UserSurfaceDefFitFunc 006.png

A Polynomial Surface Fit App is available for Origin 2016 and higher. If you want to fit a polynomial surface, it is recommended to try this App first.