Very often I struggle to understand what is happening when I try to integrate stuff in Mathematica. Generally, it deals very well with ugly symbolic integrals. But I have an example in which it deals very badly: it is slow and wrong. I have a semi-numeric calculation; a 9x9 matrix whose elements are all of the form,
$$M_{ij}=(a_{ij}+i b_{ij}) e^{(c_{ij}+i d_{ij})s},$$
with $a_{ij}, b_{ij}, c_{ij}, d_{ij}$ real numbers (numeric values). I want to compute $\int_0^\tau M ds$, which I need symbolically, i.e., $s$ is symbolic and $\tau$ too.
Notably, after performing the integral, I obtain a matrix function of $\tau$ and it must be the zero matrix whenever I set $\tau=0$. However, in my case it doesn't, I found the problem was in some particular elements of the matrix; see below one of them:
(-0.149566 + 0.149181 I) \[Epsilon]^2 + (0.0000637042 +
0.00011343 I) E^((-0.16522 +
0.661222 I) s) \[Epsilon]^2 + (0.0615629 +
0.00935082 I) E^((-0.16522 -
0.161252 I) s) \[Epsilon]^2 + (0.086871 -
0.141669 I) E^((-0.16522 -
0.13798 I) s) \[Epsilon]^2 + (0.00124649 -
0.0179924 I) E^((-0.16522 -
1.65846 I) s) \[Epsilon]^2 + (0.0000790824 +
0.0000814583 I) E^((-0.161286 -
0.860535 I) s) \[Epsilon]^2 - (0.00423634 -
0.00658894 I) E^((-0.161286 -
1.68301 I) s) \[Epsilon]^2 + (0.0139537 -
0.0131108 I) E^((-0.161286 -
1.65974 I) s) \[Epsilon]^2 - (0.0000300511 +
8.17237*10^-8 I) E^((-0.161286 -
3.18022 I) s) \[Epsilon]^2 - (0.000349407 -
0.00152877 I) E^((-0.00393366 +
1.52176 I) s) \[Epsilon]^2 - (0.00959497 -
0.0059272 I) E^((0.00393366 - 1.52176 I) s) \[Epsilon]^2
Using simply
Integrate[%,{s,0,\tau}]/.\tau->0
I get as a result
(-0.531902 + 0.461049 I) \[Epsilon]^2
instead of 0. I find it a bit frustrating that the computation is also slow, and we are only dealing with a bunch of exponentials.
I have found a workaround that I usually keep in mind when I deal with Integrate. It consists in finding the primitive and then evaluating the boundaries concerned
integrate[f_,s_,xi_,xf_]:=Module[{Ii,If, adv},
adv = Assuming[assumptions,Integrate[f,s]];
Ii =adv/.s->xi;
If =adv/.s->xf;
Return[If-Ii]
]
Using the above, the sanity check $\tau \to0$ gives zero for the integral as expected.
I really wanted to understand what is "wrong" with Integrate[], how and why I should use it. It feels really weird that such a nice function doesn't recognize simple symbolic integrals and cannot decide the correct/most efficient way of integrating automatically. What am my missing here?
Integrate[%,{s,0, tau }]/. tau->0
to $(\text{3.469446951953614$\grave{ }$*${}^{\wedge}$-16}-\text{1.1102230246251565$\grave{ }$*${}^{\wedge}$-16} i) \epsilon ^2$ $\endgroup$Rationalize[(-0.149566 + 0.149181 I) \[Epsilon]^2 + (0.0000637042 + 0.00011343 I) E^((-0.16522 + 0.661222 I) s) \[Epsilon]^2 +...- 3.18022 I) s) \[Epsilon]^2 - (0.000349407 - 0.00152877 I) E^((-0.00393366 + 1.52176 I) s) \[Epsilon]^2 - (0.00959497 - 0.0059272 I) E^((0.00393366 - 1.52176 I) s) \[Epsilon]^2,0]
? $\endgroup$Integrate
is exact solver. Same asDSolve
and so on. This generally produces more accurate results as the internal algorithms are designed to work with exact values. For non-exact, it might be forced to use numerical algorithms for example. Your input was no exact. Converting it to exact gives the exact zero. You could also have doneChop
at the end. But better always to start with exact input. $\endgroup$