# Documentation

## Univariate Nonlinear Regression

 Note:   Use only in the MuPAD Notebook Interface. This functionality does not run in MATLAB.

Nonlinear regression can assume any type of relationship between the dependent variable y and independent variables xj. For nonlinear regression, MuPAD® provides the `stats::reg` function. This function uses the least-squares approach for computing the regression. `stats::reg` chooses the parameters p1, ..., pn by trying to minimize the quadratic error:

.

Here xij is the `i`th measurement of the independent variable xj. The `stats::reg` function also can perform weighted least-squares nonlinear regression. By default, weights are equal to 1.

`stats::reg` returns a list of optimized parameters [p1, ..., pn] and the minimized value of the quadratic error for the specified model. Suppose, you want to find a model for the following data:

```sampleX := [1, 2, 3, 4, 5, 6, 7, 8, 9]: sampleY := [36.97666099, 54.14911101, 131.3852077, 30.43939553, 202.2004454, 129.5801972, 321.0663718, 411.3959961, 929.597986]:```

```plot( plot::Scatterplot(sampleX, sampleY, LinesVisible = FALSE) ):```

The scatter plot clearly shows that linear models do not fit the data. The dependency looks similar to exponential. Therefore you can try to fit the data to different models involving exponential functions. Suppose, you want to try fitting the data to the expression :

```fit := stats::reg(sampleX, sampleY, p1 + p2^2*exp(x1)/x1, [x1], [p1, p2])```

The `stats::reg` function returns the parameters of the model and the quadratic error as a nested list. To access the parameters separately, use the following commands:

```a := fit[1][1]; b := fit[1][2]; chi2 := fit[2]```

Now, plot the data and expression that you used to fit the data on the same plot. This plot shows how the model expression fits the data:

```plot2 := plot::Function2d(a + b^2*exp(x)/x, x = 1..9): plot(plot1, plot2)```