How do I create a "function function"? How do you make a function that accepts another arbitrary function as an input argument?

For example, the MATLAB "integral" function is a perfect example of a special type of function in MATLAB called a "function function". I want to make a function like this. Is this even possible? Can it be easily done?

 採用された回答

Stephen23
Stephen23 2018 年 11 月 9 日
編集済み: Stephen23 2018 年 11 月 9 日
'... a special type of function in MATLAB called a "function function".'
There is nothing special about a function that accepts a function handle as an input.
'How do you make a function that accepts another arbitrary function as an input argument?'
A function handle is a fundamental MATLAB data type, so they can be passed just like any other data type. Simply define your function with an input argument, and inside that function you can call that input argument as a function. For example:
function myfun(infun)
x = 0:0.1:pi;
y = infun(x);
plot(x,y)
end
and then call it with a function handle:
myfun(@sin)
A function handle is just like any other variable, you can pass it, allocated it to another variable, etc.:
f = @sin
g = f;
g(pi)

1 件のコメント

Mario Hernandez
Mario Hernandez 2018 年 11 月 9 日
編集済み: Mario Hernandez 2018 年 11 月 9 日
Wow, you're right. It's just that simple. Thank you.

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

カテゴリ

ヘルプ センター および File ExchangeFunction Handles についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by