Matlab exponent bug?

9 ビュー (過去 30 日間)
Jonas Reber
Jonas Reber 2011 年 11 月 24 日
編集済み: Bruno Luong 2021 年 3 月 7 日
Hello MatLabers
I have encountered a problem when calculating a non integer exponent/power of a variable.
example:
>> -3.^(1.3)
>> ans = -4.1712
thats exactly what I aim to calculate. However, if I do the exact same thing with a variable - the result gets complex:
>> a = -3
>> a.^(1.3)
>> ans = -2.4518 - 3.3745i
is this an known issue or am I doing somehting wrong? (tested on R2010a, R2011b)
  1 件のコメント
Jonas Reber
Jonas Reber 2011 年 11 月 24 日
see also:
http://www.mathworks.com/support/solutions/en/data/1-15M1N/index.html

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

採用された回答

Daniel Shub
Daniel Shub 2011 年 11 月 24 日
  3 件のコメント
Daniel Shub
Daniel Shub 2011 年 11 月 24 日
and I got the documentation link ...
Jan
Jan 2011 年 11 月 24 日
then you get the credits. +1

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

その他の回答 (2 件)

Jan
Jan 2011 年 11 月 24 日
The POWER operation has a higher precedence than the unary minus. Try this:
-3 .^ (1.3)
(-3) .^ (1.3)
  1 件のコメント
Jonas Reber
Jonas Reber 2011 年 11 月 24 日
Jan&Daniel - thanks for the fast replies
>> (-3).^(1.3)
didn't realize that.

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


Edgar An
Edgar An 2021 年 3 月 7 日
編集済み: Edgar An 2021 年 3 月 7 日
The problem is not about precedence. The problem occurs when we use variables instead of numbers. Just like the person who posted.
-0.685^1.5 gives a correct answer
but a = -0.685, b = 1.5, and then a^b gives wrong answer
i am using R2020b version
  3 件のコメント
Walter Roberson
Walter Roberson 2021 年 3 月 7 日
In MATLAB, a^b is defined to be equivalent to exp(log(a)*b). When a is negative the log is complex with a πι component and if b is not an integer then the exp() of the πι*b is going to be complex.
In practice you can tell from timings that for at least some integer values a^b is not implemented through logs: for example a^2 has timing the same as a*a, but the principle is the same.
Bruno Luong
Bruno Luong 2021 年 3 月 7 日
編集済み: Bruno Luong 2021 年 3 月 7 日
To be precise for z complex (including negarive real)
log(z)
is defined in term of real-argument function log and atan2
log(abs(z)) + 1i*angle(z)
where abs(z) is
sqrt(imag(z)^2+real(z)^2) % the square "x^2" here is interpreted as (x*x)
angle(z) is
atan2(imag(z),real(z))
with all the rule we discuss recently, notably discontinuity when real(z) is negative and numeriral sign of imag(z).
The definition of
exp(z)
has no ambiguity.

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

カテゴリ

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