How can I evaluate an objective function in matlab? Im trying to evaluate an objective funciton defined in matlab.

6 ビュー (過去 30 日間)
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 件のコメント
Rachel Surridge
Rachel Surridge 2020 年 7 月 17 日
編集済み: Rachel Surridge 2020 年 7 月 17 日
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
R. Can 2020 年 7 月 17 日
Thank you SOOO much. it does work.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Programming and Mixed-Integer Linear Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by