@Marcos there is no need to use global variables. For example... y = 3 + 4*exp(x*1.5) + randn(size(x))/3;
The model is
y = a + b*exp(c*x);
help nlinfit
nlinfit - Nonlinear regression
This MATLAB function returns a vector of estimated coefficients for the
nonlinear regression of the responses in Y on the predictors in X using
the model specified by modelfun.
Syntax
beta = nlinfit(X,Y,modelfun,beta0)
beta = nlinfit(X,Y,modelfun,beta0,options)
beta = nlinfit(___,Name=Value)
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(___)
Input Arguments
X - Predictor variables
matrix
Y - Response values
vector
modelfun - Nonlinear regression model function
function handle
beta0 - Initial coefficient values
vector
options - Estimation algorithm options
structure created using statset
Name-Value Arguments
ErrorModel - Form of error term
'constant' (default) | 'proportional' | 'combined'
ErrorParameters - Initial estimates for error model parameters
1 or [1,1] (default) | scalar value | two-element vector
ReduceJacobian - Flag to reduce Jacobian rank
true or 1 (default) | false or 0
Weights - Observation weights
vector | function handle
Output Arguments
beta - Estimated regression coefficients
vector
R - Residuals
vector
J - Jacobian
matrix
CovB - Estimated variance-covariance matrix
matrix
MSE - Mean squared error
scalar value
ErrorModelInfo - Information about error model fit
structure
Examples
openExample('stats/NonlinearRegressionModelUsingDefaultOptionsExample')
openExample('stats/NonlinearRegressionUsingRobustOptionsExample')
openExample('stats/NonlinearRegressionUsingObservationWeightsExample')
openExample('stats/NonlinearRegressionUsingWeightsFunctionHandleExample')
openExample('stats/NonlinearRegressionUsingNonconstantErrorModelExample')
See also fitnlm, nlparci, nlpredci, Nonlinear Regression Fitter
Introduced in Statistics and Machine Learning Toolbox before R2006a
Documentation for nlinfit
doc nlinfit
fun = @(b,X) b(1)+b(2)*exp(b(3)*X);
paramsnl = nlinfit(x,y,fun,bstart)
paramsnl =
3.5314 3.5085 1.6182
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
The solution should be something close to the vector [3,4,1.5]. As you can see, it did well. Now we can try fmincon.
ssqobj = @(Bfit) sum((y - fun(Bfit,x)).^2);
Note that the variables X and Y are buried inside the functions fun and ssqobj. There is no need for any global variables.
paramsfmincon = fmincon(ssqobj,bstart)
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.
paramsfmincon =
3.5314 3.5085 1.6182
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
I could have employed constraints on the variables of course, since fmincon was used. But what you need to see was nothing special was done, EXCEPT for the creation of a wrapper like ssqobj to convert the function in fun into a scalar objective that fmincon can use.