0
$\begingroup$

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?

$\endgroup$
7
  • 1
    $\begingroup$ Mathematica v12.2 evaluates Integrate[%,{s,0, tau }]/. tau->0 to $(\text{3.469446951953614$\grave{ }$*${}^{\wedge}$-16}-\text{1.1102230246251565$\grave{ }$*${}^{\wedge}$-16} i) \epsilon ^2$ $\endgroup$ May 18 at 8:40
  • $\begingroup$ Did you try 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$
    – user64494
    May 18 at 8:43
  • $\begingroup$ @UlrichNeumann Right, not a bad zero. I run 13 in a Mac ARM processor btw. $\endgroup$
    – Marcelo
    May 18 at 8:44
  • $\begingroup$ @user64494 doesn't work =/ $\endgroup$
    – Marcelo
    May 18 at 8:47
  • 4
    $\begingroup$ The rule of thumb in all CAS systems is this: Better to use exact numbers with exact solvers. Integrate is exact solver. Same as DSolve 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 done Chop at the end. But better always to start with exact input. $\endgroup$
    – Nasser
    May 18 at 9:45

2 Answers 2

1
$\begingroup$

To investigate what's happening during the integration try using EvaluatioMonitor or StepMonitor.

$\endgroup$
1
  • 1
    $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    May 19 at 9:26
1
$\begingroup$

In version 13 on Windows 10

f= Rationalize[(-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, 0];
Integrate[f, {s, 0, \[Tau]}]) /. \[Tau] -> 0

0

$\endgroup$
2
  • $\begingroup$ Now it worked for me. Yet, rationalizing doesn't seem to change the (output) integrand. Do you have an idea why this works? This post more about trying to understand the problem than to solve it, so it'd really be meaningful to add some comment about it in your answer. $\endgroup$
    – Marcelo
    May 18 at 9:41
  • $\begingroup$ See the Nasser's comment to your question to this end. $\endgroup$
    – user64494
    May 18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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