Reversing/Alternatives of function handles

9 ビュー (過去 30 日間)
Utsav Bhattarai
Utsav Bhattarai 2020 年 10 月 15 日
コメント済み: Utsav Bhattarai 2020 年 10 月 15 日
Hi there,
I am learning Matlab and am new to the concept of function handles. I want to see how function handles make the code simpler thats why I want to see examples of alternatives for function handles. For example given this code:
sine = @(t) sin( pi*t);
To my understanding, (t) is where any input goes which eventually evaluates sin(pi*t). But what is the difference if I were to just have a variable t and change it everytime and just have sine = sin(pi*t) where t is a seperate variable. So I just want to know the reasoning behind using function handles thats all.
thanks

採用された回答

David Hill
David Hill 2020 年 10 月 15 日
Look at the following.
t=0:.01:.5;
y=sin(pi*t);%works fine if t defined before
t=.5:.01:1;%if t changes, y does not change
Using function handle, you can get y for different values of t without having to write the function multiple times.
fun = @(t) sin( pi*t);
y=fun(0:.01:.5);
y=fun(.5:.01:1);
  1 件のコメント
Utsav Bhattarai
Utsav Bhattarai 2020 年 10 月 15 日
Oh this puts it into an understandable perspective, thank you David!

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

その他の回答 (2 件)

Steve Eddins
Steve Eddins 2020 年 10 月 15 日
One of the major uses of function handles is for use with functions that operate on other functions. For example, you can use the integral function to compute the definite integral of your function over the interval :
>> f = @(t) sin(pi*t);
>> integral(f,0,pi)
ans =
0.6056
The integral function uses adaptive numerical algorithms to determine where to evaluate in order to provide an accurate answer.

Bjorn Gustavsson
Bjorn Gustavsson 2020 年 10 月 15 日
Some of our mathematic operations does not only depend on calculations of their value at a fixed set of known points.
For example we have optimization, integration and integration of differential equations.
For optimization we want to search for the best parameters, which gives the smalles function-value. This is most conveniently done if we have an optimization-function that can handle a large range of input-functions - that is where the function-handles comes in, an optimization-function that can handle a function-handle can optimize any function, otherwise we'd have to produce tailormade optimization-functions for each optimization-problem (this tailor-making might not be terribly complicated, perhaps it would be possible to just change some function-calls and input-arguments at a couple of places in this optimization-function, but many run multiple optimizations per day and it is way mode convenient to use function-handles).
For integration we have the same situation, it is way easier if we want to integrate some complicated function to be able to call the integration-function with a function-handle (for multi-dimensional integrals we might even want to have some of the integration-boundaries be curves that are easiest to handle with function-handles), for example:
Q1 = integral(@(x) x.^2,0,3)
% or
Q2 = integral(@(x) exp(-x^2),0,inf);
For differential equations we have the same situation, the ODE and PDE-integrating functions use very clever schemes that evaluate the differential equation and its solution at many intermediate positions, the best way is for such functions to accept a function-handle (or multiple function-handles for the PDE with initial and boundary-conditions) that they can evaluate as is required.
Then it is possible to imagine additional use-cases as well, I remember using function-handles for some other problems but cannot remember exactly what...
...however, once you've grasped the concept you should also remember that it is not the only programming-pattern you should use, some other problems are better solved with other tricks...
Hopefully this clarifies the benefit of function-handles.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by