fmin additional variables ( are vector)

2 ビュー (過去 30 日間)
alejandro lopez
alejandro lopez 2020 年 5 月 17 日
コメント済み: alejandro lopez 2020 年 5 月 19 日
Hello
I am trying to use fmincon but add extra parameters to the function. The function is
function sw = opt(x,g_14,k_4)
sw = ((log(1+x(1))*log(1+x(1)))+log(1+x(1)).*g_14 + k_4)/(1.6806) + ((log(1+x(2))*log(1+x(2))).*g_14 + ((log(1+x(3))*log(1+x(3))).*g_14 ;
end
where g_14 is a numeric vector that change. The system is
x0 = [0,0,0];
A = [1 -1 0;
0 1 -1;
1 0 -1];
b = [0 0 0];
Aeq = [176.9880 179.0538 296.3700];
beq = [1225];
lb = [0 0 0];
up = [Inf Inf Inf];
And finally; [X fval exitflag output lambda grad hessian] = fmincon(@(x) fun(x,g_14,k_4) , x0, A, b, Aeq, beq,lb, up);
When i run the code , MATLAB returns : Supplied objective function must return a scalar value.
So, I try change objetive function and solution by
  • [X fval exitflag output lambda grad hessian] = fmincon(fun, x0, A, b, Aeq, beq,lb, up);
  • function sw = opt(x)
sw = ((log(1+x(1))*log(1+x(1)))+log(1+x(1)).*g_14 + k_4)/(1.6806) ((log(1+x(2))*log(1+x(2))).*g_14 + ((log(1+x(3))*log(1+x(3))).*g_14 ;;
end
So in this case , the problem is ; Undefined function or variable 'g_14'. , even though I had already defined them on the outside
Thanks !!

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 5 月 17 日
Your first attempt was right
[X fval exitflag output lambda grad hessian] = fmincon(@(x) fun(x,g_14,k_4) , x0, A, b, Aeq, beq,lb, up);
The problem is that your objective function returns a vector, while fmincon expects a scalar to minimize. Either you have some problem with your error definition or you want to get a least squares solution. If the second case, just do:
sw = ((log(1+x(1))*log(1+x(1)))+log(1+x(1)).*g_14 + k_4)/(1.6806) + ((log(1+x(2))*log(1+x(2))).*g_14 + ((log(1+x(3))*log(1+x(3))).*g_14 ;
sw = norm(sw);
  1 件のコメント
alejandro lopez
alejandro lopez 2020 年 5 月 19 日
Hi Thiago, I'm very grateful for your answer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by