MATLAB Answers

Why do I get "complex" arrays?

1 view (last 30 days)
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"

Accepted Answer

Steven Lord
Steven Lord on 11 May 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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!