How can I evaluate an objective function in matlab? Im trying to evaluate an objective funciton defined in matlab.
Show older comments
Hello there everyone,
I have the next problem
x = optimvar('x',4);
objec =(2*x(1))*(2*x(1))+(x(1)-x(4))*(x(1)-x(4))+(2*x(2)+x(3))*(2*x(2)+x(3))+(2+x(2))*(2+x(2))+x(3)*x(3)
prob = optimproblem('Objective',objec);
prob.Constraints.cons1 = x(1)+x(2) == 1;
prob.Constraints.cons2 = x(1) >= 0;
prob.Constraints.cons3 = x(2) >= 0;
problem = prob2struct(prob);
[x,fval1] = quadprog(problem)
which works fine and gives
x =
0.7143
0.2857
-0.2857
0.7143
fval1 =
3.4286
However, for other more general cases, I need to evaluate the objective function objec at (0,0,0,0) to check if there is a constant remaining (in this case, that constant is 4).
However, if I try to evaluate at y=[0 0 0 0]
val = evaluate(prob.Objective,y)
I get the error:
Error using optim.problemdef.OptimizationExpression/evaluate
Second argument must be a nonempty struct.
I wish to get the value val=4 but it is just not working.
Can someone give me some idea on this?
Thank you.
2 Comments
Rachel Surridge
on 17 Jul 2020
Edited: Rachel Surridge
on 17 Jul 2020
Hello! The evaluate function expects a structure as its second argument. This structure should represent the point at which to evaluate. Try:
sol.x = [0 0 0 0]
val = evaluate(prob.Objective, sol)
R. Can
on 17 Jul 2020
Answers (0)
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!