Skip to Main Content Skip to Search
Product Documentation

Custom Linear Models

About Custom Linear Models

You can use the Curve Fitting Tool custom equation fit to define your own linear or nonlinear equations. The custom equation fit uses the nonlinear least-squares fitting procedure. See Selecting a Custom Equation Fit Interactively.

You can define a custom linear 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, you must use the legacy Curve Fitting Tool. For an example, see Example: Legendre Polynomial. For a procedure, see Creating Custom Models Using the Legacy Curve Fitting Tool.

Example: Legendre Polynomial

This example uses the legacy Curve Fitting Tool to define custom linear equations.

This example fits 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. Refer to the legendre function for information about generating Legendre polynomials.

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)

Fit the data using a fourth-degree Legendre polynomial with only even terms:

  1. Load the 12C alpha-emission data from the file carbon12alpha.mat, which is provided with the toolbox:

    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 legacy Curve Fitting Tool by entering:

    cftool -v1
  3. Import these two variables into the Curve Fitting Tool.

    In the Curve Fitting Tool, click Data to open the Data dialog box.

    In the Data dialog box:

    1. Select angle and counts for X and Y.

    2. Name the data set C12Alpha

    3. Click Create data set.

    4. Click Close.

  4. In the Curve Fitting Tool, click Fitting to open the Fitting dialog box.

  5. In the Fitting dialog box:

    1. Click New fit.

    2. Specify a meaningful fit name.

    3. Select the C12Alpha data set.

    4. Select Custom Equations for the type of fit.

      The Fit Editor for a custom equation fit type is shown following.

    5. Click New to open the New Custom Equation dialog box.

      You use the Linear Equations tab in the New Custom Equation dialog box, because the Legendre polynomials depend only on the predictor variable and constants. This tab is shown for the model given by 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(Θα).

      Edit these until your New Custom Equation dialog box resembles the example shown.

      1. Change the coefficent names to a2, a4, and a0 (click Add a term to show the third term).

      2. Change the Terms for a2 to (1/2)*(3*cos(x)^2-1).

      3. Change the Terms for a4 to (1/8)*(35*cos(x)^4-30*cos(x)^2+3).

      4. Specify the Equation name Leg4Even.

      5. Click OK.

  6. In the Fitting dialog box, click Apply to save the changes to the fit.

The fit and residuals appear in the Curve Fitting Tool. 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.

The numerical fit results in the Fitting dialog box appear next. The 95% confidence bounds indicate that the coefficients associated with P0(x) and P4(x) are known fairly accurately, but that the P2(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, fit the data using both even and odd terms:

  1. In the Fitting dialog box, click Copy and Edit to make a modified copy of your previous Legendre polynomial.

  2. Use the Linear Equations tab of the New Custom Equation dialog box as follows to fit the model given by y2(x):

    1. Click Add a term twice to add the odd Legendre terms.

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

    3. Change the Terms for a1 to cos(x).

    4. Change the Terms for a3 to cos(x).

    5. Specify the Equation name Leg4EvenOdd.

      The Copy and Edit Custom Equation dialog box should resemble this example.

    6. Click OK to close the dialog box.

  3. In the Fitting dialog box select your new equation Leg4EvenOdd, and click Apply to save the changes to the new fit. Observe the new fit plotted in Curve Fitting Tool.

    The numerical results in the Fitting dialog box 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 is the best one.

Creating Custom Models Using the Legacy Curve Fitting Tool

If you need linear least-squares fitting for custom equations, open the legacy Curve Fitting Tool by entering:

cftool -v1

Use the Linear Equations pane to define custom linear equations.

Create custom equations in the New Custom Equation dialog box. Open the dialog box in one of two ways:

The dialog box contains two tabs: one for creating linear custom equations and one for creating general (nonlinear) custom equations.

Linear Equations

Linear models are linear combinations of (perhaps nonlinear) terms. They are defined by equations that are linear in the parameters. Use the Linear Equations pane on the New Custom Equation dialog box to create custom linear equations.

General Equations

General models are, in general, nonlinear combinations of (perhaps nonlinear) terms. They are defined by equations that might be nonlinear in the parameters. Use the General Equations tab in the New Custom Equation dialog box to create custom general equations.

Editing and Saving Custom Models

When you click OK in the New Custom Equation dialog box, the displayed Equation name is saved for the current session in the Custom Equations list in the Fitting dialog box. The list is highlighted as shown next.

To edit a custom equation, select the equation in the Custom Equations list and click the Edit button. The Edit Custom Equation dialog box appears. It is identical to the New Custom Equation dialog box, but is prepopulated with the selected equation. After editing an equation in the Edit Custom Equation dialog box, click OK to save it back to the Custom Equations list for further use in the current session. A Copy and Edit button is also available, if you want to save both the original and edited equations for the current session.

To save custom equations for future sessions, select File > Save Session in the Curve Fitting Tool.

  


 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS