%%Function Handles

% Often we want to pass a function as an argument to another function.

%Ex: fzero - finds the roots of a function
%Need a way to tell fzero which function you want to find the roots of
%Can't just write the name of the function - how will MATLAB know it should
%be treated as a function instead of a variable?

%Solution: create a function handle

%Function handle - a way to pass a function to another function
%Special type of variable which tells MATLAB "this is a function and should
%be treated like one"
%Use @ symbol

%Examples:
sinhandle = @sin;
trighandle = @aTrigFunction;
xx = -pi:.1:pi;
plot(xx,sinhandle(xx),xx,trighandle(xx))

%Should give the same value, as they effectively run the same function
disp(trighandle([1,pi]))
disp(aTrigFunction([1,pi]))


%Example: addNoise, modifies values using the given function to add "noise"
xx = 5 : 0.01 : 10;
yy = polyval([0.1,1, 5], xx);
noise1 = addNoise(yy,sinhandle);
noise2 = addNoise(yy,trighandle);
plot(xx,yy,xx,noise1,xx,noise2)

