Univariate Nonlinear Regression

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

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 ith 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]:

Plotting the data can help you choose the model:

plot1 := 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)