MATLAB: Passing extra params to fmincon: anonymous function vs old method

anonymous functionextra parametersfmincon

Hi,
I was trying to pass extra parameters to fmincon function. I did it in two different ways: anonymous functions and old method, and I got 2 different results.
When I use anonymous functions, Matlab said that the initial point is already local minima. But when I use the old method, the optimization proceeds and run until the objective function x reaches f(x) = 0.0468.
Why is it like that? With this situation I prefer to use the old method to pass the extra parameters. However, the old method does not allow me add Output function to fmincon.
Thanks in advance. Amad
—————-
Anonymous function
Code:
myfun = @(k)funObj(kSearch,x0,ti,tP);
[kSearch,resnorm] = fmincon(myfun,kSearch,[],[],[],[],lb,ub,[],optionLSQ);
Output:
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 51 0.0502957 0
Local minimum found that satisfies the constraints.
————————–
Old method:
Code:
[kSearch,resnorm] = fmincon(@funObj,kSearch,[],[],[],[],lb,ub,[],optionLSQ,x0,ti,tP);
Output:
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 51 0.0502957 0
1 114 0.0502503 0 0.000244 -0.752 17.2
2 172 0.0495527 0 0.00781 -0.551 34.1
3 232 0.0494097 0 0.00195 -0.408 10.8

Best Answer

The error is in this line of code:
myfun = @(k)funObj(kSearch,x0,ti,tP);
funObj does not have a variable k, so no matter what value of k is chosen, funobj always returns the same value. You probably meant to write
myfun = @(kSearch)funObj(kSearch,x0,ti,tP);
Alan Weiss
MATLAB mathematical toolbox documentation
Related Question