MATLAB: Reversing/Alternatives of function handles

functionhandlesMATLAB

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

Best Answer

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);