Operations with function handles / anonymous functions

2 vues (au cours des 30 derniers jours)
D zepp
D zepp le 25 Mai 2021
Commenté : D zepp le 25 Mai 2021
I am trying to find the proper way to add two functions. These are expressed as function handles because I need to use these functions as an fmincon parameter.
Example:
function1 = @(x) x(1)^2+5
function 2 = @(x) 2*x(2)
Sum would be: @(x) x(1)^2 + 2*x(2) + 5
I am confused by the documentation surrounding these types. I know there probably is a way to sum these handles or some other workaround, but I do not see it.
Thanks in advance.

Réponse acceptée

the cyclist
the cyclist le 25 Mai 2021
Is this what you want?
function1 = @(x) x(1)^2+5
function2 = @(x) 2*x(2)
function12 = @(x) function1(x) + function2(x)
  1 commentaire
D zepp
D zepp le 25 Mai 2021
Thank you, did not realise i had to put the (x) again.

Connectez-vous pour commenter.

Plus de réponses (1)

Fangjun Jiang
Fangjun Jiang le 25 Mai 2021
function1 = @(x) x(1)^2+5
function2 = @(x) 2*x(2)
function3=@(x) function1(x)+function2(x)
function1([1 2])
function2([1 2])
function3([1 2])
  1 commentaire
D zepp
D zepp le 25 Mai 2021
Thank you, did not realise i had to put the (x) again.

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by