When I use nlinfit, the sintax nlinfit(t0,p0 ,@(b,t) ...),but if Ireplace nlinfit with fmincon, the sintax is not valid, why?

When I use nlinfit, the sintax nlinfit(t0,p0 ,@(b,t) ...) works, but if Ireplace nlinfit with fmincon, the sintax is not valid, why?

 Respuesta aceptada

Because it is a different function. The syntax of fmincon is documented here,

2 comentarios

Marcos
Marcos el 7 de Feb. de 2026
Movida: Stephen23 el 7 de Feb. de 2026
Thank you for the answer. I have to figure out how to solve this. I will try to use global variables and see if it works
It is not just a question of directly replacing nlinfit with fmincon. nlinfit assumes you will be minimizing a sum of squares of the residuals. So nlinfit assumes a function form that approximates f(x), then it implicitly forms the sum of squares of residuals. (Well, actually, it does not truly need to do that, but in terms of the mathematics, you can think of it like that.)
HOWEVER, fmincon is designed to mimimize a scalar function of some parameters. It has no clue that you want it to form a sum of squares of the residuals. It is you who needs to do that.

Iniciar sesión para comentar.

Más respuestas (1)

@Marcos there is no need to use global variables. For example...
x = rand(20,1);
y = 3 + 4*exp(x*1.5) + randn(size(x))/3;
plot(x,y,'.')
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
bstart = rand(1,3)*2;
fun = @(b,X) b(1)+b(2)*exp(b(3)*X);
paramsnl = nlinfit(x,y,fun,bstart)
paramsnl = 1×3
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 = 1×3
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.

Categorías

Etiquetas

Preguntada:

el 7 de Feb. de 2026

Respondida:

el 8 de Feb. de 2026

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by