Complex multiplication giving incorrect result

5 ビュー (過去 30 日間)
jayakd
jayakd 2025 年 1 月 16 日
編集済み: Catalytic 2025 年 1 月 17 日
I have a vector A of complex numbers but the product of the numbers should result in real value (there complex conjugate values that make sure the product is real). But when I run prod(A) I get an incorrect result.
A = 1.0e+08 *[-1.1051 + 0.0000i -0.4594 + 1.8182i -0.4594 - 1.8182i -0.2933 + 2.8161i -0.2933 - 2.8161i];
prod(A) = -3.1156e+41 - 9.6714e+24i
The correct value should be -3.1156e+41. why is MATLAB giving incorrect result here?

採用された回答

Walter Roberson
Walter Roberson 2025 年 1 月 16 日
編集済み: Walter Roberson 2025 年 1 月 16 日
For the given A, the prod() is completely real.
A = 1.0e+08 *[-1.1051 + 0.0000i, -0.4594 + 1.8182i, -0.4594 - 1.8182i, -0.2933 + 2.8161i, -0.2933 - 2.8161i];
prod(A)
ans = -3.1156e+41
prod(sym(A))
ans = 
double(ans)
ans = -3.1156e+41
prod(sym(A, 'f'))
ans = 
double(ans)
ans = -3.1156e+41
The results would probably be different if the given A is the format short approximation of the actual values stored in A. There is a difference between how values are stored and how they are displayed, and that difference can be quite important.

その他の回答 (2 件)

John D'Errico
John D'Errico 2025 年 1 月 16 日
編集済み: John D'Errico 2025 年 1 月 16 日
Yet another person who does not understand the realities of double precision arithmetic.
Note that the imaginary part is roughly 1e-17 as large as the real part. ANY noise in the least significant bits of the computation will be down at that level. The imaginary part is effectively zero, to the extent that it can be computed.
eps(-3.1156e+41)
ans = 3.8686e+25
And while 9.6714e+24i may seem large to you, it is effectively indistinguishable form zero, just floating point trash in this computation.
Welcome to the wonderfully wacky world of floating point arithmetic, where you need to learn about tolerances, how to apply them, how to use them, and how to recognize that you need to consider them.

Catalytic
Catalytic 2025 年 1 月 17 日
編集済み: Catalytic 2025 年 1 月 17 日
You can also pre-sort -
A = 1.0e+08 *[-1.1051 + 0.0000i , -0.4594 + 1.8182i, -0.4594 - 1.8182i, -0.2933 + 2.8161i , -0.2933 - 2.8161i];
prod(sort(A,'ComparisonMethod','real'))
ans = -3.1156e+41

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by