In the Curve Fitting app, you can use the Custom
Equation
fit to define your own linear or nonlinear
equations. The custom equation fit uses the nonlinear least-squares
fitting procedure.
You can define a custom linear equation in Custom
Equation
, but the nonlinear fitting is less efficient
and usually slower than linear least-squares fitting. If you need
linear least-squares fitting for custom equations, select Linear
Fitting
instead. Linear models are linear combinations
of (perhaps nonlinear) terms. They are defined by equations that are
linear in the parameters.
Tip
If you need linear least-squares fitting for custom equations,
select Linear Fitting
. If you don’t
know if your equation can be expressed as a set of linear functions,
then select Custom Equation
instead. See Selecting a Custom Equation Fit Interactively.
In the Curve Fitting app, select some curve data in
the X data and Y data lists.
You can only see Linear Fitting
in the
model type list after you select some curve data, because Linear
Fitting
is for curves, not surfaces.
Curve Fitting app creates a default polynomial fit.
Change the model type from Polynomial
to Linear
Fitting
in the model type list.
An example equation appears when you select Linear
Fitting
from the list.
You can change x
and y
to
any valid variable names.
The lower box displays the example equation. Click Edit to change the example terms in the Edit Custom Linear Terms dialog box and define your own equation.
For an example, see Fit Custom Linear Legendre Polynomials in Curve Fitting App.
To use a linear fitting algorithm, specify a cell array of model
terms as an input to the fit
or fittype
functions.
Do not include coefficients in the expressions for the terms. If there
is a constant term, use '1' as the corresponding expression in the
cell array.
To specify a linear model of the following form:
coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...
term1
, term2
,
etc., use a cell array where each term, without coefficients, is specified
in a cell array of strings, as follows:LinearModelTerms = {'term1', 'term2', 'term3', ... }
Identify the linear model terms you need to input
to fittype
. For example, the model
a*log(x) + b*x + c
a
, b
, and c
.
It has three terms log(x)
, x
,
and 1
(because c=c*1
). To specify
this model you use this cell array of terms: LinearModelTerms
= {'log(x)','x','1'}
.Use the cell array of linear model terms as the input
to the fittype
function:
linearfittype = fittype({'log(x)','x','1'})
linearfittype = Linear model: linearfittype(a,b,c,x) = a*log(x) + b*x + c
Load some data and use the fittype
as
an input to the fit
function.
load census
f = fit(cdate,pop,linearfittype)
f = Linear model: f(x) = a*log(x) + b*x + c Coefficients (with 95% confidence bounds): a = -4.663e+04 (-4.973e+04, -4.352e+04) b = 25.9 (24.26, 27.55) c = 3.029e+05 (2.826e+05, 3.232e+05)
fit
function:f = fit(x,z,{'log(x)','x','1'})
Plot the fit and data.
plot(f,cdate,pop)
For an example, see Fit Custom Linear Legendre Polynomials at the Command Line.
This example shows how to fit data using several custom linear equations. The data is generated, and is based on the nuclear reaction ^{12}C(e,e'α)^{8}Be. The equations use sums of Legendre polynomial terms.
Consider an experiment in which 124 MeV electrons are scattered from ^{12}C nuclei. In the subsequent reaction, alpha particles are emitted and produce the residual nuclei ^{8}Be. By analyzing the number of alpha particles emitted as a function of angle, you can deduce certain information regarding the nuclear dynamics of ^{12}C. The reaction kinematics are shown next.
The data is collected by placing solid state detectors at values of Θ_{α} ranging from 10^{o} to 240^{o} in 10^{o} increments.
It is sometimes useful to describe a variable expressed as a function of angle in terms of Legendre polynomials
$$y(x)={\displaystyle \sum _{n=0}^{\infty}{a}_{n}{P}_{n}(x)}$$
where P_{n}(x)
is a Legendre polynomial of degree n, x is
cos(Θ_{α}), and a_{n} are
the coefficients of the fit. For information about generating Legendre
polynomials, see the legendre
function.
For the alpha-emission data, you can directly associate the coefficients with the nuclear dynamics by invoking a theoretical model. Additionally, the theoretical model introduces constraints for the infinite sum shown above. In particular, by considering the angular momentum of the reaction, a fourth-degree Legendre polynomial using only even terms should describe the data effectively.
You can generate Legendre polynomials with Rodrigues' formula:
$${P}_{n}(x)=\frac{1}{{2}^{n}n!}{\left(\frac{d}{dx}\right)}^{n}{({x}^{2}-1)}^{n}$$
Legendre Polynomials Up to Fourth Degree
n | P_{n}(x) |
---|---|
0 | 1 |
1 | x |
2 | (1/2)(3x^{2}– 1) |
3 | (1/2)(5x^{3} – 3x) |
4 | (1/8)(35x^{4} – 30x^{2} + 3) |
This example shows how to fit the data using a fourth-degree Legendre polynomial with only even terms:
$${y}_{1}(x)={a}_{0}+{a}_{2}\left(\frac{1}{2}\right)(3{x}^{2}-1)+{a}_{4}\left(\frac{1}{8}\right)(35{x}^{4}-30{x}^{2}+3)$$
Load the ^{12}C alpha-emission data by entering
load carbon12alpha
The workspace now contains two new variables:
angle
is a vector of angles (in
radians) ranging from 10^{o} to 240^{o} in
10^{o} increments.
counts
is a vector of raw alpha
particle counts that correspond to the emission angles in angle
.
Open the Curve Fitting app by entering:
cftool
In the Curve Fitting app, select angle
and counts
for X
data and Y data to create a default
polynomial fit to the two variables.
Change the fit type from Polynomial
to Linear
Fitting
to create a default custom linear fit.
You use Linear Fitting
instead of Custom
Equation
fit type, because the Legendre polynomials
depend only on the predictor variable and constants. The equation
you will specify for the model is y_{1}(x)
(that is, the equation given at the beginning of this procedure).
Because angle
is given in radians, the argument
of the Legendre terms is given by cos(Θ_{α}).
Click Edit to change the equation terms in the Edit Custom Linear Terms dialog box.
Change the Coefficients names
to a2
, a4
, and a0
.
Change the Terms for a2
to
(1/2)*(3*cos(x)^2-1)
The Curve Fitting app updates the fit as you edit the terms.
Change the Terms for a4
to
(1/8)*(35*cos(x)^4-30*cos(x)^2+3)
The fit appears in the Curve Fitting app.
Rename the Fit name to Leg4Even
.
Display the residuals by selecting View > Residuals Plot.
The fit appears to follow the trend of the data well, while the residuals appear to be randomly distributed and do not exhibit any systematic behavior.
Examine the numerical fit results in the Results pane. Look at each coefficient value and its confidence bounds in parentheses. The 95% confidence bounds indicate that the coefficients associated with a_{0}(x) and a_{4}(x) are known fairly accurately, but that the a_{2}(x) coefficient has a relatively large uncertainty.
Select Fit > Duplicate Leg4Even to make a copy of your previous Legendre polynomial fit to modify.
The duplicated fit appears in a new tab.
To confirm the theoretical argument that the alpha-emission data is best described by a fourth-degree Legendre polynomial with only even terms, next fit the data using both even and odd terms:
$${y}_{2}(x)={y}_{1}(x)+{a}_{1}x+{a}_{3}\left(\frac{1}{2}\right)(5{x}^{3}-3x)$$
Rename the new fit to Leg4EvenOdd
.
Click Edit to change the equation terms. The Edit Custom Linear Terms dialog box opens.
Edit the terms as follows to fit the model given by y_{2}(x):
Click the + button to add a term twice, to add the odd Legendre terms.
Change the new coefficient names to a1
and a3
.
Change the Terms for a1
to
cos(x)
Change the Terms for a3
to
pro(1/2)*(5*cos(x)^3-3*cos(x))
Observe the new fit plotted in the Curve Fitting app, and examine the numerical results in the Results pane.
Note that the odd Legendre coefficients (a1
and a3
)
are likely candidates for removal to simplify the fit, because their
values are small and their confidence bounds contain zero. These results
indicate that the odd Legendre terms do not contribute significantly
to the fit, and the even Legendre terms are essentially unchanged
from the previous fit. This confirms that the initial model choice
in the Leg4Even
fit is the best one.
To compare the fits side by side, select Left/Right tile. You can display only the plots by hiding the fit settings and results panes using the Curve Fitting app View menu.
Fit the same model at the command line that you created in Curve Fitting app.
To use a linear fitting algorithm, specify a cell
array of model terms as an input to the fittype
function.
Use the same Terms you entered in Curve Fitting
app for the Leg4Even
fit, and do not specify any
coefficients.
linearft = fittype({'(1/2)*(3*cos(x)^2-1)', ... '(1/8)*(35*cos(x)^4-30*cos(x)^2+3)','1'})
linearft = Linear model: linearft(a,b,c,x) = a*((1/2)*(3*cos(x)^2-1))... + b*((1/8)*(35*cos(x)^4-30*cos(x)^2+3)) + c
Load the angle
and counts
variables
in the workspace.
load carbon12alpha
Use the fittype
as an input to
the fit
function, and specify the angle
and counts
variables
in the workspace.
f = fit(angle, counts, linearft)
f = Linear model: f(x) = a*((1/2)*(3*cos(x)^2-1))... + b*((1/8)*(35*cos(x)^4-30*cos(x)^2+3)) + c Coefficients (with 95% confidence bounds): a = 23.86 (4.436, 43.29) b = 201.9 (180.2, 223.6) c = 102.9 (93.21, 112.5)
Plot the fit and data.
plot(f, angle, counts)
For more details on linear model terms, see the fittype
function.