关于符号 MATLAB

About symbolic MATLAB

下面的代码有什么问题?

clear all

syms x y a ;

u=2*x*y;
v=a^2+x^2-y^2;

diff=diff(u,'x',1)+diff(v,'y',1);
if diff==0
    disp('Liquid motion is possible.')
    disp('Stream function exists.')
else
    disp('Liquid motion is not possible.')
    disp('Stream function does not exist.')
end

diff2=diff(v,'x',1)-diff(u,'y',1);
if diff2==0
    disp('Velocity potential exists.')
else
    disp('Velocity potential does not exist.')
end

这是在我运行上面的命令window中出现的。

Liquid motion is possible.
Stream function exists.
Error using sym/subsindex (line 672)
Invalid indexing or function definition. When defining a function, ensure that the body of the function is a SYM
object. When indexing, the input must be numeric, logical or ':'.

Error in sym>privformat (line 1502)
    x = subsindex(x)+1;

Error in sym/subsref (line 694)
            [inds{k},refs{k}] = privformat(inds{k});

Error in q7 (line 17)
diff2=diff(v,'x',1)-diff(u,'y',1);

但是如果我在第一个 if 构造之后重写(重新定义)符号变量,它 运行 没问题。此外,如果我取消第一个 if 构造,它 运行s.

我会避免覆盖保留名称,所以

diff=diff(u,'x',1)+diff(v,'y',1);

我会建议

derFcn = diff(u,'x',1)+diff(v,'y',1);

这会触发第二个错误;

diff2=diff(v,'x',1)-diff(u,'y',1);

此时 diff 是你的 diff 值(顺便说一句,它是 0)所以它等同于 write

0(v,'x',1)

这当然不会编译,这不是你的意思。

所以,请进行替换(并相应地更新您的 if 语句)。