2012-01-21 68 views

回答

6

你试过了吗?学习像matlab这样的工具的最好方法是尝试一些东西!

实际上,你甚至不需要创建一个m文件函数。我会在这里使用一个函数句柄。

fun = @(x) sin(x); 
f1 = @(f,a,b,c) c*f(a+b); 
f1(fun,2,3,4) 

ans = 
     -3.8357 

我也可以将f1定义为一个m文件函数,但这需要我保存一个文件。何必?

5

你在找什么是function handle

a = 1; 
b = 2; 
c = 3; 
fun = @sqrt;  % obtain the function handle of sqrt() 
f = f1(fun, a,b,c); % pass the function handle of sqrt() into your function f1(). 

当你使用:

您可以通过将“at”符号(“@”)在函数名前获得函数的函数句柄(在下列情况下,sqrtfun,就好像你在使用sqrt函数。

欲了解更多详细信息,你也可以参考另一个Stackoverflow的问题:function handle in MATLAB