Complex multiplication giving incorrect result

조회 수: 2 (최근 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 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