operator '*' is not supported for operands of type 'function_handle'.

7 Ansichten (letzte 30 Tage)
So I am trying to maximize a function. In order to maximze a function f(x), I have to minimize the function -f(x).
I have tried to do this but I get the errors as such.
Any help would be appreciated.
I am using the optimize task from the live script.
Thanks

Akzeptierte Antwort

John D'Errico
John D'Errico am 4 Jun. 2022
Bearbeitet: John D'Errico am 4 Jun. 2022
This question (or the equivalent) gets asked over and over again. I can understand where things get confusing. For example:
f = @(x) x^2;
whos f
Name Size Bytes Class Attributes f 1x1 32 function_handle
f is a function handle. We cannot do things like multiply it by -1, because it is not a number. Just the name of a function. So how do we negate it? We create a NEW function that takes the output of f, and negates THAT.
minusf = @(x) -f(x);
f(5)
ans = 25
minusf(5)
ans = -25
The new function handle minusf does exactly what you need.
  1 Kommentar
Avikash Chand
Avikash Chand am 4 Jun. 2022
I got around that error by multiplying -1 to initial function. so then the objfcn was as follows
This should be fine I think?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by