ans = 
Complex multiplication giving incorrect result
古いコメントを表示
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?
採用された回答
その他の回答 (2 件)
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)
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.
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'))
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!