Complex multiplication giving incorrect result

5 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Walter Roberson
Walter Roberson il 16 Gen 2025
Modificato: Walter Roberson il 16 Gen 2025
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.

Più risposte (2)

John D'Errico
John D'Errico il 16 Gen 2025
Modificato: John D'Errico il 16 Gen 2025
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 il 17 Gen 2025
Modificato: Catalytic il 17 Gen 2025
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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by