Why does Matlab generate smal complex numbers?

26 visualizzazioni (ultimi 30 giorni)
Hi all,
why do i get very small complex numbers like this 2.339787366554155e+02 + 0.000000000000000e+00i ?
NOTE: There is no sqrt for negative numbers
  1 Commento
Walter Roberson
Walter Roberson il 1 Mar 2020
Sorry not enough information about what you are doing.
That line has a complex component of exactly 0.

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 1 Mar 2020
Modificato: John D'Errico il 1 Mar 2020
In the example you show, you are probably taking ONE element from a vector or array of numbers, an ensemble that contains at least ONE element that is complex. For example...
A = rand(5,1);
A (2) = A(2) + sqrt(-1)
A =
0.276025076998578 + 0i
0.679702676853675 + 1i
0.655098003973841 + 0i
0.162611735194631 + 0i
0.118997681558377 + 0i
As you can see, MATLAB seems to indicate that there are 5 COMPLEX numbers, even though we know that only one was truly complex. Depending on the display format employed, you may find those zero imaginary terms being displayed as 0.000000000000000e+00i .
Why do SOME of the elements in your array or vector contain complex results? That is difficult to know, however, a complex number is not generated only when you have a sqrt. In fact, it is trivially easy to find other expressions that yield a complex result.
asin(1.1)
ans =
1.5707963267949 - 0.443568254385115i
log(-2)
ans =
0.693147180559945 + 3.14159265358979i
So just because you lack a sqrt in your code, don't immediately decide that you could NEVER have a complex result of any sort. That might be a hasty conclusion, and probably a false one, since somewhere you probably did generate something with an imaginary part in it. What you did? That we cannot know, since it is difficult to see inside your computer.
  1 Commento
Walter Roberson
Walter Roberson il 1 Mar 2020
A.^B will turn out complex if A is negative and B is not an integer.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by