python sympy 库 or matlab 符号计算问题记录
sympy or matlab
关于 matlab 中的 symbol 坑:
符号函数的参数必须是符号,不能是数字,对于耦合的 indexed symbols/symbol array 的符号函数定义,容易出错的点:
1
2
3
4
5syms followers(x)
d = sym('d',[1 3],'positive');
for k=1:2
followers(d(k))=d(k)/(d(1)+d(2))
end函数先定义后赋值,这段代码的实际运行时,首先,k=1,定义 followers(d(1))=d(1)/(d(1)+d(2)),其中 d(1)为自变量,这样分母的 d(1)也是自变量,然后 k=2,
复制给了以符号为参变量的函数 follower。实际上 followers(d(1)) = followers(d(2)) = followers(d(2))=followers(d) 都是 follower 函数针对 d 这一个符号变量的值,这值也就是唯一的,重复赋值,其值只会是最后一个。
Indexing inside symbolic product - MATLAB Answers - MATLAB Central
When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'. Syms can never be used as indices to indexing indexed symbols.