Question about vectorized fminsearch using flipud
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Chang seok Ma
il 6 Nov 2021
Risposto: John D'Errico
il 6 Nov 2021
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);
0 Commenti
Risposta accettata
Walter Roberson
il 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))
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 Commento
Più risposte (2)
Matt J
il 6 Nov 2021
Modificato: Matt J
il 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,fval]=fminsearch(@(c) norm(obj2(c)) , [0;0] )
John D'Errico
il 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)
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.
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!