Practice function function won't work for "humps"

11 ビュー (過去 30 日間)
Jack Dempsey
Jack Dempsey 2012 年 9 月 19 日
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]

採用された回答

Jan
Jan 2012 年 9 月 19 日
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)
  1 件のコメント
Jack Dempsey
Jack Dempsey 2012 年 9 月 19 日
Thanks! That was what I needed to do!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by