## Custom Linear Fitting

### About Custom Linear Models

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.

### Selecting a Linear Fitting Custom Fit Interactively

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

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

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

4. The lower box displays the example equation. Click 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.

### Selecting Linear Fitting at the Command Line

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 + ... ```
where no coefficient appears within any of `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', ... } ```

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 this cell array of terms: ```LinearModelTerms = {'log(x)','x','1'}```.

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

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 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 Fitting 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\left(x\right)=\sum _{n=0}^{\infty }{a}_{n}{P}_{n}\left(x\right)$`

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 `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}\left(x\right)=\frac{1}{{2}^{n}n!}{\left(\frac{d}{dx}\right)}^{n}{\left({x}^{2}-1\right)}^{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:

`${y}_{1}\left(x\right)={a}_{0}+{a}_{2}\left(\frac{1}{2}\right)\left(3{x}^{2}-1\right)+{a}_{4}\left(\frac{1}{8}\right)\left(35{x}^{4}-30{x}^{2}+3\right)$`
1. Load the 12C alpha-emission data by entering

```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 Fitting app by entering:

`cftool`

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

4. 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 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. Click to change the equation terms in the Edit Custom Linear Terms dialog box.

1. Change the Coefficients names to `a2`, `a4`, and `a0`.

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

3. 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 a0(x) and a4(x) are known fairly accurately, but that the a2(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}\left(x\right)={y}_{1}\left(x\right)+{a}_{1}x+{a}_{3}\left(\frac{1}{2}\right)\left(5{x}^{3}-3x\right)$`
• Rename the new fit to `Leg4EvenOdd`.

• Click to change the equation terms. The Edit Custom Linear Terms dialog box opens.

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

1. Click the + button to 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

`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 tile. You can display only the plots by hiding the fit settings and results panes using the Curve Fitting app View menu.

#### Fit Custom Linear Legendre Polynomials at the Command Line

Fit the same model at the command line that you created in Curve Fitting app.

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

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 `fittype` function.