Main Content

Data Selection

Curve Fitting Toolbox™ offers two ways of fitting curves and surfaces to data. You can create fits at the command line using the fit function, or create fits interactively using the Curve Fitter app. Both ways require you to select data before you fit the curve or surface. Curve fitting requires you to select Y data and, optionally, X data. Surface fitting requires you to select X, Y, and Z data.

Import Data to Workspace

Before you select data, you must import it into a MATLAB® workspace variable. You can import sample data or data from a file by using the functions described in Standard File Formats.

Select Data to Fit at Command Line

To select data at the command line, pass the data to the fit function during the fitting process.

  • To select data for curve fitting, save the X and Y data as column vectors with the same number of rows. Then, pass the column vectors to the fit function as the x and y input arguments.

  • To select data for surface fitting, do one of the following:

    • Save the X, Y, and Z data as column vectors of the same size. Then, pass the vectors to the fit function as the x, y, and z input arguments.

    • Save the X and Y data as an array with two columns. Save the Z data as a column vector with the same number of rows as the array. Pass the array and vector to the fit function as the x and y input arguments, respectively.

For an example of fitting a curve to data at the command line, see Fit a Quadratic Curve. For an example of fitting a surface to data at the command line, see Fit a Polynomial Surface.

Select Data to Fit in Curve Fitter App

To select data interactively, click the Apps tab, display the apps gallery, and then select Curve Fitter in the Math, Statistics and Optimization section. In the Data section of the Curve Fitter tab, click Select Data.

Select data to fit

The Curve Fitter app uses a snapshot of the data you select. Subsequent workspace changes to the data have no effect on the fit. To update the fitted data from the workspace, first change the variable selection, and then reselect the variable with the drop-down controls.

Select Data for Curve Fitting

To select data for curve fitting, import the Y data in a vector, table variable, or array. If you have X data, import it in a vector, table variable, or array. X and Y must have the same number of elements. Then, do one of the following:

  • In the Select Fitting Data dialog box, select the variables using the X data and Y data menus. To select data contained in a table variable, select the table name to display a second menu to the right. Then, select the table variable from the second menu.

  • To plot the Y variable against its indices, select only the Y variable from the Y data menu.

This Select Fitting Data dialog box shows a vector variable selected for the X data and a table variable selected for the Y data.

Select Fitting Data dialog box with a vector variable selected for the X data and a table variable selected for the Y data

Similarly, you can select any numeric variable in the workspace to use as Weights, including a numeric table column. If you specify weights, the weights variable must have the same number of elements as the other data variables.

For an example of fitting curves and surfaces in the Curve Fitter app, see Interactive Curve and Surface Fitting.

Select Data for Surface Fitting

To select data for surface fitting, save the X, Y, and Z data each in a vector, table variable, or array. One of the following conditions must be satisfied:

  • The X, Y, and Z variables contain the same number of elements.

    Note

    The Curve Fitter app expects data variables to be the same size. If the sizes are different but the number of elements is the same, the app reshapes the variables to create a fit and displays a warning in the Results pane.

  • The X variable is a vector containing n elements, the Y variable is a vector containing m elements, and the Z variable is an m-by-n matrix. In this case, the Y data and X data are treated as headers for table rows and columns, respectively. See Tabular Data for more information.

    Note

    If the Z variable is an n-by-m matrix, the app creates a fit by transposing Z and displays a warning about the data transformation in the Results pane.

In the Select Fitting Data dialog box, select the variables using the X data, Y data, and Z data menus. If a variable is saved in a table, select the table name to display a second menu to the right. Then, select the table variable from the second menu.

This Select Fitting Data dialog box shows vector variables selected for the X and Y data and a table variable selected for the Z data.

Select Fitting Data dialog box with vector variables selected for the X and Y data and a table variable selected for the Z data

Similarly, you can select any numeric variable in the workspace to use as Weights, including a numeric table column. If you specify weights, the weights variable must have the same number of elements as the Z variable.

For an example of fitting curves and surfaces using the Curve Fitter app, see Interactive Curve and Surface Fitting.

Tabular Data

The data variables can form tabular data, where the Y data and X data represent the row and column headers, respectively, of a table (sometimes called breakpoints) and the Z data contains the table values.

Sizes are compatible when all of these conditions apply:

  • The X data is saved in a vector of length n.

  • The Y data is saved in a vector of length m.

  • The Z data is saved in a matrix of size [m,n].

The following table shows an example of data in tabular form with n = 4 and m = 3.

 x(1)x(2)x(3)x(4)
y(1)z(1,1)z(1,2)z(1,3)z(1,4)
y(2)z(2,1)z(2,2)z(2,3)z(2,4)
y(3)z(3,1)z(3,2)z(3,3)z(3,4)

For surface fitting at the command line with the fit function, use the prepareSurfaceData function when your data is in tabular form.

See Also

| |

Related Topics