Operations with function handles / anonymous functions

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.

 採用された回答

the cyclist
the cyclist 2021 年 5 月 25 日

1 投票

Is this what you want?
function1 = @(x) x(1)^2+5
function2 = @(x) 2*x(2)
function12 = @(x) function1(x) + function2(x)

1 件のコメント

D zepp
D zepp 2021 年 5 月 25 日
Thank you, did not realise i had to put the (x) again.

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2021 年 5 月 25 日

1 投票

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 件のコメント

D zepp
D zepp 2021 年 5 月 25 日
Thank you, did not realise i had to put the (x) again.

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

カテゴリ

ヘルプ センター および File ExchangeSymbolic Math Toolbox についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 5 月 25 日

コメント済み:

2021 年 5 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by