problem with imag command when evaluating symbolic expressions.

1 回表示 (過去 30 日間)
Carlos
Carlos 2013 年 2 月 11 日
Hi, when I execute the following code in Matlab
syms x y; f=4*i+9*x+y; imag(f)
I get the following result
ans =
(9*i*conj(x))/2 - (i*y)/2 - (9*i*x)/2 + (i*conj(y))/2 + 4
and the result should be 4 instead of what I get. I cannot figure out where have I gone wrong.Any ideas?
Thank you in advance.

採用された回答

Walter Roberson
Walter Roberson 2013 年 2 月 11 日
The result is correct. The symbolic toolbox assumes that variables might be complex unless it has been told otherwise. Try
syms x y real

その他の回答 (1 件)

Carlos
Carlos 2013 年 2 月 11 日
The answer is right, if you try subs((9*i*conj(x))/2 - (i*y)/2 - (9*i*x)/2 + (i*conj(y))/2 + 4),{x,y},{0,0}) or subs((9*i*conj(x))/2 - (i*y)/2 - (9*i*x)/2 + (i*conj(y))/2 + 4),{x,y},{1,1}) or you give x and y any value, the answer is always 4. It was just that I found the answer a bit awkward. From your answer I grasp that the only problem here was that I should have declared x and y as symbolic real variables.
Than you very much Walter.

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by