Why quadprog fvalue3 & fmincon fvalue1 are not same in the example below?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chandra Upadhyaya
el 23 de Dic. de 2022
Comentada: Torsten
el 24 de Dic. de 2022
clc
clear
close all
price=[44.30133121 43.12406916 42.31422055 41.67599213 42.07729061 42.88582271 44.38989514 45.84411461 47.17838781 48.66472142 49.57772213 49.9537966 49.33055149 48.28029342 47.13492903 46.72706493 47.45610769 50.11226391 50.60314012 49.52144286 47.93957865 46.24335397 43.33797528 40.28594902 38.0842816 36.80017942 36.30394883 36.25929615 36.72432769 38.6619497 41.16986843 44.12309007 46.18842079 46.69566671 46.94333393 46.9464285 46.86775644 46.70612372 46.23813628 45.4851 47.01292074 51.29090431 53.48025645 52.85008289 52.1403893 50.98058129 48.48306443 45.56344424 43.8483262 43.21341571 42.97801815 43.29793883];
price=price';
inFlow=[ 70 60 50 42 38 33 28 24 22 22 18 18 18 18 18 16 20 16 16 16 16 16 16 16 16 16 16 16 16 19 19 28 36 70 68 132 138 244 226 240 274 228 200 242 242 180 238 220 200 156 170 120 ];
inFlow=inFlow';
x0 = [inFlow; zeros(size(inFlow))];
stor0 = 180;
k1 = 0.4422;
k2 = 132.837;
MW2kW = 1000;
C2A = 1;
C = [C2A k1 k2 MW2kW];
N = length(inFlow);
% DefineConstraints
LB = [9.072*ones(N,1);
zeros(N,1)];
UB = [85.3*ones(N,1);
Inf(N,1)];
ot = ones(N,1);
b = -5*ot;
A = spdiags([-ot -ot],[0 N],N,N*2);
A2 = spdiags([ot ot -ot -ot],[0 N -1 N-1],N,N*2);
b2 = 900*ot;
A2(1,:) = [];
b2(1,:) = [];
A = [A; A2; -A2];
b = [b; b2; b2];
c = stor0 + cumsum(inFlow);
b = [b; 333.71-c; -63.51+c];
s = -sparse(tril(ones(N)));
s = [s s];
A = [A; s; -s];
Aeq = ones(1,2*N);
beq = sum(inFlow);
X = sym('x',[2*N,1]);
F = sym('F',[N,1]);
P = sym('P',[N,1]);
s0 = sym('s0');
c = sym({'c1','c2','c3','c4'}.','real');
TotFlow = X(1:N)+X(N+1:end);
S = cell(N,1);
S{1} = s0 + c(1)*(F(1)-TotFlow(1));
for ii = 2:N
S{ii} = S{ii-1} + c(1)*(F(ii)-TotFlow(ii));
end
k = c(2)*([s0; S(1:end-1)]+ S)/2+c(3);
MWh = k.*X(1:N)/c(4);
R = P.*MWh;
totR = -sum(R);
totR = subs(totR,[c;s0;P;F],[C';stor0;price;inFlow]);
matlabFunction(totR,'vars',{X},'file','objFcn');
options = optimset('MaxFunEvals',Inf,'MaxIter',1000,...
'Algorithm','interior-point','Display','iter');
tic
[x1, fval1] = fmincon(@objFcn,x0,A,b,Aeq,beq,LB,UB,[],options);
fval1
toc
Rsub = subs(R,[c;s0;P;F],[C';stor0;price;inFlow]);
Rtot = -sum(Rsub);
fsym = gradient(Rtot,X);
f = double(subs(fsym,X,zeros(size(X))));
H = 0.5.*double(hessian(Rtot,X));
qpoptions = optimset('Algorithm','interior-point-convex','Disp','iter');
tic
[x3,fval3] = quadprog(H,f,A,b,Aeq,beq,LB,UB,[],qpoptions);
fval3
toc
norm(x1-x3)
0 comentarios
Respuesta aceptada
Torsten
el 23 de Dic. de 2022
Editada: Torsten
el 23 de Dic. de 2022
Should be
H = double(hessian(Rtot,X));
instead of
H = 0.5*double(hessian(Rtot,X));
since
Rtot(x) = Rtot(0) + f.'*x + 0.5*x.'*H*x
So you solve two different problems with two different solutions.
2 comentarios
Torsten
el 24 de Dic. de 2022
I am supposed to get both fvalue1 & fvalue3 same. Also x1 and x3 should be same.
No.
The Taylor series development of Rtot is as written above. Thus in order to minimize Rtot, you will have to supply H = double(hessian(Rtot,X)) to "quadprog", not H = 0.5*double(hessian(Rtot,X)).
Make the test:
If you use "fmincon" to solve the problem for which you use "quadprog" at the moment, you will get the same result as quadprog gives.
Más respuestas (0)
Ver también
Categorías
Más información sobre Nonlinear Optimization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!