MATLAB Answers

Matlab 2020a outputs wrong fval for quadprog

6 views (last 30 days)
Paul Gesel
Paul Gesel on 8 Jul 2020
Answered: Alan Weiss on 9 Jul 2020
Running the following code:
k = optimvar('k',2);
c = optimvar('c',2);
objec = (k-2)'*(k-2)+(c-1)'*(c-1);
prob = optimproblem('Objective',objec);
problem = prob2struct(prob);
[x,fval] = quadprog(problem);
sol.c = x(1:2);
sol.k = x(3:4);
% Matlab documention claims:
% [x,fval] = quadprog(___), for any input variables, also returns fval, the value of the objective function at x:
% fval = 0.5*x'*H*x + f'*x
% the real objective function value at x is
% it seems the 0.5 multiplication is incorrect and this is a bug
yields the folloing output:
fval =
ans =
ans =
ans =
Is this a known bug with quadprog in Matlab 2020a?


Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 9 Jul 2020
Your problem has an added constant term that quadprog does not take into account, though solve does. Did you try calling
[sol fval] = solve(prob)
sol =
struct with fields:
c: [2×1 double]
k: [2×1 double]
fval =
To compute things the way you want, try this, where fval is the result of your quadprog optimization:
fval + problem.f0
% and
.5*x'*problem.H*x+problem.f'*x + problem.f0
Alan Weiss
MATLAB mathematical toolbox documentation


Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by