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):
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:
But the imaginary part of the analytic integral vs the numerical integral have opposite signs:
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:
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.