user3546200
user3546200

Reputation: 289

Difference between Matlab's fmincon and quadprog case for linear case

I am trying to convert my quadprog linear quadratic problem over to fmincon so that later I can add nonlinear constraints. I am having difficulty when I compare my solutions using the two methods (for the same problem). The odd thing is that I get very different cost output when I get almost the same x values. Below is a simplified case of my code without constraints.

Here, my objective function is

cost = a + b*x(1) + c*x(1)^{2} + d + e*x(2) + f*x(2)^{2}

%objective function
% cost = a + b*x(1) + c*x(1)^2 + d + e*x(2) + e*x(2)^2 
param = [1;2;3;4;5;6];
H = [2*param(3) 0; 0 2*param(6)];
f = [param(2); param(5)];
x0 = [0,0];

[x1,fval1] = quadprog(H,f);
[x2,fval2] = fmincon(@(x) funclinear(x,param), x0);
fval1
fval2


%% defining cost objective function
function cost = funclinear(x, param);
    cost=(param(1) + param(2)*x(1) + param(3)*(x(1))^2+ param(4) +param(5)*x(2)+param(6)*(x(2))^2);
end

My resulting x1 and x2 are

x1 =[-3.333333333305555e-01;-4.166666666649305e-01];
x2 =[-3.333333299126037e-01;-4.166666593362859e-01];

which makes sense that they are slightly different since they are different solvers.

However my optimized costs are

fval1 =-1.375000000000000e+00;
fval2 =3.625000000000001e+00;

Does this mean that my objective function is different than my H and f? Any help would be appreciated.

Upvotes: 0

Views: 1552

Answers (1)

Daniel1000
Daniel1000

Reputation: 797

In the quadprog formulation, the constant terms a and d are not considered.

param(1)+param(4) = 1 + 4 = 5

The difference of your results is also 5.

Upvotes: 1

Related Questions