# Integrate() Error

For: SageMath version 9.0, Release Date: 2020-01-01 When integrating this:

t = var('t'); f(t) = (sin(2*t)*sin(t))/(cos(t)+3)
a_2 = integrate(f, t, 0, pi)*2/(2*pi)
a_2


Output:

-17


I get -17 ?!

If I just replace sin(2t) with its identity 2sin(t)*cos(t), and integrate again:

t = var('t'); f(t) = (2*sin(t)*cos(t)*sin(t))/(cos(t)+3)
a_2 = integrate(f, t, 0, pi)*2/(2*pi)
a_2


Output:

   -(470832*sqrt(2) - 665857)/(13860*sqrt(2) - 19601)


This last value is the correct one. Simplifying this expression (which I don't know how to do in Sage), you have:

(12*sqrt(2)-17)


It seems like the first result, wrongly reporting -17, somehow is missing the 12*sqrt(2) part of it.

edit retag close merge delete

Sort by » oldest newest most voted

Sage can call various engines for computing integrals.

The default integration engine Sage calls is Maxima. It gets some integrals wrong, so it's always good to check by computing with several integration engines.

Here is how to tell Sage to ask SymPy, Maxima, Giac, Fricas to compute the integral.

Define the function:

sage: f(t) = (sin(2*t)*sin(t))/(cos(t)+3)
sage: assume(t, 'real')


Integrate (default choice of integration engine):

sage: a_2 = integrate(f(t), t, 0, pi)*2/(2*pi)
sage: a_2
-17

sage: integrate(f, t, 0, pi, algorithm='sympy')
-17*pi + 12*sqrt(2)*pi

sage: integrate(f, t, 0, pi, algorithm='maxima')
-17*pi

sage: integrate(f, t, 0, pi, algorithm='giac')
pi*(12*sqrt(2) - 17)

sage: integrate(f, t, 0, pi, algorithm='fricas')
-17*pi + 12*sqrt(2)*pi


See somewhat longer discussion as part of an answer to a recent question about integration in Sage:

Here is how to get all the results at once as a list:

sage: zz = ['sympy', 'maxima', 'giac', 'fricas']
sage: for z in zz:
....:     print(f"{z}:", integrate(f, t, 0, pi, algorithm=z)/pi)
sympy: -(17*pi - 12*sqrt(2)*pi)/pi
maxima: -17
giac: 12*sqrt(2) - 17
fricas: -(17*pi - 12*sqrt(2)*pi)/pi


(Only include FriCAS if it is installed, otherwise an error will be raised instead of displaying the last result.)

To get the results in a list:

sage: zz = ['sympy', 'maxima', 'giac', 'fricas']
sage: aa = [integrate(f, t, 0, pi, algorithm=z) for z in zz]
sage: aa
[-17*pi + 12*sqrt(2)*pi,
-17*pi,
pi*(12*sqrt(2) - 17),
-17*pi + 12*sqrt(2)*pi]


Example on SageCell:

more

This is done by clicking the "accept" button, the button consisting of a tick mark "✓" icon, located to the left of the top of the answer, below the "upvote" and "downvote" buttons and the answer's score.

This marks the question as solved in the list of questions.

This saves time for people looking for unsolved questions to answer.