Question about vectorized fminsearch using flipud

1 view (last 30 days)
Hello,
I am trying to understand how the following code optimize the objective function.
I thought if I use vector as an input for fminsearch as below, the result 'Kp' should give me the point where the objective function is minimized.
For example, Kp[1] should give me the point where the objective function is minimized for given K[1] and Z[1]. So it is just solving univariate function.
But if I have the objective function as below. Kp[4] should also affect the Kp[1] because there is flipud(Vs) inside the function and vice versa.
And fminsearch still gives me the result without showing any error message.
If this is the case, what does exactly fminsearch minimize? Is Kp[1] still minimize the objective function for given K[1] and Z[1]?
Does fminsearch take this as a multivariate optimization problem instead of univariate in the case without flipud(Vs)?
If so, how does fminsearch minimze the objective function because then there are more than one equations to minimize in this case?
clear
clc
R = 1.008;
sig = 0.75;
tempkgrid = linspace(-2,6,2)';
K = [tempkgrid ; tempkgrid];
Z = [2*ones(2,1);4*ones(2,1)];
aconst1 = -2*ones(4,1);
aconst2 = 6*ones(4,1);
const = R * (K + Z);
obj = @(Vs) -((1/(1-1/sig)) * ((Z + K - Vs./R) > 0) .* (Z + K - Vs./R).^(1-1/sig) + flipud(Vs));
Kp = fminsearchbnd(@(c) norm(obj(c)) ,aconst1, aconst1, const);

Accepted Answer

Walter Roberson
Walter Roberson on 6 Nov 2021
fminsearch() and John's fminsearchbnd() can never handle multiple equations.
You use norm(), and norm reduces array inputs to scalars, so as far as fminsearchbnd() can tell you only have a single output.
norm(rand(3,5))
ans = 2.3418
Question: why do you calculate aconst2 if you are not going to use it?
And why are you passing in the same initial point and lower bound? fminsearchbnd(fun,x0,lb,ub...)
  1 Comment
Chang seok Ma
Chang seok Ma on 6 Nov 2021
Edited: Chang seok Ma on 6 Nov 2021
Oh... I see... now I see what is going on.... Thank you very much. This made me struggling couple of days.
So the result Kp should give me the number which minimizes the norm of the objective function... I thought I was having 4 different objective functions in this case...
I was going to use aconst as upper bound in the first place but instead I just used const.
I haven't thought about it because this is just a sample code, but is there an issue if I make the initial point as an lower bound?
Then, shouldn't fminsearch search minimizer starting from the lower bound?

Sign in to comment.

More Answers (2)

Matt J
Matt J on 6 Nov 2021
Edited: Matt J on 6 Nov 2021
I think it would help to work with a simpler example, and one that actually uses fminsearch directly. As you can see below, the presence of flip() does affect the solution. There is no reason to think that it won't. There is no reason to expect an error message in either case, however, because in both cases, you are providing a legitimate function from R^2-->R to be minimized.
obj0=@(Vs) [Vs(1)-1; Vs(2)-2];
obj1=@(Vs) obj0(Vs) + flip(Vs);
obj2=@(Vs) obj0(Vs) + Vs;
[Vs,fval]=fminsearch(@(c) norm(obj1(c)) , [0;0] )
Vs = 2×1
0.7516 0.7484
fval = 0.7071
[Vs,fval]=fminsearch(@(c) norm(obj2(c)) , [0;0] )
Vs = 2×1
0.5000 1.0000
fval = 8.3772e-05
  1 Comment
Chang seok Ma
Chang seok Ma on 6 Nov 2021
Thank you very much.
Bit embarrasing but I was keep thinking I was getting multiple values for 'fval'. That was the reason I was struggling with this issue.
Thanks for all the comments!

Sign in to comment.


John D'Errico
John D'Errico on 6 Nov 2021
I fail to undertand your question. It minimizes what you give it. If the flipud is in there, your objective function is different.
R = 1.008;
sig = 0.75;
tempkgrid = linspace(-2,6,2)';
K = [tempkgrid ; tempkgrid];
Z = [2*ones(2,1);4*ones(2,1)];
aconst1 = -2*ones(4,1);
aconst2 = 6*ones(4,1);
const = R * (K + Z);
But most importantly, consider what even just a part of your objective functino looks like:
obj1 = @(Vs1) -((1/(1-1/sig)) * ((Z(1) + K(1) - Vs1./R) > 0) .* (Z(1) + K(1) - Vs1./R).^(1-1/sig) + Vs1)
obj1 = function_handle with value:
@(Vs1)-((1/(1-1/sig))*((Z(1)+K(1)-Vs1./R)>0).*(Z(1)+K(1)-Vs1./R).^(1-1/sig)+Vs1)
So, here I have extracted just the firt component of that objective. Now, plot that over the range you gave it:
fplot(obj1,[-2,6])
Fminsearch is NOT designed to solve discontinuous problems. You will get random garbage. It is not smart enough to know how to search the entire domain.

Community Treasure Hunt

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

Start Hunting!