필터 지우기
필터 지우기

Negation of a function handle

조회 수: 3 (최근 30일)
Daniela Würmseer
Daniela Würmseer 2022년 2월 19일
답변: John D'Errico 2022년 2월 19일
Hello,
i have an Optimizationproblem where i want to negate my functions (which are nonlinear) if it is a max Problem.
minusf2 = @(x) -f2;
This is working but at one point of my programm i want to do the following:
c1 =@(x) f2(x) - d(2)*x(size(A,2)+1) - z(2);
and then i get this message:
Unary operator '-' is not supported for operand of type 'function_handle'
(My code is perfectly working for min Problems, but i wanted to try to apply it even for max Problems by negating functions but then my calculations are not working. Does someone knows a trick here? Thanks

채택된 답변

John D'Errico
John D'Errico 2022년 2월 19일
minusf2 = @(x) -f2;
"This is working"
No. In fact, that does NOT work. That will simply not work. If f2 is a function handle, and you want to negate it, then you do this:
minusf2 = @(x) -f2(x);
So you evaluate f2, and then negate the result of f2.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by