Complex number when using variables

1 回表示 (過去 30 日間)
user86753
user86753 2020 年 4 月 11 日
コメント済み: user86753 2020 年 4 月 11 日
I am getting two different answers in MATAB 2017a. If I compute it directly in the Command Window vs assigning the variables x,y, I get a complex answer?
-50.8478 ^ -1.017
ans =
-0.0183959070209519
K>> x = -50.8478; y = -1.017
K>> x^y
ans =
-0.0183696777893977 + 0.000982004601423454i
  1 件のコメント
user86753
user86753 2020 年 4 月 11 日
Thanks!

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2020 年 4 月 11 日
That's correct. The unary minus operator (before 50.8478) is at precedence level 4. The matrix power operator with unary minus (between 50.8478 and 1.017) is at precedence level 3. So:
-50.8478 ^ -1.017
is equivalent to:
-(50.8478^-1.017)
The expression inside the parentheses is real.
When you use variables:
x = -50.8478;
y = -1.017
x^y
that is equivalent to:
(-50.8478)^(-1.017)
which gives a complex result.

その他の回答 (1 件)

James Tursa
James Tursa 2020 年 4 月 11 日
編集済み: James Tursa 2020 年 4 月 11 日
Operator precedence
>> -50.8478 ^ -1.017
ans =
-0.0184
>> (-50.8478) ^ -1.017
ans =
-0.0184 + 0.0010i
The ^ operator has higher precedence than the - operator when typed in at the command line.

カテゴリ

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

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by