Question

I want to fit two parameters on an equation using fmincon. I did that with lsqnonlin but I want to take advantage of the inequality constraints of fmincon.

The problem is that I can not pass the two vectors of the measured data, e.g. y(x), into the function in the same way I did it in lsqnonlin.

How should I do this?

estim = lsqnonlin(@fit_simp,X0,lb,ub,options,X,Y) %X,Y are the measured data in vectors, y(x)

estim = fmincon(@fit_simp,X0,[],[],[],[],[],[],nonlcon,options); 

how will I pass the measured vectors?

Was it helpful?

Solution

Use an anonymous function like this:

estim = fmincon(@(y)fit_simp(y),X0,[],[],[],[],[],[],nonlcon,options); 

Obviously you have to adjust your objective function to accept the parameters, i.e. fit_simp becomes fit_simp(y)

This is directly from the documentation, search for anonymous in this page:

http://www.mathworks.com/help/optim/ug/fmincon.html and also http://www.mathworks.com/help/optim/pass-extra-parameters.html

I just saw in your code you want to pas in two extra parameters, X and Y. You might have to encapsulate these in a single variable for the anonymous function (but also maybe you can send both I'm not sure)

try

options.Y = Y;
options.X = X;

and then use @(options)fit_simp(options)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top