以下代码有什么问题?关于符号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
这是在我运行上述命令窗口时出现的。
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
构造,它会运行。
不要重新定义内置函数'diff'。使用['isAlways'](https://www.mathworks.com/help/symbolic/isalways.html)代替'=='进行符号比较。 'diff(u,'x',1)'与'diff(u,x)'相同。 – horchler
为了扩展@ horchler的评论,您正在创建一个名为'diff'的新变量,它会影响内置的diff功能。一般而言,您需要避免使用与要使用的函数相同的名称命名变量。 – jodag
@horchler谢谢。解决了我的问题。 –