MATLAB: Practice function function won’t work for “humps”

function functionhumps

I'm in the process of working through a book of practice problems, and I'm stuck on one that asks me to write a function function that returns the difference between a passed function's maximum and minimum values for a given range, and then plots the function for that range.
This is what I have so far:
function fdif = funcdif( f,X )
%Calculates the difference between an input function's maximum
%and minimum values for a given range
%input:
% f = input function
% X = range of function (with increment)
%output:
% fdif = difference between maximum and minimum values
fval = f(X);
fmax = max(fval)
fmin = min(fval)
fdif = fmax - fmin;
plot(X,fval)
end
I've run it for
f = @(t)(10*e.^(-.25*t)).*sin(t-4) for t = 0:pi/32:6*pi
and for
v = @(x)(e.^(5*x)).*(sin(1./x)) for x = .01:.0001:.2
and it works fine for both. (Obviously, I'd assigned e = exp(1).)
However, when I try to use it for the built-in "humps" function with X = 0:.01:3, I get an error that says
"Subscript indices must either be real positive integers or logicals.
Error in funcdif (line 10) fval = f(X);"
However, when I run the steps of the function in the command window, it works fine. For the record, when I try to plug in the "humps" function, this is what I'm putting into the command line:
X = (0:.01:3)';
funcdif(humps,X)
Can anyone help me figure out what the problem is? Most appreciated.
[EDITED, Code formatted, Jan]

Best Answer

This provides the output of humps() as input to funcdif:
funcdif(humps,X)
But you want to provide a handle to the function:
funcdif(@humps,X)