custom linear fitting
about custom linear models
in the curve fitter 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, choose linear fitting. if you don’t know if your equation can be expressed as a set of linear functions, then choose custom equation instead. see .
selecting a linear fitting custom fit interactively
in the curve fitter app, select some curve data. on the curve fitter tab, in the data section, click select data. in the select fitting data dialog box, select x data and y data values.
curve fitter creates a default polynomial fit.
change the model type. on the curve fitter tab, in the fit type section, click the arrow to open the gallery. in the gallery, click linear fitting in the custom group.
in the fit options pane, an example equation appears when you click linear fitting.
you can change
x
andy
to any valid variable names.the lower box displays the example equation. change the coefficients and terms entries to change the example terms and define your own equation.
for an example, see fit custom linear legendre polynomials in curve fitter app.
selecting linear fitting at the command line
to use a linear fitting algorithm, specify a cell array or string 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 array.
to specify a linear model of the following form:
coeff1 * term1 coeff2 * term2 coeff3 * term3 ...
term1
,
term2
, etc., use a cell array or string array where each
term, without coefficients, is specified as a separate element. for
example:linearmodelterms = {'term1', 'term2', 'term3', ... }
identify the linear model terms you need to input to
fittype
. for example, the modelis linear ina*log(x) b*x c
a
,b
, andc
. it has three termslog(x)
,x
, and1
(becausec=c*1
). to specify this model you use these terms:linearmodelterms = {'log(x)','x','1'}
.use the cell array or string 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 thefit
function.load census f = fit(cdate,pop,linearfittype)
alternatively, you can specify the cell array or string array of linear model terms as an input to thef = 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.
fit custom linear legendre polynomials
fit custom linear legendre polynomials in curve fitter app
this example shows how to fit data using several custom linear equations. the data is generated, and is based on the nuclear reaction 12c(e,e'α)8be. the equations use sums of legendre polynomial terms.
consider an experiment in which 124 mev electrons are scattered from 12c nuclei. in the subsequent reaction, alpha particles are emitted and produce the residual nuclei 8be. by analyzing the number of alpha particles emitted as a function of angle, you can deduce certain information regarding the nuclear dynamics of 12c. the reaction kinematics are shown next.
the data is collected by placing solid state detectors at values of θα ranging from 10o to 240o in 10o increments.
it is sometimes useful to describe a variable expressed as a function of angle in terms of legendre polynomials
where pn(x) is a legendre polynomial of degree n, x is cos(θα), and an are the coefficients of the fit. for information about generating legendre polynomials, see the 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:
legendre polynomials up to fourth degree
n | pn(x) |
---|---|
0 | 1 |
1 | x |
2 | (1/2)(3x2– 1) |
3 | (1/2)(5x3 – 3x) |
4 | (1/8)(35x4 – 30x2 3) |
this example shows how to fit the data using a fourth-degree legendre polynomial with only even terms:
load the 12c alpha-emission data.
load carbon12alpha
the workspace now contains two new variables:
angle
is a vector of angles (in radians) ranging from 10o to 240o in 10o increments.counts
is a vector of raw alpha particle counts that correspond to the emission angles inangle
.
open the curve fitter app.
curvefitter
in the curve fitter app, on the curve fitter tab, in the data section, click select data. in the select fitting data dialog box, select
angle
andcounts
as the x data and y data values, respectively, to create a default polynomial fit to the two variables.change the fit type to a default custom linear fit. on the curve fitter tab, in the fit type section, click the arrow to open the gallery. in the gallery, click linear fitting in the custom group.
use linear fitting instead of the 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 y1(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(θα).in the fit options pane, change the equation terms.
change the coefficients names to
a2
,a4
, anda0
.change the terms value for
a2
to the following:(1/2)*(3*cos(x)^2-1)
the curve fitter app updates the fit as you edit the terms.
change the terms value for
a4
to the following:(1/8)*(35*cos(x)^4-30*cos(x)^2 3)
the fit appears in the curve fitter app.
in the table of fits pane, double-click the fit name value and change it to
leg4even
.display the residuals. on the curve fitter tab, in the visualization section, click 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 a0(x) and a4(x) are known fairly accurately, but that the a2(x) coefficient has a relatively large uncertainty.
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:
first, make a copy of the legendre polynomial fit to modify. on the curve fitter tab, in the file section, click duplicate. the duplicated fit appears in a new tab in the fits pane.
in the table of fits pane, rename the new fit to
leg4evenodd
.in the fit options pane, change the equation terms.
edit the terms as follows to fit the model given by y2(x):
click the last button twice, to add the odd legendre terms.
change the new coefficient names to
a1
anda3
.change the terms value for
a1
to the following:cos(x)
change the terms value for
a3
to the following:(1/2)*(5*cos(x)^3-3*cos(x))
observe the new fit plotted in the curve fitter app, and examine the numerical results in the results pane.
note that the odd legendre coefficients (
a1
anda3
) 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 theleg4even
fit is the best one.compare the fits side by side. click the document actions arrow located to the far right of the fit figure tabs. select the
tile all
option and specify a 1-by-2 layout.you can display only the plots by dragging and hiding the fit options, results, and table of fits panes.
fit custom linear legendre polynomials at the command line
fit the same model at the command line that you created in the curve fitter app.
to use a linear fitting algorithm, specify a cell array or string array of model terms as an input to the
fittype
function. use the same terms you entered in the curve fitter app for theleg4even
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
andcounts
variables in the workspace.load carbon12alpha
use the
fittype
as an input to thefit
function, and specify theangle
andcounts
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 function.