Complex multiplication giving incorrect result

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops 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