简体   繁体   English

Octave“FUN必须是字符串或内联函数”

[英]Octave “FUN must be a string or inline function”

Question

What is the meaning of "FUN must be a string or inline function" message in Octave? Octave中“FUN必须是字符串或内联函数”的含义是什么意思?

In Octave 4.2.0 (Windows) I am trying to run Gradients, Gradient Plots and Tangent Planes which is originally implemented in MATLAB 7.8, and got the error as below. 在Octave 4.2.0(Windows)中,我试图运行最初在MATLAB 7.8中实现的Gradients,Gradient Plots和Tangent Planes ,并得到如下错误。

Kindly suggest this is due to Octave being incompatible with MATLAB or due to other causes. 请注意,这是因为Octave与MATLAB不兼容或由于其他原因。 If there is a workaround, kindly suggest. 如果有解决方法,请提出建议。

>> 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

It seems that vectorize expects a string literal, and fails to obtain the function handle from a symbolic expression, but you can use function_handle instead: 似乎vectorize需要一个字符串文字,并且无法从符号表达式获取函数句柄,但您可以使用function_handle

ffun = function_handle(f)

which results in the following vector-friendly function: 这导致以下矢量友好功能:

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

that can be used onwards, for example with your contour call: 可用于向前,例如使用contour调用:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM