Define Vectors X and Y, containing values of an independent variable x and a dependent variable y.
>
|
|
>
|
|
Find the values of a and b that minimize the least-squares error when the model function is used.
>
|
|
| (1) |
It is also possible to return a summary of the regression model using the summarize option:
>
|
|
| (2) |
|
Model:
|
|
Coefficients
|
Estimate
|
Standard Error
|
t-value
|
P(>|t|)
|
a
|
|
|
|
|
b
|
|
|
|
|
|
|
|
R-squared:
|
|
Adjusted R-squared:
|
|
|
Residuals
|
|
Residual Sum of Squares
|
Residual Mean Square
|
Residual Standard Error
|
Degrees of Freedom
|
|
|
|
|
|
|
Five Point Summary
Minimum
|
First Quartile
|
Median
|
Third Quartile
|
Maximum
|
|
|
|
|
|
|
|
|
|
|
Fit a polynomial of degree 3 through this data.
>
|
|
| (3) |
Use the output option to see the residual sum of squares and the standard errors.
>
|
|
| (4) |
Fit the model function , which is nonlinear in the parameters.
>
|
|
| (5) |
Consider now an experiment where quantities , , and are quantities influencing a quantity according to an approximate relationship
with unknown parameters , , and . Six data points are given by the following matrix, with respective columns for , , , and .
>
|
|
We take an initial guess that the first term will be approximately quadratic in , that will be approximately , and for we don't even know whether it's going to be positive or negative, so we guess . We compute both the model function and the residuals. Also, we select more verbose operation by setting .
>
|
|
>
|
|
In NonlinearFit (algebraic form)
| |
| (7) |
We note that Maple selected the nonlinear fitting method. Furthermore, the exponent on is only about , and the other guesses were not very good either. However, this problem is conditioned well enough that Maple finds a good fit anyway.
Now suppose that the relationship that is used to model the data is altered as follows:
We adapt the calling sequence very slightly:
>
|
|
In LinearFit (container form)
| |
final value of residual sum of squares: .0537598869493245
| |
Summary:
----------------
Model: .82307292*x-.16791011*x^2/y-.75802268e-1*y*z
----------------
Coefficients:
Estimate Std. Error t-value P(>|t|)
a 0.8231 0.1898 4.3374 0.0226
b -0.1679 0.0940 -1.7862 0.1720
c -0.0758 0.0182 -4.1541 0.0254
----------------
R-squared: 0.9600, Adjusted R-squared: 0.9201
| |
| (8) |
>
|
|
This time, Maple could select the linear fitting method, because the expression is linear in the parameters. In addition, as the infolevel is greater than 0 and the expression is linear in the parameters, a summary for the regression is displayed. The initial values for the parameters are not used.
Finally, consider a situation where an ordinary differential equation leads to results that need to be fitted. The system is given by
where and are parameters that we want to find, is a variable that we can vary between experiments, and is a quantity that we can measure at . We perform 10 experiments at , and the results are as follows.
>
|
|
| (9) |
>
|
|
| (10) |
We now need to set up a procedure that NonlinearFit can call to obtain the value for a given input value and a given pair of parameters and . We do this using dsolve/numeric.
>
|
|
| (11) |
>
|
|
| (12) |
We now have a procedure ODE_Solution that can compute the correct value, but we need to write a wrapper that has the form that NonlinearFit expects. We first need to call ODE_Solution once to set the parameters, then another time to obtain the value of at , and then return this value (for more information about how this works, see dsolve/numeric). By hand, we can do this as follows:
>
|
|
| (13) |
| (14) |
>
|
|
| (15) |
Note that for some settings of the parameters, we cannot obtain a solution. We need to take care of this in the procedure we create (which we call f), by returning a value that is very far from all output points, leading to a very bad fit for these erroneous parameter values.
>
|
f := proc(zValue, aValue, bValue) global ODE_Solution, a, b, z, x, t; ODE_Solution('parameters' = [a = aValue, b = bValue, z = zValue]); try return eval(x(t), ODE_Solution(1)); catch: return 100; end try; end proc;
|
| (16) |
>
|
|
We need to provide an initial estimate for the parameter values, because the fitting procedure is only performed in a local sense. We go with the values that provided a solution above: .
>
|
|