# 4.2.2.7 Fit Surface with User Defined Function

## Summary

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.

## Steps

The data we are going to fit is the file PolynomialSurface.dat under the <Origin EXE Folder>\Samples\Curve Fitting\ path.

The fitting function to be created and used is shown below:

$z=z_0+A_1x+A_3x^3+B_1y+B_3y^3+Cxy+C_2x^2y+C_3xy^2$

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.
5. In the Variables and Parameters page, enter the variable and parameter names as the image below and then click Next:

In the Parameters box, use comma (",") as delimiter.
6. In the Expression Function page, enter the equation below in Function Body:
z0+A1*x+A3*x^3+B1*y+B3*y^3+C*x*y+C2*x^2*y+C3*x*y^2
7. Give estimated initial values to the parameters according to this particular data and function.

 You can also give different initial values each time before you actually carry out the fitting. You Can click the Evaluate button 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. Create a new workbook. Click the button to import the PolynomialSurface.dat file under <Origin EXE Folder>\Samples\Curve Fitting\ path.
2. Right click column C and select Set As: Z.
3. Keep the worksheet activated, select Analysis:Fitting:Nonlinear Surface Fit to open the NLFit dialog.
4. In the Function Selection page, you can see that Category is read only and set as Surface. Set Function as MyPolynomialSurface(User).
5. Click the Fit button to carry out the fit.
6. 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.
7. Activate the source worksheet and highlight col(C). Click the 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 downward-pointing arrow 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.

Click OK to add the surface to the source data graph.
 Note: 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.