Question about fminsearch with vector input and output
Show older comments
Hello,
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.
clear
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
More Answers (1)
John D'Errico
on 4 Nov 2021
Edited: John D'Errico
on 4 Nov 2021
0 votes
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
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...
Categories
Find more on Loops and Conditional Statements 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!