2017-11-11 205 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