@ Symbol in function handles as an input variable to another function

2 次查看(过去 30 天)
Dominic Kaiser
Dominic Kaiser2021-11-24
评论: Stephen ,2021-11-24
Hey guys i am currently trying to wrap my head around the concept of function-handles and there is one thing i am not understanding right now. I have the following function which i want to use as an input argument for another function
function[y]=fun2(x)
y = exp(-x.^2).*log(x).^2;
end
and then i have the other function
fun = @fun2
Q = integral(fun,0,inf)
Now the thing that i do not understand is; if i were to remove the @ sign it would tell me that i am missing input arguments and i am confused what does the @ sign change in terms of input arguments. Why do i need more input arguments when all that changed is that i didn't declare it as a function handle, but tried to open it directly? It is probably something elementary that i just didnt fully understand so advices would be highly appreciated.
Thank you in advance!

采纳的回答

Stephen
Stephen 2021-11-24
编辑:Stephen 2021-11-24
"if i were to remove the @ sign it would tell me that i am missing input arguments..."
Because in that case you are just calling the function.
If the called function requires inputs but you do not provide any, then clearly that should throw an error.
"... and i am confused what does the @ sign change in terms of input arguments."
Absolutely nothing.
Defining a function handle makes zero difference to the number of input arguments that a function accepts, or what inputs you need to provide, ar anything about input arguments at all.
"Why do i need more input arguments when all that changed is that i didn't declare it as a function handle, but tried to open it directly?"
If you do not use @ then you are not "opening" a function, you are calling it.
Just like if I call
sin
it will throw an error, if you call your function (which must have minimum one input argument) without any input argument, then clearly that will throw an error. But that has nothing to do with function handles.
  2 个评论
Stephen
Stephen 2021-11-24
"i didn't differentiate between opening a function..."
The only meaning I know of "opening a function" is to open it in some edtor. What does "open" mean to you?

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2021-11-24
Integral will only accept an input function as a function handle. The @ converts the m-file name into a function handle, that integral can accept. Some of the older tools would accept m-file names in quotes, at least in older releases. But I see that quadgk also requires a function handle input in my release.
I will add that infinite limits of integration will sometimes cause a strange to interpret failure, where you may be surprised at the result.

dpb
dpb 2021-11-24
The issue is given by the requirements of the function integral -- the help file for it reads in part--
>> help integral
integral Numerically evaluate integral.
Q = integral(FUN,A,B) approximates the integral of function FUN from A
to B using global adaptive quadrature and default error tolerances.
FUN must be a function handle. ...
...
The first argument to integral MUST be a function handle--in otherwords, a pointer to a function that integral can evaluate internally whatever it is that the user has declared to be the integrand. That's how the flexibility to integrate any function is achieved.
When you break that contract and pass a function name instead, the internals of integral try to evaluate the function as written, but the function needs an argument which isn't there.
It's just operator error in not living up to the requirements of integral

Community Treasure Hunt

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

Start Hunting!

Translated by