Practice function function won't work for "humps"

11 views (last 30 days)
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]

Accepted Answer

Jan
Jan on 19 Sep 2012
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)

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!