## Curve and Surface Fitting Objects and Object Functions

This topic describes how to create curve and surface fit objects and how to use their object functions to manipulate the fits. Use the Curve Fitting Toolbox™ objects and object functions at the MATLAB® command line or to write MATLAB programs for curve and surface fit applications. You can also use Curve Fitting Toolbox in combination with other MATLAB toolboxes to create curve and surface fit workflows.

This topic focuses on how to create and manipulate curve and surface fits programmatically. Alternatively, the Curve Fitter app allows convenient, interactive use of Curve Fitting Toolbox objects and object functions, without programming. You can select a fit on the Curve Fitter tab, in the Fit Type section.

### Curve Fitting Objects

In MATLAB programming, all workspace variables are objects of a particular class. Familiar examples of MATLAB classes are `double`, `char`, and `function_handle`. You can also create custom MATLAB classes, using object-oriented programming.

Object functions are functions that operate exclusively on objects of a particular class. Data types package together objects and object functions so that the object functions operate exclusively on objects of their own type, and not on objects of other types. A clearly defined encapsulation of objects and object functions is the goal of object-oriented programming.

Curve Fitting Toolbox software provides you with new MATLAB data types for performing curve fitting:

• `fittype` — Objects allow you to encapsulate information describing a parametric model for your data. Object functions allow you to access and modify that information.

• `cfit` and `sfit` — Two subtypes of `fittype`, for curves and surfaces. Objects capture information from a particular fit by assigning values to coefficients, confidence intervals, fit statistics, etc. Object functions allow you to post-process the fit through plotting, extrapolation, integration, etc.

Because `cfit` is a subtype of `fittype`, `cfit` inherits all `fittype` object functions. In other words, you can apply `fittype` object functions to both `fittype` and `cfit` objects, but `cfit` object functions are used exclusively with `cfit` objects. The same is true for `sfit` objects.

As an example, the `fittype` object function `islinear`, which determines if a model is linear or nonlinear, logically applies before or after a fit, that is, to both `fittype` and `cfit` objects. On the other hand, the `cfit` object functions `coeffvalues` and `confint`, which, respectively, return fit coefficients and their confidence intervals, makes no sense to apply to a general `fittype` object, which describes a parametric model with undetermined coefficients.

Curve fitting objects have properties that depend on their type, and also on the particulars of the model or the fit that they encapsulate. For example, the following code uses the constructor object functions for the two curve fitting types to create a `fittype` object `f` and a `cfit` object `c`:

`f = fittype('a*x^2+b*exp(n*x)')`
```f = General model: f(a,b,n,x) = a*x^2+b*exp(n*x)```
`c = cfit(f,1,10.3,-1e2)`
```c = General model: c(x) = a*x^2+b*exp(n*x) Coefficients: a = 1 b = 10.3 n = -100```

Note that the display object function for `fittype` objects returns only basic information, piecing together outputs from `formula` and `indepnames`.

`cfit` and `fittype` objects are evaluated at predictor values `x` using `feval`. You can call `feval` indirectly using the following functional syntax:

```y = cfun(x); % cfit objects y = ffun(coef1,coef2,...,x); % fittype objects```

### Curve Fitting Object Functions

Curve fitting object functions allow you to create, access, and modify curve fitting objects. They also allow you, through object functions like `plot` and `integrate`, to perform operations that uniformly process the entirety of information encapsulated in a curve fitting object.

The object functions listed in the following table are available for all `fittype` objects, including `cfit` objects.

Fit Type Object FunctionDescription

`argnames`

Get input argument names

`category`

Get fit category

`coeffnames`

Get coefficient names

`dependnames`

Get dependent variable name

`feval`

Evaluate model at specified predictors

`fittype`

Construct `fittype` object

`formula`

Get formula

`indepnames`

Get independent variable name

`islinear`

Determine if model is linear

`numargs`

Get number of input arguments

`numcoeffs`

Get number of coefficients

`probnames`

Get problem-dependent parameter names

`setoptions`

Set model fit options

`type`

Get name of model

The object functions listed in the following table are available exclusively for `cfit` objects.

Curve Fit Object FunctionDescription

`cfit`

Construct `cfit` object

`coeffvalues`

Get coefficient values

`confint`

Get confidence intervals for fit coefficients

`differentiate`

Differentiate fit

`integrate`

Integrate fit

`plot`

Plot fit

`predint`

Get prediction intervals

`probvalues`

Get problem-dependent parameter values

A complete list of object functions for a curve fitting object can be obtained with the MATLAB `methods` command. For example,

```f = fittype('a*x^2+b*exp(n*x)'); methods(f) ```
```Methods for class fittype: argnames dependnames fittype islinear probnames category feval formula numargs setoptions coeffnames fitoptions indepnames numcoeffs type ```

Some of the object functions listed by `methods` do not appear in the tables above, and do not have reference pages in the Curve Fitting Toolbox documentation. These additional object functions are generally low-level operations used by the Curve Fitter app, and not of general interest when writing curve fitting applications.

There are no global accessor object functions, comparable to `getfield` and `setfield`, available for `fittype` objects. Access is limited to the object functions listed above. This is because many of the properties of `fittype` objects are derived from other properties, for which you do have access. For example,

`f = fittype('a*cos( b*x-c )')`
```f = General model: f(a,b,c,x) = a*cos( b*x-c )```
`formula(f)`
```ans = 'a*cos( b*x-c )'```
```argnames(f) ```
```ans = 4×1 cell array {'a'} {'b'} {'c'} {'x'}```

You construct the `fittype` object `f` by giving the formula, so you do have write access to that basic property of the object. You have read access to that property through the `formula` object function. You also have read access to the argument names of the object, through the `argnames` object function. You don't, however, have direct write access to the argument names, which are derived from the formula. If you want to set the argument names, set the formula.

### Surface Fitting Objects and Object Functions

The surface fit object (`sfit`) stores the results from a surface fitting operation, making it easy to plot and analyze fits at the command line.

Like `cfit` objects, `sfit` objects are a subclass of `fittype` objects, so they inherit all the same object functions of `fittype` listed in Curve Fitting Object Functions.

`sfit` objects also provide object functions exclusively for `sfit` objects. See `sfit`.

One way to quickly assemble code for surface fits and plots into useful programs is to generate a file from a fit in the Curve Fitter app. In this way, you can transform your interactive analysis of a single data set into a reusable function for command-line analysis or for batch processing of multiple data sets. You can use the generated file without modification, or edit and customize the code as needed. See Generate Code and Export Fits to the Workspace.