raising negative number to a power

16 ビュー (過去 30 日間)
William Perkins
William Perkins 2016 年 6 月 3 日
コメント済み: Steven Lord 2024 年 10 月 18 日
>> c=-1.5^2
c=-2.2500
>> c=-1.5*-1.5
c=2.2500
The latter result is correct. The former is incorrect. How to raise negative numbers to a power that is internally consistent and correct in Matlab.

採用された回答

Callum Clarke
Callum Clarke 2016 年 6 月 3 日
Just put your negative values in brackets:
(-1.5)^2 = 2.2500
In your case Matlab is applying the sign afterwards, as it sees it as a standard operator and is following the usual operation order by calculating the power before subtraction.
  4 件のコメント
Jason
Jason 2024 年 10 月 18 日
Hello,
I typed a rather simple expression: (-8)^(1/3)
but the answer I got was 1.0000 + 1.7321i, instead of a nice -2. Any ideas...?
Thank you, Jason
Steven Lord
Steven Lord 2024 年 10 月 18 日
There are three values that when raised to the third power give -8. [Yes, the real parts of y are not all exactly -8 and y has a non-zero imaginary part. Such is floating point arithmetic.]
format longg
x = roots([1 0 0 8]) % roots of x^3+8 = 0 or x^3 = -8
x =
-2 + 0i 1 + 1.73205080756888i 1 - 1.73205080756888i
y = x.^3
y =
-8 + 0i -7.99999999999999 + 4.44089209850063e-16i -7.99999999999999 - 4.44089209850063e-16i
Alternately if you know that you want the real root, use nthroot.
z1 = nthroot(-8, 3)
z1 =
-2
w1 = z1.^3
w1 =
-8

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeExponents and Logarithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by