Note: This page has been translated by MathWorks. Please click here

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

This section describes how to use Curve Fitting Toolbox™ functions from the command-line or to write programs for curve and surface fitting applications.

The Curve Fitting app allows convenient, interactive use of Curve Fitting
Toolbox functions,
without programming. You can, however, access Curve Fitting
Toolbox functions
directly, and write programs that combine curve fitting functions
with MATLAB^{®} functions and functions from other toolboxes. This
allows you to create a curve fitting environment that is precisely
suited to your needs.

Models and fits in the Curve Fitting app are managed internally
as curve fitting *objects*. Objects are manipulated
through a variety of functions called *methods*.
You can create curve fitting objects, and apply curve fitting methods,
outside of the Curve Fitting app.

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.

*Methods* are functions that operate exclusively
on objects of a particular class. *Data types* package
together objects and methods so that the methods operate exclusively
on objects of their own type, and not on objects of other types. A
clearly defined encapsulation of objects and methods 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. Methods 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. Methods allow you to post-process the fit through plotting, extrapolation, integration, etc.

Because `cfit`

is a subtype of `fittype`

, `cfit`

inherits
all `fittype`

methods. In other words, you can apply `fittype`

methods
to both `fittype`

and `cfit`

objects,
but `cfit`

methods are used exclusively with `cfit`

objects.
Similarly for `sfit`

objects.

As an example, the `fittype`

method `islinear`

, which determines if a model
is linear or nonlinear, would apply equally well before or after a
fit; that is, to both `fittype`

and `cfit`

objects.
On the other hand, the `cfit`

methods `coeffvalues`

and `confint`

, which, respectively, return
fit coefficients and their confidence intervals, would make no sense
if applied 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 methods 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 method 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 methods allow you to create, access, and modify
curve fitting objects. They also allow you, through methods like `plot`

and `integrate`

,
to perform operations that uniformly process the entirety of information
encapsulated in a curve fitting object.

The methods listed in the following table are available for
all `fittype`

objects, including `cfit`

objects.

Fit Type Method | Description |
---|---|

Get input argument names | |

Get fit category | |

Get coefficient names | |

Get dependent variable name | |

Evaluate model at specified predictors | |

Construct | |

Get formula string | |

Get independent variable name | |

Determine if model is linear | |

Get number of input arguments | |

Get number of coefficients | |

Get problem-dependent parameter names | |

Set model fit options | |

Get name of model |

The methods listed in the following table are available exclusively
for `cfit`

objects.

Curve Fit Method | Description |
---|---|

Construct | |

Get coefficient values | |

Get confidence intervals for fit coefficients | |

Differentiate fit | |

Integrate fit | |

Plot fit | |

Get prediction intervals | |

Get problem-dependent parameter values |

A complete list of methods 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

Note that some of the methods listed by `methods`

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

There are no global accessor methods, comparable to `getfield`

and `setfield`

,
available for `fittype`

objects. Access is limited
to the methods 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 = '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`

method. You also have read access
to the argument names of the object, through the `argnames`

method. 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.

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 methods of `fittype`

listed in Curve Fitting Methods.

`sfit`

objects also provide methods 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 session in the Curve Fitting 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.

Was this topic helpful?