我有以下一段函数定义(测试代码):
function [X,Y,Z] = test(x,y,z)
syms a b c;
a = b + c; % This is where it gets wrong
X=x;
Y=y;
Z=z;
keyboard
% nested functions
function y = fun1(t,x)
y=t+x;
end
function res = bvpbc(y0,yT)
res= y0+yT;
end
end
基本上,我在 test
函数中有一些嵌套函数,我在其中声明了一些符号变量 a
、b
和 c
。但是,当我通过键入运行函数时
测试(1,1,1)
总是有这样的错误信息:
Error using assignin
Attempt to add "b" to a static workspace.
See MATLAB Programming, Restrictions on Assigning to
Variables for details.
Error in syms (line 66)
assignin('caller',x,sym(x));
Error in test (line 3)
syms a b c;
似乎是符号声明有问题,但我不明白为什么。我该如何解决?
谢谢!
编辑:此外,每当我删除这两个嵌套函数时,test
函数都会正常工作。
最佳答案
以下最小工作示例重现了该问题,并且作为 Andrew Janke在评论中解释,不是错误:
function foo
syms A
function nested
end
end
您可以通过将符号变量显式分配给工作区来解决它:
A = sym('A');
关于Matlab: "Error using assignin: Attempt to add "c"to a static workspace",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17105493/