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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by