Why do I get "complex" arrays?

16 ビュー (過去 30 日間)
Jamie Al
Jamie Al 2021 年 5 月 11 日
コメント済み: Jamie Al 2021 年 5 月 11 日
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"

採用された回答

Steven Lord
Steven Lord 2021 年 5 月 11 日
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 件のコメント
Steven Lord
Steven Lord 2021 年 5 月 11 日
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 2021 年 5 月 11 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by