Why do I get "complex" arrays?

17 vues (au cours des 30 derniers jours)
Jamie Al
Jamie Al le 11 Mai 2021
Commenté : Jamie Al le 11 Mai 2021
I am not supposed to get complex values but unfortunatley something is wrong in my eigenvalue caclultions. When plotting I get the error: (which I shouldn't be getting technically)
Warning: Imaginary parts of complex X and/or Y arguments ignored.
The part where the complex value show is in this specific line. However, everything above is a +ve number or value so I don't see how it turns complex:
r=q(1,:); % this is +ve when printed out
u=q(2,:)./r; E=q(3,:)./r;
Bx = q(4,:)./r; By = q(5,:)./r;
p=(gamma-1)*r.*(E-0.5*(u.^2)-(Bx.^2+By.^2)/(2*mu0)); % this is +ve
a=sqrt(gamma*p./r); % this is COMPLEX ???
CA1 = sqrt((Bx.^2+By.^2)./(r*mu0));
CAx1 = sqrt((Bx.^2)./(r*mu0));
a_MHD=sqrt(0.5*(CA1.^2+a.^2+sqrt((CA1.^2+a.^2).^2-4*a.^2.*CAx1.^2)));
I posted the entire code as well, main code is "MUSCLEULER_Test"

Réponse acceptée

Steven Lord
Steven Lord le 11 Mai 2021
If r contains only positive values and p contains only positive values, what about gamma?
areALLElementsOfRPositive = all(r > 0)
areALLElementsOfPNonnegative = all(p >= 0)
areALLElementsOfGammaNonnegative = all(gamma >= 0)
If any elements of r, p, or gamma would result in a complex value in the corresponding element of a, the array a will have the complex attribute.
  4 commentaires
Steven Lord
Steven Lord le 11 Mai 2021
find where the elements of r that are less than or equal to 0 are.
Use breakpoints to determine where those elements in q(1, :) are set to a negative value. You might want to add assert statements to error as soon as any element becomes negative to help you locate the introduction of the negative numbers.
Work your way back to determine why those elements are negative.
Jamie Al
Jamie Al le 11 Mai 2021
Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by