Finding anti-derivatives is more difficult than finding derivatives, for computers as well as humans. Some functions simply have no anti-derivative with a formula in terms of standard functions. When such anti-derivatives do exist, Sage can usually find them.
To compute definite integrals, we can either find an antiderivative, substitute endpoints, and subtract, as usual, or we can ask Sage to do it all with a single command. To do $\ds\int_0^3 \cos(x)\sin(x)+x^2$, for example, here are both methods.
Sometimes Sage may give an answer in a form different than you might get when you find an anti-derivative by hand. We know that $$\int \sec^3 x\,dx={\sec x\tan x\over 2}+{\ln|\sec x+\tan x|\over 2} +C.$$ Let's see what Sage gives.
This doesn't look much like the "standard'' anti-derivative, though there are some similarities. We can easily check whether both are correct, since we can take the derivative of each answer. Perhaps the easiest way to do this is to ask Sage to take the derivative of the difference of the two; we should get zero.
Well, that doesn't look like zero, but maybe if we simplify it.
Still not zero. One more thing we can try:
The `simplify_full
' function clearly does something different, and
it's always worth trying if you think something might have a simpler
form. Note that the syntax of `simplify_full
' is different than
what we have seen for other operations: instead of
`simplify_full(D(x))
' we must type `D(x).simplify_full()
'.
So the bottom line is that both anti-derivatives were correct.
Sage can also do improper integrals, as described in
section 9.7. There we saw that
$\ds \int_a^\infty {1\over x^2}\,dx={1\over a}$. Here it is in Sage.
There is one new wrinkle: $1/x^2$ is undefined at 0, so we need $a>0$
for this to make sense. If you remove the line `assume(a>0)
'
below, you will get a rather unpleasant error message, but if you look
carefully you will see "Is _SAGE_VAR_a
positive, negative or zero?''
in the error message, which gives a clue.
Of course, we could also compute $\ds\int_a^b {1\over x^2}\,dx$ and then take the limit as $b$ goes to infinity, which is what we would do by hand. Sage can also compute (some) limits, although in this case the limit is very easy to do by hand.
In section 8.6 we discussed some simple methods for finding approximations of definite integrals when we can't find an exact answer. Sage knows more powerful methods for approximating integrals; here's how to use one of them.
The first number in the answer is the approximated value, the second is an error bound indicating how accurate the answer is.