MuPAD® notebooks are not recommended. 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
ith measurement of the independent variable xj.
also can perform weighted least-squares nonlinear regression. By default,
weights are equal to 1.
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): plot(plot1)
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])
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; b := fit; chi2 := fit
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)