为什么我不能将函数句柄传递给fsolve,但我可以在fsolve中编写一个等效的匿名函数?

时间:2013-12-05 19:03:30

标签: matlab anonymous-function

我发现这有点奇怪。我目前正在编写一个简单的函数来使用fsolve.求解方程组。这就是我所拥有的:

%Variable Declarations
 I0 = 10e-12;
 n = 1;
 Vt = 0.0259;
 R = 10e3;
 Vs = 3;

 %Function 1 (Some may recognize that this is the Shockley Diode Equation, if anyone cares...)
  i1 = @(v1)(I0) * (exp((v1)/(n*Vt))-1);

 %Function 2
 i2 = @(v1) ((Vs-v1)/R);

 %This is what I originally tried
 h = @(v1) i1(v1)-i2(v1);

 fsolve(h(v1), 1)

 %After running this, I receive "Undefined function or variable 'v1.'"
 % However, if I write

 fsolve(@(v1)i1(v1)-i2(v1),1)

 %The function works. With the result, I plugged that value into h(v1), and it produces the expected result (very close to 0)

那说,为什么matlab不允许我将函数句柄传递给fsolve

1 个答案:

答案 0 :(得分:1)

您希望传递一个函数句柄,即h,而不是h(v1)。无法评估h(v1)本身,因为未定义v1

尝试fsolve(h, 1)