custom linear fitting -凯发k8网页登录

main content

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

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

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

    default linear fitting equation

  3. you can change x and y to any valid variable names.

  4. 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   ...
where no coefficient appears within any of 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', ... }
  1. identify the linear model terms you need to input to fittype. for example, the model

    a*log(x)   b*x   c
    is linear in a, b, and c. it has three terms log(x), x, and 1 (because c=c*1). to specify this model you use these terms: linearmodelterms = {'log(x)','x','1'}.
  2. 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
  3. 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)
    alternatively, you can specify the cell array or string array of linear model terms as an input to the fit function:
    f = fit(x,z,{'log(x)','x','1'})
  4. 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

y(x)=n=0anpn(x)

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:

pn(x)=12nn!(ddx)n(x21)n

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:

y1(x)=a0 a2(12)(3x21) a4(18)(35x430x2 3)

  1. 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 in angle.

  2. open the curve fitter app.

    curvefitter
  3. 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 and counts as the x data and y data values, respectively, to create a default polynomial fit to the two variables.

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

    default fit options for linear fitting

    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(θα).

  5. in the fit options pane, change the equation terms.

    1. change the coefficients names to a2, a4, and a0.

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

    3. change the terms value for a4 to the following:

      (1/8)*(35*cos(x)^4-30*cos(x)^2 3)

      modified coefficients and terms for linear fitting

      the fit appears in the curve fitter app.

  6. in the table of fits pane, double-click the fit name value and change it to leg4even.

    curve fitter display for the leg4even fit

  7. display the residuals. on the curve fitter tab, in the visualization section, click residuals plot.

    fit and residuals plots for the leg4even fit

    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.

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

    results pane displaying coefficient values and goodness-of-fit statistics for the leg4even fit

  9. 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:

    y2(x)=y1(x) a1x a3(12)(5x33x)

    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.

  10. in the table of fits pane, rename the new fit to leg4evenodd.

  11. in the fit options pane, change the equation terms.

    edit the terms as follows to fit the model given by y2(x):

    1. click the last button twice, to add the odd legendre terms.

    2. change the new coefficient names to a1 and a3.

    3. change the terms value for a1 to the following:

      cos(x)
    4. change the terms value for a3 to the following:

      (1/2)*(5*cos(x)^3-3*cos(x))

      modified coefficients and terms for the leg4evenodd fit

  12. observe the new fit plotted in the curve fitter app, and examine the numerical results in the results pane.

    results pane displaying coefficient values and goodness-of-fit statistics for the leg4evenodd fit

    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.

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

    plots for the two fits side by side in the curve fitter app

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.

  1. 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 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
  2. load the angle and counts variables in the workspace.

    load carbon12alpha
  3. 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)
  4. plot the fit and data.

    plot(f,angle,counts)

for more details on linear model terms, see the function.

网站地图