Cannot call function
5 ビュー (過去 30 日間)
古いコメントを表示
Simple Problem: Cannot call function; Function function? Need to use function handles? Error: "Undefined function or method ... for input arguments of type 'double'." What am I missing?
Very simple code, I think this effectively teaches the problem to avoid -- once we can figure out what the problem is! Please help me; I'm so terribly frustrated. Here is the code:
a = [-1 0 1 2 3];
b = 2*a;
myinter = inline( 'interp1(xdata,ydata,vari)','xdata','ydata','vari')
myfun = inline('3*myinter(xdata,ydata,vari)','xdata','ydata','vari')
myinter(a,b,2.7) % testing to see that it works
myfun(a,b,3.3) % testing to see that it works
Here is the resulting output:
myinter =
Inline function:
myinter(xdata,ydata,vari) = interp1(xdata,ydata,vari)
myfun =
Inline function:
myfun(xdata,ydata,vari) = 3*myinter(xdata,ydata,vari)
ans =
5.4000
??? Error using ==> inlineeval at 15
Error in inline expression ==> 3*myinter(xdata,ydata,vari)
Undefined function or method 'myinter' for input arguments of type 'double'.
Error in ==> inline.subsref at 27
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr,
INLINE_OBJ_.expr);
Error in ==> script at 15
myfun(a,b,3.3) % testing to see that it works
I think it's because I'm trying to create a 'function function'? I don't understand why it's giving me this issue, though, since it seems no different from inline('2*sin(x)'), except sin is a built-in function, while mine are not. I'm trying to start simple: myfun will eventually be the integrand for the 'quad' command, with factors like x*exp(-y*z) where x, y, and z are all interpolated values. I must learn how to call functions within functions -- are such functions called "function functions" as http://www.mathworks.com/help/techdoc/ref/function_handle.html suggests?
I've spent several hours and I still don't understand function handles and when they must be used. Something about the syntax escapes me. Please help! Thank you.
0 件のコメント
採用された回答
Walter Roberson
2012 年 3 月 15 日
Inline functions are not evaluated in your current workspace. "myinter" is defined in only in your current workspace, and so is not available to "myfun"
Try
myinter = @(xdata, ydata, vari) interp1(xdata,ydata,vari);
myfun = @(xdata, ydata, vari) 3 * myinter(xdata, ydata, vari);
Note: in this situation you could abbreviate
myinter = @interp1;
0 件のコメント
その他の回答 (1 件)
per isakson
2012 年 3 月 15 日
I never use inline, because I believe that anonymous functions are a better alternative
a = [-1 0 1 2 3];
b = 2*a;
myinter = @(xdata,ydata,vari) interp1( xdata, ydata, vari );
myfun = @(xdata,ydata,vari) 3 * myinter( xdata, ydata, vari );
myinter(a,b,2.7) % testing to see that it works
myfun(a,b,3.3) % testing to see that it works
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!