3
$\begingroup$

I am interested in getting a completely-symbolic form of an integral of an expression. But I noticed that when I compare my symbolic integral with a numerically calculated integral, I see that there is a substantial difference.

When I integrate my expression, I get a very clean result, but with a very complicated Condition attached to it, which looks like this (code provided at end):

image of expression

I am interpreting that this integral "blows up" for some very complicated situation, but otherwise has a single solution. Now if I grab that solution and plot the real and imaginary parts as a function of the variable $\Delta_p$, I observe that the analytical solution disagrees with its numerical counterpart.

The plots of the real part of the analytic integral vs the numerical integral seems to be pretty close: real parts of analytical vs numerical integrals

But the imaginary part of the analytic integral vs the numerical integral have opposite signs:

imaginary parts of analytical vs numerical integrals

The imaginary part is flipped! Any ideas what is going on? Why is this issue happening?

Here is my code for obtaining these results:

expr = 1/W Sqrt[Log[2]/π] 1/(
   1 + (Δ/
     W)^2) (I (-4 (Δ + Δc1)^2 + 
        4 (Δ + Δc1) (Δ + Δc2) + 
        2 γ (Γ + 
           2 I (-Δ + Δc1 - Δc2 - Δp)) + 
        2 I Γ (Δc1 - Δp) + 
        8 (Δ + Δc1) (Δ + Δp) - 
        4 (Δ + Δc2) (Δ + Δp) - 
        4 (Δ + Δp)^2 + Ωc2^2))/(2 γ (Γ + 
         2 I (-Δ + Δc1 - Δc2 - Δp)) (Γ - 
         2 I (Δ + Δp)) + 
      I (2 Γ^2 (Δc1 - Δp) + 
         8 (Δ + Δc1)^2 (Δ + Δp) + 
         8 (Δ + Δc2) (Δ + Δp)^2 + 8 (Δ + Δp)^3 - 
         2 (Δ + Δc2) Ωc1^2 - 2 (Δ + Δp) Ωc1^2 + 
         2 (Δ + Δc1) (-4 (Δ + Δc2) (Δ + Δp) - 
            8 (Δ + Δp)^2 + Ωc1^2) - 
         2 (Δ + Δp) Ωc2^2 + 
         I Γ (4 (Δ + Δc1)^2 + 4 (Δ + Δc2) (Δ + Δp) + 8 (Δ + Δp)^2 - 
            4 (Δ + Δc1) (Δ + Δc2 + 
               3 (Δ + Δp)) - Ωc1^2 - Ωc2^2)));

parameterRules =  {Ωc1 -> 4, Ωc2 -> .1, Γ -> 1,  
                   Δc1 -> 0, Δs -> 0, Δc2 -> 0, 
                   z -> 1, γ -> 0, ϕ -> 0, W -> 10};

(*Analytic Integration:*)
DL4lvldopplerPtoPΔ = Integrate[ expr, {Δ, -∞, ∞}] // Normal // Simplify;
analyticalSol = DL4lvldopplerPtoPΔ /. parameterRules;


(*Numeric Integration:*)
numericallyIntegraled = expr /. parameterRules // Simplify;
f[Δp_?NumericQ] := NIntegrate[numericallyIntegraled, {Δ, -∞, ∞}];


(*Plotting real and imag parts of (Analytic and Numeric):*)
Plot[{Re[ComplexExpand[f[Δp]]], 
      Re[ComplexExpand[analyticalSol]]}, {Δp, -10, 10}, 
 Frame -> True, 
 FrameLabel -> {{None, None}, {"Δp", 
    "Im[expr]: Numeric Vs Analytic"}}, GridLines -> Automatic, 
 GridLinesStyle -> LightGray, BaseStyle -> 12
]
Plot[{Im[ComplexExpand[f[Δp]]], 
      Im[ComplexExpand[analyticalSol]]}, {Δp, -10, 10}, 
 Frame -> True, 
 FrameLabel -> {{None, None}, {"Δp", 
    "Re[expr]: Numeric Vs Analytic"}}, GridLines -> Automatic, 
 GridLinesStyle -> LightGray, BaseStyle -> 12, PlotRange -> All
]

One thing to note is that I tweaked the conditional expression to just be a normal expression. If I don't do this, I cannot obtain a plot anymore, and if I try to look at my analytic expression, I get the form:

Cell[CellGroupData[{Cell[BoxData[
 RowBox[{"Simplify", "[", 
  RowBox[{
   RowBox[{"Re", "[", 
    RowBox[{"ComplexExpand", "[", "analyticalSol", "]"}], "]"}], ",", 
   " ", 
   RowBox[{"Element", "[", 
    RowBox[{"Δp", ",", " ", "Reals"}], "]"}]}], 
  "]"}]], "Input",
 CellChangeTimes->{{3.799702119453383*^9, 3.799702156106647*^9}, {
  3.7997022143955765`*^9, 3.7997022256841283`*^9}},
 CellLabel->"In[97]:="],

Cell[BoxData["Undefined"], "Output",
 CellChangeTimes->{
  3.7997020940830355`*^9, 3.799702157040344*^9, {
   3.7997022211933966`*^9, 3.799702226293498*^9}},
 CellLabel->"Out[97]="]
}, Open  ]]

Any help would be greatly appreciated!


EDIT: Thanks to the user @MarcoB, I've made a correction to this question. My primary interest is getting a correct, fully symbolic solution for this integral. The numerics I'm performing is really just a check for the analytic integration was performed accurately. So while I know that I can fix the plot (by plugging in the parameters used for the plot before performing the analytic integration), it really prevents me from obtaining my main goal: an accurate, fully-symbolic integration of the expression.

EDIT2: @CATrevillian suggested that the analytic integration is correct - but by dropping the Conditional Form of the expression I'm getting an incorrect result. Here's an example I accidentally found that directly plots this Conditional Expression (without doing the suspicious step of removing the conditional statement). In this example there is still a disagreement with the numerical solution, but now a slightly different disagreement. Here is the code:

expr = 1/W Sqrt[Log[2]/\[Pi]] 1/(
     1 + (Δ/W)^2) (I (-4 (Δ + Δc1)^2 + 
          4 (Δ + Δc1) (Δ + Δc2) + 
          2 \[Gamma] (Γ + 
             2 I (-Δ + Δc1 - Δc2 - Δp)) + 
          2 I Γ (Δc1 - Δp) + 8 (Δ + Δc1) (Δ + Δp) - 
          4 (Δ + Δc2) (Δ + Δp) - 
          4 (Δ + Δp)^2 + Ωc2^2))/(2 \[Gamma] (Γ + 
           2 I (-Δ + Δc1 - Δc2 - Δp)) (Γ - 
           2 I (Δ + Δp)) + 
        I (2 Γ^2 (Δc1 - Δp) + 8 (Δ + Δc1)^2 (Δ + Δp) + 
           8 (Δ + Δc2) (Δ + Δp)^2 + 8 (Δ + Δp)^3 - 
           2 (Δ + Δc2) Ωc1^2 - 
           2 (Δ + Δp) Ωc1^2 + 2 (Δ + Δc1) (-4 (Δ + Δc2) (Δ + Δp) - 
              8 (Δ + Δp)^2 + Ωc1^2) - 
           2 (Δ + Δp) Ωc2^2 + I Γ (4 (Δ + Δc1)^2 + 
              4 (Δ + Δc2) (Δ + Δp) + 8 (Δ + Δp)^2 - 
              4 (Δ + Δc1) (Δ + Δc2 + 3 (Δ + Δp)) - Ωc1^2 - Ωc2^2))) /. { Δc1 -> 0, Δs -> 0, Δc2 -> 0, γ -> 0, Γ -> 1}; // Simplify

parameterRules =  {Ωc1 -> 4, Ωc2 -> .1, Γ -> 1,  Δc1 -> 0, Δs -> 
    0, Δc2 -> 0, z -> 1, γ -> 0, ϕ -> 0, W -> 10};

(*Analytic Integration:*)
DL4lvldopplerPtoPΔ = 
  Integrate[ expr, {Δ, -∞, ∞}, 
    Assumptions -> {Δp ∈ Reals, Ωc1 ∈ Reals, Ωc2 ∈ Reals, 
       Γ ∈ Reals,  Δc1 ∈ Reals, Δs ∈ Reals, Δc2 ∈ Reals, 
      z ∈ Reals, γ ∈ Reals, ϕ ∈ Reals, W ∈ Reals}] // Simplify;
analyticalSol = DL4lvldopplerPtoPΔ /. parameterRules;

(*Numeric Integration:*)
numericallyIntegraled = expr /. parameterRules // Simplify;
f[Δp_?NumericQ] := NIntegrate[numericallyIntegraled, {Δ, -∞, ∞}];


(*Plotting real and imag parts of (Analytic and Numeric):*)
Plot[{Re[ComplexExpand[f[Δp]]], 
      Re[ComplexExpand[analyticalSol]]}, {Δp, -10, 10}, 
 Frame -> True, 
 FrameLabel -> {{None, None}, 
                {"Δp", "Re[expr]: Numeric Vs Analytic"}}, 
 GridLines -> Automatic, 
 GridLinesStyle -> LightGray, BaseStyle -> 12
]

Plot[{Im[ComplexExpand[f[Δp]]], 
      Im[ComplexExpand[analyticalSol]]}, {Δp, -10, 10}, 
 Frame -> True, 
 FrameLabel -> {{None, None}, 
                {"Δp", "Im[expr]: Numeric Vs Analytic"}}, 
 GridLines -> Automatic, 
 GridLinesStyle -> LightGray, BaseStyle -> 12, PlotRange -> All
]

And it looks like this:

imaginary parts of the analytical vs. numeric integrals of the new expression

Which now is in agreement with the numerics for half of the domain of $\Delta_p$. This suggests to me that something is going wrong in the symbolic integration.

$\endgroup$
9
  • $\begingroup$ You tweaked the results from the fully symbolic integrate? How can you be sure that this is not where the errors begin to occur? $\endgroup$ May 29, 2020 at 2:52
  • $\begingroup$ I answered that at the bottom. If you leave it as a conditional expression, it still cannot find the solution and thinks its "undefined." (Even though the numeric solution can easily solve it). $\endgroup$ May 29, 2020 at 2:58
  • $\begingroup$ I don’t follow. You obtain a fully symbolic integration. You show this, it is a conditional, yes, but otherwise fully symbolic. If you expect an exact form, how do you have confidence you can find this? Then you tweak it, why—for what reason? And how do you know that the tweaks you employ do not cause the discrepancies you see? $\endgroup$ May 29, 2020 at 3:01
  • 1
    $\begingroup$ @CATrevillian Thanks a lot for your help. This code was run on Mathematica 11.3. $\endgroup$ May 29, 2020 at 5:29
  • 1
    $\begingroup$ @CATrevillian, Okay so if I'm understanding correctly, you think that the fact that I can't plot this solution (without forcing it to be in a "normal form") is because Mathematica can't find a closed-form-solution for the set of inputs in the parameter space I'm interested in? $\endgroup$ May 29, 2020 at 5:47

1 Answer 1

1
$\begingroup$

Since you end up substituting numerical values, I recommend doing that earlier rather than later. This should greatly simplify the complex task you set for Integrate and is likely to lead to better results. You also evaluate the integral for real values of Δp, so it would make sense to make Integrate aware of that possible simplification as well, through Assumptions:

analytical = Integrate[
    expr /. parameterRules,
    {Δ, -Infinity, Infinity},
    Assumptions -> Δp ∈ Reals
  ]

(* Out: 
-(((-1 + 200*Δp*(21*I + 2*Δp))*Sqrt[Pi*Log[2]]) /
      ((21*I + 2*Δp)*(-1601 + 200*Δp*(21*I + 2*Δp))))
*)

analytical output of Integrate

The numerical version is:

ClearAll[numerical]
numerical[dp_?NumericQ] :=
  NIntegrate[
    expr /. parameterRules /. Δp -> dp,
    {Δ, -Infinity, Infinity}
  ]

Checking with a random value shows that it works: numerical[4] returns -0.0185615 + 0.0649662 I.

The following plots show that the real and imaginary parts obtained through these methods are identical:

Plot[
   Evaluate@#[{analytical, numerical[Δp]}], {Δp, -10, 10},
   PlotStyle -> {
     Directive[Thickness[0.03], GrayLevel[0.6]],
     Directive[Thick, Darker@Orange]},
   PlotLegends -> {"analytical", "numerical"}, ImageSize -> Medium,
   Epilog -> Inset[Style[ToString@#, 18, Black], Scaled[{0.9, 0.9}]]
] & /@ {Re, Im}

comparison of Real and Imaginary parts

You can also plot the differences between the analytical and numerical expressions and you will see that they are identical, within the numerical error to be expected with machine-precision evaluation.

$\endgroup$
5
  • $\begingroup$ Sorry! I don't think I was clear! The analytical expression (without the particular parameters) IS very important to me. I need the analytical expression in its full form. (It just so happens that I can verify that something is wrong with that analytical expression when I look at this plot!) I'll edit the question to make that more clear for everyone. $\endgroup$ May 29, 2020 at 1:55
  • $\begingroup$ I tried cleaning up the language to use the phrase "completely-symbolic expression" instead of "analytic expression", and added a note at the end explaining that I can't plug in the parameters at an earlier point without losing the goal of getting a "completely-symbolic integral", and that the purpose of the numeric integration is simply to check if the analytic integral is accurate. $\endgroup$ May 29, 2020 at 2:07
  • $\begingroup$ Also, I tried seeing if using "Assumptions -> Δp ∈ Reals" in the full integral would fix the issue and it didn't, unfortunately. You can assume that all the variables are real, and that all values except Δp, Δs, Δc1, and Δc2 are positive. If you could take a look at the fully-symbolic integral it would be greatly appreciated! $\endgroup$ May 29, 2020 at 2:29
  • $\begingroup$ @StevenSagona I think, perhaps, you may be making the problem more difficult than it needs to be. The solution provided by MarcoB shows that you have the correct form. Why are you concerned about the non-dependent parameters? This shows the numeric result is accurate, also. You might find NIntegrate will run much faster than Integrate and could find/plot trends much faster that way. $\endgroup$ May 29, 2020 at 2:50
  • $\begingroup$ @CATrevillian, my goal is a correct, fully-symbolic integral. The purpose of using NIntegrate is simply to check if this symbollic integral is correct. I believe it's very standard to do numerical simulations to "check" if an analytic solution is correct, and if I simply wanted a numerical solution I would use Matlab to do it. $\endgroup$ May 29, 2020 at 2:55

Your Answer

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

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