Clear Filters
Clear Filters

Question about fminsearch with vector input and output

6 views (last 30 days)
I am struggling with fminsearch these days.
I found that fminsearch can be used with vector input but I was also told that I should be careful when I use them.
And there is one thing I don't get it so far.
The sample code is as below.
R = 1.00795101;
sig = 0.75;
tempkgrid = linspace(-20,60,100)';
K = [tempkgrid ; tempkgrid ; tempkgrid];
Z = [2*ones(100,1);4*ones(100,1);6*ones(100,1)];
aconst1 = -20*ones(300,1);
aconst2 = 60*ones(300,1);
const = R * (K + Z);
obj = @(Vs) -((1/(1-1/sig)) * ((Z + K - Vs./R) > 0) .* (Z + K - Vs./R).^(1-1/sig) + ...
((Z + K - Vs./R) <= 0) * (-999999));
Sol_v = fminsearchbnd(@(c) norm(obj(c)) ,(aconst1 +aconst2)/2, aconst1, const);
obj2 = @(Ss) -((1/(1-1/sig)) * ((Z(1) + K(1) - Ss/R) > 0) * (Z(1) + K(1) - Ss/R)^(1-1/sig) + ...
((Z(1) + K(1) - Ss/R) <= 0) * (-999999));
Sol_s = fminsearchbnd(obj2,(aconst1(1) +aconst2(1))/2, aconst1(1), const(1))
So basically, obj is the objective function using vectorized input Vs.
And obj2 is the scalarized version using the first element of each vector.
So, what I thought in the first place is that the first element of 'Sol_v' should be equal to 'Sol_s' because it has to be the same problem.
But somehow the first element of Sol_v is -20 while the Sol_s gives me -18.1431
I think I should optimize the function using for loop over the grid points instead of using vectorized version but I still don't get how these two answers are different.
Can anyone help me with this issue?
Thanks in advance.

Accepted Answer

Matt J
Matt J on 4 Nov 2021
Edited: Matt J on 4 Nov 2021
When I run your code, I get the exit message below. Did you not get the same?
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: NaN
The answers are different because the first version never converged.
Moreover, note that if any one of the terms of the vectorized objective function strays into a region where it evaluates to NaN, the whole objective function norm(obj(c)) evaluates to NaN as well. This means that a failure in one of the terms makes life difficult for all the others.
  1 Comment
Chang seok Ma
Chang seok Ma on 4 Nov 2021
Oh I see your point.
So if any element of the vector lead to NaN, then I should not use fminsearch for optimization.
answer = zeros(300,1);
for i = 1:300
obj3 = @(Ss) -((1/(1-1/sig)) * ((Z(i) + K(i) - Ss/R) > 0) * (Z(i) + K(i) - Ss/R)^(1-1/sig) + ...
((Z(i) + K(i) - Ss/R) <= 0) * (-999999));
answer(i) = fminsearchbnd(obj3,(aconst1(i) +aconst2(i))/2, aconst1(i), const(i));
I change the code to find the minimum using for loop.
Thanks for the comments.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 4 Nov 2021
Edited: John D'Errico on 4 Nov 2021
You want to use fminsearchbnd with 300 UNKNOWNS? REALLY 300? THREE HUNDRED?
SIGH. Are you not the one I already told not to use it with 30 unknowns. So 300 unknowns must be way better. Will you next try 3000 unknowns?
AGAIN, 6-8 unknowns should be your maximum. Hoping to try to use fminsearch or fminsearchbnd with that many unknowns is asking for complete garbage as a result.
  1 Comment
Matt J
Matt J on 4 Nov 2021
Edited: Matt J on 4 Nov 2021
I think the question though is, would there be an exception to this rule in cases (like the posted problem) where the objective function is additively separable into many 1D objective functions? In other words, is fminsearch not smart enough to see that the search can be done separably? I don't think it is...

Sign in to comment.


Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!