Mathematica and the Fundamental Theorem of Calculus
January 19, 2008 — Oleksandr Pavlyk, Senior Kernel Developer, Algorithms R&D
Most calculus students might think that if one could compute indefinite integrals, it would always be easy to compute definite ones. After all, they might think, the fundamental theorem of calculus says that one just has to subtract the values of the indefinite integral at the end points to get the definite integral.
So how come inside Mathematica there are thousands of pages of code devoted to working out definite integrals—beyond just subtracting indefinite ones?
The answer, as is often the case, is that in the real world of mathematical computation, things are more complicated than one learns in basic mathematics courses. And to get the correct answer one needs to be considerably more sophisticated.
In a simple case, subtracting indefinite integrals works just fine.
Consider computing the area under a sine curve, which equals
We work out the indefinite integral:
Then we can just subtract its value at each end point, and correctly find a definite integral such as
But consider a more complicated case:
We can verify that this indefinite integral at least formally differentiates correctly:
Now let’s compute the definite integral by subtracting values of our indefinite one:
But this cannot be correct. After all, if we plot the integrand, we can see that it is positive throughout the range 0 to 2π:
Mathematica’s built-in definite integration of course gives exactly the correct answer:
So what went wrong with subtracting the end points? The issue is that the fundamental theorem of calculus isn’t directly applicable here. Because, when you state it fully, the theorem requires that the antiderivative that is going to be subtracted be continuous throughout the interval.
But the antiderivative we have here looks like this:
It has a discontinuity right in the middle of the interval.
So how does Mathematica get its answer? It has to be more careful. Sometimes it works by detecting discontinuities in the antiderivative, and then breaking up the integration region into parts, and carefully taking directional limits at the discontinuity points:
Another thing it can do is to make use of ambiguity in the antiderivative. Every calculus student knows that antiderivatives can contain an arbitrary additive constant. But in fact, there’s more arbitrariness than that: one can add different constants on different parts of the interval.
Often it’s not obvious from the algebraic form that one has added a piecewise constant like this. For example, consider:
Differentiating this shows that it is indeed an antiderivative of the same function as above.
It doesn’t happen to be the antiderivative that Mathematica generates by default. But it is a perfectly valid one. And it turns out to be continuous over the region of integration:
So if one uses it, one can now directly apply the fundamental theorem of calculus and get the correct result:
Even though their algebraic forms look different, one can verify that the antiderivatives differ by a piecewise constant:
So how come Mathematica doesn’t always generate the “better” antiderivative, so that the fundamental theorem of calculus applies directly?
The problem is that there’s actually no way to produce an antiderivative that has this property for all definite integrals one might want to compute. Here’s the formal situation.
The fundamental theorem of calculus states that an antiderivative continuous along a chosen path always exists. It is defined as , where the integration is performed along the path. Its existence is of theoretical importance—though in practice cannot always be expressed in terms of any predetermined set of elementary and special functions.
Moreover, if a meromorphic integrand has simple poles in the complex plane, it is impossible to choose an antiderivative continuous along every imaginable path in the complex plane—because of branch cuts in .
Our integrand has simple poles at :
But now consider how our two antiderivatives behave in the complex plane. Here are the real parts of these functions:
Looking on the real axis, is not continuous, so the fundamental theorem cannot directly be applied. But is continuous, so the fundamental theorem will work.
However, look now at the line from , indicated in the picture by a black line. Along this line, is continuous, so the fundamental theorem will work fine for it. But now is not continuous, so the fundamental theorem will not work:
And indeed one can show that there is no single choice of antiderivative for which the fundamental theorem will always work.
So Mathematica has to go to more effort to get the correct answer for the definite integral.
This may seem subtle—but actually it is just the tip of the iceberg of the issues that crop up in doing definite integration correctly in Mathematica—and in mathematics. It’s the job of our group at Wolfram Research to understand all these issues and figure out good algorithms for handling them. It’s a fascinating exercise not only in algorithm development but also in mathematics itself.