次のコードで何が問題になっていますか?シンボリック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ありがとうございます。私の問題を解決しました。 –