0
$\begingroup$

I have a function Y[x_,β2_], It can be plotted normally. But when I take specific values, I get something like +0.000000*I when x is equal to some value, but it should't be able to Plot the function if it has complex numbers when I don't take the absolute value.

I've seen some answers that say Chop can be used, but the function Y[x_,β2_] can be drawn normally so it must don't have a imaginary part. The reason I think this imaginary part has a definite value is because the running result of my other code changes when I use Chop[Y[x_,β2_]] in a Piecewise Function. And I think this problem is probably the cause of my other code running wrong. One result of a certain value:

483.282589+0.000000 I

Here is the code about Y[x_,β2_], for different Q1, Q2 has different Y[x_,β2_], just one of which is listed below. When I use Chop or not Chop it Plot the same result, but when I run other functions with Y[x_,β2_] (as discussed below), adding Chop gets a completely different result. :

Clear["Global`*"]
Print["Q1 = ", DecimalForm[q1 = 122500. - 517.194735, {20, 6}]];

Print["Q2 = ", DecimalForm[q2 = 114200., {20, 6}]];
Print["A24 = ", 
  DecimalForm[
   A24 = 97.001875 (122500. - 517.194735)/122500., {20, 6}]];
δ = -1.2131226805013586 10^-6;
Axr = -1/9 (q1^2 - q1 q2 + q2^2);
Bxr[x_] := -(q1 - q2)/(4 δ) x^2 + 
   1/54 (q1 + q2) (9 q1 q2 - 2 (q1 + q2)^2);

β[x_, bxr_] := ArcCos[bxr[x]/(-Axr)^(3/2)];
Y[x_, β2_] := 
  2 Sqrt[-Axr] Cos[β2[x, Bxr]/3] - 1/3 (q1 + q2);
Xd0 = 2 (-δ/(q1 - q2))^(1/2) (Bxr[0] + (-Axr)^(3/2))^(1/2);
DecimalForm[Y[A24/2, β], {12, 6}]
Plot[-Y[x, β], {x, -Xd0, Xd0}]

I need to define a piecewise function usinig Y[x_,β2_] and use Array turn that piecewise function into an Array Lensarray, then plot the Lensarray. The Lensarraydrawing code is as follows, It reminds me that Less::nord: tried to use 37344.8-3240.47i for an invalid comparison.. Then I want to use Lensarray to do an iterative calculation. And then the iteration went wrong too. when I iterate with the matrix LensArray, it gets an error saying Is not a non-empty list, nor is it a rectangular array of numeric quantities, and when I check the stack trace , I find that it's due to the piecewise function. It shows that the array contains piecewise functions. So I think there's something wrong with 'Lensarray' or 'Lens'. But I check the matrix Lensarray, but it's really made up of numbers, there's no piecewise function. So why did this happen?

δ = 1.2131226805013586 10^-6;
β = 4.75349804 10^-8;
χ = -2 δ + 2 I β;
δ1 = -1.2131226805013586 10^-6;
δ2 = (1.2131226805013586 10^-6)/(1 - 1.2131226805013586 10^-6);
f = 5151736.70155;
LensLength = Yrange[[62]];

Table[Subscript[Axr, 
   i] = ((-1)/9) (q1[[i]]^2 - q1[[i]] q2[[i]] + q2[[i]]^2), {i, 1, 
   62}];
Table[Subscript[Bxr, i] = 
   Module[{δs}, δs = 
     If[OddQ[i], δ1, δ2]; ((-(q1[[i]] - 
            q2[[i]]))/(4δs)) x^2 + (1/54) (q1[[i]] + 
        q2[[i]]) (9 q1 [[i]] q2[[i]] - 2 (q1[[i]] + q2[[i]])^2)], {i, 
   1, 62}];
Table[Subscript[β, i] = 
   ArcCos[Subscript[Bxr, i]/(-Subscript[Axr, i])^((3/2))], {i, 1, 62}];
Table[Subscript[Y, i] = 
   Abs[-(2 Sqrt[-Subscript[Axr, i]] Cos[
         Subscript[β, i]/3] - (1/3) (q1[[i]] + q2[[i]]))], {i, 
   1, 62}];
Len1 = {χ, 
   0 <= y < (Sqrt[x^2 + f^2] - f)/δ + 10 && 
    0 < y < Yrange[[1]] && -50 <= x <= 50};

Lens[x_, y_] = 
  Piecewise[
   Join[{Len1}, 
    Table[{χ, 
      Subscript[Y, i] + Apcie[[i]] <= y < 
        Subscript[Y, i + 1] + Apcie[[i + 1]] && 
       Yrange[[i]] <= y < Yrange[[i + 1]] && -50 <= x <= 50}, {i, 1, 
      61, 2}]]];

Lensarray = Array[Lens, {Nx, Nz}, {{xStart, xEnd}, {0, Maxz}}];
LenF = Lensarray;
MatrixPlot[Abs[Lensarray], ColorFunction -> "Monochrome", 
 PlotLegends -> Automatic]
AbsoluteTime[] - time

Here is the definition of the matrix used in the function:

q1={5151536.701550,2499990.000000,1614774.131776,1169990.000000,904742.171199,729990.000000,605711.526739,513990.000000,443182.277830,387990.000000,343258.780795,306590.000000,275934.940760,249990.000000,227506.115311,208190.000000,191077.508363,176290.000000,162848.416323,151190.000000,140369.167291,130990.000000,121982.805265,114190.000000,106694.176102,100290.000000,93923.837408,88490.000000,82991.123566,78390.000000,73613.061532,69670.000000,65476.568200,62140.000000,58472.599719,55590.000000,52312.925767,49820.000000,46892.253210,44720.000000,42084.946419,40180.000000,37775.450513,36102.000000,33901.020142,32420.000000,30386.185769,29060.000000,27166.524869,25970.000000,24198.228993,23120.000000,21453.974356,20470.000000,18886.440141,17985.000000,16481.195581,15650.000000,14213.422727,13445.000000,12071.827247,11355.000000};

q2={2500000.00,1615000.00,1170000.00,905000.00,730000.00,606000.00,514000.00,443500.00,388000.00,343600.00,306600.00,276300.00,250000.00,227900.00,208200.00,191500.00,176300.00,163300.00,151200.00,140850.00,131000.00,122500.00,114200.00,107240.00,100300.00,94500.00,88500.00,83600.00,78400.00,74250.00,69680.00,66140.00,62150.00,59150.00,55600.00,53010.00,49830.00,47600.00,44730.00,42800.00,40190.00,38500.00,36112.00,34630.00,32430.00,31120.00,29070.00,27905.00,25980.00,24940.00,23130.00,22198.00,20480.00,19637.00,17995.00,17234.00,15660.00,14970.00,13455.00,12830.00,11365.00,10798.00};


Apcie={422.115008,432.115008,900.361323,910.361323,1440.287177,1450.287177,2041.061643,2051.061643,2697.077993,2707.077993,3403.093596,3413.093596,4158.904889,4168.904889,4972.510513,4982.510513,5841.823636,5851.823636,6768.969999,6778.969999,7754.012562,7764.012562,8813.501181,8823.501181,9926.509448,9936.509448,11105.652528,11115.652528,12346.394885,12356.394885,13645.086416,13655.086416,14984.837936,14994.837936,16358.648861,16368.648861,17766.072000,17776.072000,19196.022557,19206.022557,20642.637485,20652.637485,22102.984088,22112.984088,23571.045417,23581.045417,25050.097410,25060.097410,26540.372202,26550.372202,28034.705468,28044.705468,29536.538260,29546.538260,31048.730592,31058.730592,32567.164839,32577.164839,34090.612687,34100.612687,35618.226849,35628.226849};

Yrange={210.000000,657.983232,657.983232,1168.190124,1168.190124,1738.760438,1738.760438,2368.783813,2368.783813,3048.297198,3048.297198,3778.152836,3778.152836,4562.789578,4562.789578,5405.002150,5405.002150,6303.407313,6303.407313,7259.802708,7259.802708,8281.207297,8281.207297,9369.325079,9369.325079,10512.672040,10512.672040,11724.528962,11724.528962,12993.333353,12993.333353,14318.518216,14318.518216,15672.238217,15672.238217,17065.723094,17065.723094,18483.818790,18483.818790,19921.076138,19921.076138,21377.186972,21377.186972,22841.963946,22841.963946,24314.859648,24314.859648,25798.572541,25798.572541,27292.143209,27292.143209,28788.731112,28788.731112,30297.098119,30297.098119,31811.535011,31811.535011,33333.742112,33333.742112,34858.785440,34858.785440,36389.282916};
$\endgroup$
8
  • $\begingroup$ Plot was updated a few years ago to ignore small imaginary parts (as if they were mere round-off errors), so often Chop is no longer necessary. -- The definition of Xd0 has an extraneous ], and I'm not sure if it should be deleted or something else is missing. -- I would ask that a minimal working example not Print out things I don't need to see, and there's very little I need to see (thanks). $\endgroup$
    – Michael E2
    Apr 13, 2021 at 18:34
  • $\begingroup$ @ Michael E2 Sorry I added a superfluous ]. I have corrected it. $\endgroup$
    – shrocat
    Apr 13, 2021 at 18:40
  • $\begingroup$ @Michael E2 If I use Chop my function will change dramatically, but logically it should not add Chop because it's a real number, about` Sqrt[-subscript [Axr, I]]` I have calculated it and It's an array of real numbers . So I really don't understand where the problem is. $\endgroup$
    – shrocat
    Apr 13, 2021 at 18:47
  • $\begingroup$ Thanks. I'm a little unclear what you're asking. The plotting code works the way it should. The function has a zero 0. imaginary part that comes from Cos[β[x, Bxr]/3], which basically computes Cos[ArcCos[t]], with t > 1. The code for Lens is incomplete (Apcie is missing), so I can't see whether it works or not. What exactly is your question? $\endgroup$
    – Michael E2
    Apr 13, 2021 at 18:48
  • $\begingroup$ The Plot does not change with Chop. By "my function," did you mean Lens[] or Y[] or something else? $\endgroup$
    – Michael E2
    Apr 13, 2021 at 18:50

1 Answer 1

0
$\begingroup$

To avoid imaginary artifacts, either Rationalize all input or use SetPrecision so that all calculations are done either exactly or with arbitrary-precision rather than machine precision.

Clear["Global`*"]

q1 = 122500. - 517.194735 // Rationalize[#, 0] &;
q2 = 114200; A24 = 
 97.001875 (122500. - 517.194735)/122500 // Rationalize[#, 0] &;
δ = -1.2131226805013586 10^-6 // Rationalize[#, 0] &;
Axr = -1/9 (q1^2 - q1 q2 + q2^2);

Bxr[x_] := -(q1 - q2)/(4 δ) x^2 + 
   1/54 (q1 + q2) (9 q1 q2 - 2 (q1 + q2)^2);

β[x_, bxr_] := ArcCos[bxr[x]/(-Axr)^(3/2)];

Y[x_, β2_] := 
  2 Sqrt[-Axr] Cos[β2[x, Bxr]/3] - 1/3 (q1 + q2) // Simplify;

Xd0 = 2 (-δ/(q1 - q2))^(1/2) (Bxr[0] + (-Axr)^(3/2))^(1/2) // Simplify;

ex = Y[A24/2, β] // Simplify;

-Y[x, β] is real

Simplify[Im[-Y[#, β]] & /@ Range[-Xd0, Xd0, Xd0/10]]

(* {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} *)

However, if evaluated with machine precision there is an imaginary artifact.

-Y[#, β] & /@ N[Range[-Xd0, Xd0, Xd0/10]]

(* {-13967.8 + 0. I, -11711.1 + 0. I, -9566.98 + 0. I, -7560.71 + 
  0. I, -5721.25 + 0. I, -4080.63 + 0. I, -2672.91 + 0. I, -1532.35 + 
  0. I, -690.738 + 0. I, -174.198 + 0. I, -1.45519*10^-11, -174.198 + 
  0. I, -690.738 + 0. I, -1532.35 + 0. I, -2672.91 + 0. I, -4080.63 + 
  0. I, -5721.25 + 0. I, -7560.71 + 0. I, -9566.98 + 0. I, -11711.1 + 
  0. I, -13967.8 + 0. I} *)

Using arbitrary-precision avoids the artifact

-Y[#, β] & /@ N[Range[-Xd0, Xd0, Xd0/10], 8] // FullSimplify

(* {-13967.839, -11711.129, -9566.975, -7560.713, -5721.254, -4080.6264, \
-2672.9051, -1532.3469, -690.7384, -174.19799, 0, -174.19799, -690.7384, \
-1532.3469, -2672.9051, -4080.6264, -5721.254, -7560.713, -9566.975, \
-11711.129, -13967.839} *)

The rest of your code has undefined terms.

$\endgroup$
1
  • $\begingroup$ Thank you for your reply. I updated my question. In fact, I want to use Y in a piecewise function, and then matrix the piecewise function Y and matrix plot it, but It reminds me that Less::nord: tried to use 37344.8-3240.47i for an invalid comparison. I don't know why it happened but it looks like MatrixPlot the right thing. $\endgroup$
    – shrocat
    Apr 14, 2021 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.