以下代碼有什麼問題?關於符號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謝謝。解決了我的問題。 –