2017-11-11 68 views
0

以下代码有什么问题?关于符号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构造,它会运行。

+1

不要重新定义内置函数'diff'。使用['isAlways'](https://www.mathworks.com/help/symbolic/isalways.html)代替'=='进行符号比较。 'diff(u,'x',1)'与'diff(u,x)'相同。 – horchler

+0

为了扩展@ horchler的评论,您正在创建一个名为'diff'的新变量,它会影响内置的diff功能。一般而言,您需要避免使用与要使用的函数相同的名称命名变量。 – jodag

+0

@horchler谢谢。解决了我的问题。 –

回答

0

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

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值(其中,顺便为0),因此它相当于写

0(v,'x',1) 

其中,当然,将无法编译,这是不是你的意思。

因此,请进行替换(并相应更新您的if语句)。

+0

对不起,我只有在发布我的帖子后才会阅读其他回复。我认为他们应该作为答案发布。另外,diff == 0是不正确的,因为您正在比较符号表达式。 –

+0

使用isAlways而不是==给了我如果构造中的其他答案,这在这里是不正确的。任何想法为什么? –

+0

请不要在你的帖子上签名。每篇文章都以您的用户卡片和您的个人资料链接结尾,帖子中的签名或标语被认为是噪音,它们将被删除。 – meagar