八度 "FUN must be a string or inline function"

Octave "FUN must be a string or inline function"

问题

Octave中的"FUN must be a string or inline function"消息是什么意思?

在 Octave 4.2.0 (Windows) 中,我尝试 运行 Gradients, Gradient Plots and Tangent Planes 最初是在 MATLAB 7.8 中实现的,但出现如下错误。

建议这是由于Octave与MATLAB不兼容或其他原因造成的。如果有解决方法,请提出建议。

>> syms x y z
>> f=((x^2-1)+(y^2-4)+(x^2-1)*(y^2-4))/(x^2+y^2+1)^2
f = (sym)

   2    2   / 2    \ / 2    \
  x  + y  + \x  - 1/*\y  - 4/ - 5
  -------------------------------
                        2
           / 2    2    \
           \x  + y  + 1/

>> gradf=jacobian(f,[x,y])
gradf = (sym 1x2 matrix)

  [      / 2    2   / 2    \ / 2    \    \       / 2    \              / 2    2
  [  4*x*\x  + y  + \x  - 1/*\y  - 4/ - 5/   2*x*\y  - 4/ + 2*x    4*y*\x  + y
  [- ------------------------------------- + ------------------  - -------------
  [                           3                             2
  [              / 2    2    \                 / 2    2    \                   /
  [              \x  + y  + 1/                 \x  + y  + 1/                   \

    / 2    \ / 2    \    \       / 2    \      ]
  + \x  - 1/*\y  - 4/ - 5/   2*y*\x  - 1/ + 2*y]
  ------------------------ + ------------------]
              3                             2  ]
   2    2    \                 / 2    2    \   ]
  x  + y  + 1/                 \x  + y  + 1/   ]

>> [xx, yy] = meshgrid(-3:.1:3,-3:.1:3);
>> ffun = @(x,y) eval(vectorize(f));
>> fxfun = @(x,y) eval(vectorize(gradf(1)));
>> fyfun = @(x,y) eval(vectorize(gradf(2)));
>> contour(xx, yy, ffun(xx,yy), 30)
error: vectorize: FUN must be a string or inline function
error: called from
    @<anonymous> at line 1 column 15

似乎vectorize需要一个字符串字面量,并未能从符号表达式中获取函数句柄,但您可以使用function_handle代替:

ffun = function_handle(f)

这导致以下 vector-friendly 函数:

ffun =
@(x, y) (x .^ 2 + y .^ 2 + (x .^ 2 - 1) .* (y .^ 2 - 4) - 5) ./ (x .^ 2 + y .^ 2 + 1) .^ 2

可以在以后使用,例如您的 contour 电话:

contour(xx, yy, ffun(xx,yy), 30)