# Question about fminsearch with vector input and output

6 views (last 30 days)

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.

##### 0 Comments

### Accepted Answer

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.

### More Answers (1)

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
on 4 Nov 2021

Edited: Matt J
on 4 Nov 2021

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!