## Surface Fitting to Franke Data

The Curve Fitting app provides some example data generated from Franke's bivariate test function. This data is suitable for trying various fit settings in Curve Fitting app.

To load the example data and create, compare, and export surface fits, follow these steps:

To load example data to use in the Curve Fitting app, enter

`load franke`

at the MATLAB^{®}command line. The variables`x`

,`y`

, and`z`

appear in your workspace.The example data is generated from Franke's bivariate test function, with added noise and scaling, to create suitable data for trying various fit settings in Curve Fitting app. For details on the Franke function, see the following paper:

Franke, R., Scattered Data Interpolation: Tests of Some Methods, Mathematics of Computation 38 (1982), pp. 181–200.

To divide the data into fitting and validation data, enter the following syntax:

xv = x(200:293); yv = y(200:293); zv = z(200:293); x = x(1:199); y = y(1:199); z = z(1:199);

To fit a surface using this example data:

Open Curve Fitting app. Enter

`cftool`

, or select**Curve Fitting**on the**Apps**tab.Select the variables

`x`

,`y`

, and`z`

interactively in the Curve Fitting app.Alternatively, you can specify the variables when you enter

`cftool(x,y,z)`

to open Curve Fitting app (if necessary) and create a default fit.

The Curve Fitting app plots the data points as you select variables. When you select

`x`

,`y`

, and`z`

, the tool automatically creates a default surface fit. The default fit is an interpolating surface that passes through the data points.Try a Lowess fit type. Select the

`Lowess`

fit type from the drop-down list in the Curve Fitting app.The Curve Fitting app creates a local smoothing regression fit.

Try altering the fit settings. Enter

`10`

in the**Span**edit box.By reducing the span from the default to 10% of the total number of data points you produce a surface that follows the data more closely. The span defines the neighboring data points the toolbox uses to determine each smoothed value.

Edit the

**Fit name**to`Smoothing regression`

.If you divided your data into fitting and validation data in step 2, select this validation data. Use the validation data to help you check that your surface is a good model, by comparing it against some other data not used for fitting.

Select

**Fit**>**Specify Validation Data**. The Specify Validation Data dialog box opens.Select the validation variables in the drop-down lists for

**X input**,**Y input**, and**Z output**:`xv`

,`yv`

, and`zv`

.

Review your selected validation data in the plots and the validation statistics (SSE and RMSE) in the

**Results**pane and the**Table of Fits**.Create another fit to compare by making a copy of the current surface fit. Either select

**Fit**>**Duplicate "Smoothing regression"**, or right-click the fit in the**Table of Fits**, and select**Duplicate**The tool creates a new fit figure with the same fit settings, data, and validation data. It also adds a new row to the table of fits at the bottom.

Change the fit type to

`Polynomial`

and edit the fit name to`Polynomial`

.Change the

**Degrees**of**x**and**y**to`3`

, to fit a cubic polynomial in both dimensions.Look at the scales on the x and y axes, and read the warning message in the

**Results**pane:Equation is badly conditioned. Remove repeated data points or try centering and scaling.

Select the

**Center and scale**check box to normalize and correct for the large difference in scales in x and y.Normalizing the surface fit removes the warning message from the

**Results**pane.Look at the

**Results**pane. You can view (and copy if desired):The model equation

The values of the estimated coefficients

The goodness-of-fit statistics

The goodness of validation statistics

Linear model Poly33: f(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y... + p02*y^2 + p30*x^3 + p21*x^2*y + p12*x*y^2 + p03*y^3 where x is normalized by mean 1977 and std 866.5 and where y is normalized by mean 0.4932 and std 0.29 Coefficients (with 95% confidence bounds): p00 = 0.4359 (0.3974, 0.4743) p10 = -0.1375 (-0.194, -0.08104) p01 = -0.4274 (-0.4843, -0.3706) p20 = 0.0161 (-0.007035, 0.03923) p11 = 0.07158 (0.05091, 0.09225) p02 = -0.03668 (-0.06005, -0.01332) p30 = 0.02081 (-0.005475, 0.04709) p21 = 0.02432 (0.0012, 0.04745) p12 = -0.03949 (-0.06287, -0.01611) p03 = 0.1185 (0.09164, 0.1453) Goodness of fit: SSE: 4.125 R-square: 0.776 Adjusted R-square: 0.7653 RMSE: 0.1477 Goodness of validation: SSE : 2.26745 RMSE : 0.155312

To export this fit information to the workspace, select

**Fit**>**Save to Workspace**. Executing this command also exports other information such as the numbers of observations and parameters, residuals, and the fitted model.You can treat the fitted model as a function to make predictions or evaluate the surface at values of X and Y. For details see Exporting a Fit to the Workspace.

Display the residuals plot to check the distribution of points relative to the surface. Click the toolbar button or select

**View**>**Residuals Plot**.Right-click the residuals plot to select the

**Go to X-Z view**. The X-Z view is not required, but the view makes it easier to see to remove outliers.To remove outliers, click the toolbar button or select

**Tools**>**Exclude Outliers**.When you move the mouse cursor to the plot, it changes to a cross-hair to show you are in outlier selection mode.

Click a point that you want to exclude in the surface plot or residuals plot. Alternatively, click and drag to define a rectangle and remove all enclosed points.

A removed plot point displays as a red star in the plots.

If you have

**Auto-fit**selected, the Curve Fitting app refits the surface without the point. Otherwise, you can click**Fit**to refit the surface.To return to rotation mode, click the toolbar button again to switch off

**Exclude Outliers**mode.

To compare your fits side-by-side, use the tile tools. Select

**Window**>**Left/Right Tile**, or use the toolbar buttons.Review the information in the

**Table of Fits**. Compare goodness-of-fit statistics for all fits in your session to determine which is best.To save your interactive surface fitting session, select

**File**>**Save Session**. You can save and reload sessions to access multiple fits. The session file contains all the fits and variables in your session and remembers your layout.After interactively creating and comparing fits, you can generate code for all fits and plots in your Curve Fitting app session. Select

**File**>**Generate Code**.The Curve Fitting app generates code from your session and displays the file in the MATLAB Editor. The file includes all fits and plots in your current session.

Save the file with the default name,

`createFits.m`

.You can recreate your fits and plots by calling the file from the command line (with your original data or new data as input arguments). In this case, your original variables still appear in the workspace.

Highlight and evaluate the first line of the file (excluding the word

`function`

). Either right-click and select**Evaluate**, press**F9**, or copy and paste the following to the command line:[fitresult, gof] = createFits(x, y, z, xv, yv, zv)

The function creates a figure window for each fit you had in your session. Observe that the polynomial fit figure shows both the surface and residuals plots that you created interactively in the Curve Fitting app.

If you want you can use the generated code as a starting point to change the surface fits and plots to fit your needs. For a list of methods you can use, see

`sfit`

.

For more information on all fit settings and tools for comparing fits, see: