How to write a negative objective function for fmincon

10 ビュー (過去 30 日間)
Lukas Langer
Lukas Langer 2018 年 11 月 11 日
コメント済み: Star Strider 2018 年 11 月 11 日
Hello togehter,
I want to maximize the follwing objective funtion :(30x1+15x2)². Usually the objective function in Matlab wants to minimize. Therefore I tried to set the function as (-30x1-15x2)^2. But I always receive the error "Function definitions are not permitted in this context".
Could you please help me?

採用された回答

Star Strider
Star Strider 2018 年 11 月 11 日
If that is actually the function you want to maximize, use an anonymous function as your objective function:
f = @(x) (30.*x(1) + 15*x(2)).^2;
xv = fmincon(@(x)-f(x), ... );
Note that this negates the evaluated result of the function call. If you negate within the expression, then square it, the result will always be positive.
  2 件のコメント
Lukas Langer
Lukas Langer 2018 年 11 月 11 日
Thanks for your quick support!
Star Strider
Star Strider 2018 年 11 月 11 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by