如何修复:Octave上的“匿名函数主体必须是单个表达式”错误

时间:2019-04-06 01:12:20

标签: octave anonymous-function

我正在尝试在Octave中创建一个函数,在该函数中,您给octave一个函数f(x,y)作为字符串,X的变化,Y的变化,起点和矩阵的大小,函数将创建一个矩阵,该矩阵在矩阵的每个点上都填充有f(x,y)的值。

这是用于显示3d图形的应用程序,它使用矩阵将每个值映射到一个块

# funcStr: The function whose Z values are being calculated
# dx: the change in x that each block in the x direction represents
# dy: the change in y that each block in the y direction represents
# startPt: the point (in an array of x, y) that center block represents
# res: the side length (in blocks) of the plane

pkg load symbolic
syms x y
function[zValues] = calculateZValues(funcStr, dx, dy, startPt, res)

zValues = zeros(res);
eqn = @(x, y) inline(funcStr);

startX = startPt{1};
startY = startPt{2};

for yOffset = 1:res
    for xOffset = 1:res
        xCoord = startX + dx * xOffset;
        yCoord = startY + dy * yOffset;
        zValues(res * yOffset + xOffset) = double(subs(eqn, @(x, y), {xCoord, yCoord}));
    endfor
endfor
endfunction

我得到的错误是:

>> calculateZValues("x*y", 1, 1, {0,0}, 10)
parse error near line 20 of file /home/rahul/Documents/3dGraph/graph/calculateZValues.m

  anonymous function bodies must be single expressions

>>>             zValues(res * yOffset + xOffset) = double(subs(eqn, @(x, y), {xCoord, yCoord}));

我不知道问题是什么。在错误所引用的行中,我用{x,y}替换了@(x,y)部分,但是它什么也没说,或者引发了有关未声明函数subs的错误。我还尝试过将pkgsyms行移到函数头上方

0 个答案:

没有答案