Evaluating complex integrals

25 views
Skip to first unread message

Moti

unread,
Apr 3, 2014, 6:11:42 AM4/3/14
to sy...@googlegroups.com
Hi all,

First I must say that SymPy is a great tool, I came across it few weeks ago and...

Yesterday I was assigned a task to compute analytical expressions for some integrals and evaluate them.
Specifically:
1. exp(-0.5*(-x**2/2 + x**4/10)), x goes from -infinity to infinity, or from infinity to a finite value
2. exp(-0.5*(-x**2/2 + x**4/10)) * (-x**2/2 + x**4/10), same range
3. exp(-0.5*(-x**2/2 + x**4/10 - x/20)), same range
4. exp(-0.5*(-x**2/2 + x**4/10 - x/20)) * (-x**2/2 + x**4/10 - x/20), same range

Using mathematica, an analytic expresion for the first two can be found (using Integrate). The latter two are harder and can be obtained only numerically.
Using NIntegrate, the exact values can be obtained (for example in the range (-infinity, -3) or (3, infinity).
Matlab can also compute these numerically, using quadgk (Gauss-Kronrod method supporting infinites).

As for the issues I was facing:
1. First, the integrate function was failing because of the fraction values in the integrand (Coersion exception or so). It was fixed by installing the latest 0.7.5.

2. Using integrate, it only display the expression, even when specifying a range as tuple.
For example (assume x is a symbol):
integrate(exp(-0.5*(-x**2 / 2 + x**4 / 10)), (x, -float("inf"), -3))
The resulting expression is displayed as two integrals, each with a different range.
Changing to -oo doesn't matter much.

3. Only using Integral(expression + parameters) gives the correct textual expression (instead of integrate).

4. Evaluating the 1st and 3rd integrals with evalf() (of Integral) over the range (-infinity, -3) yields negative values which are clearly incorrect. Just as a testing example.

Any idea or maybe my usage is incorrect?

Thanks!,
Moti.

Aaron Meurer

unread,
Apr 6, 2014, 7:15:57 PM4/6/14
to sy...@googlegroups.com
On Thu, Apr 3, 2014 at 5:11 AM, Moti <moti...@gmail.com> wrote:
Hi all,

First I must say that SymPy is a great tool, I came across it few weeks ago and...

Yesterday I was assigned a task to compute analytical expressions for some integrals and evaluate them.
Specifically:
1. exp(-0.5*(-x**2/2 + x**4/10)), x goes from -infinity to infinity, or from infinity to a finite value
2. exp(-0.5*(-x**2/2 + x**4/10)) * (-x**2/2 + x**4/10), same range
3. exp(-0.5*(-x**2/2 + x**4/10 - x/20)), same range
4. exp(-0.5*(-x**2/2 + x**4/10 - x/20)) * (-x**2/2 + x**4/10 - x/20), same range

Using mathematica, an analytic expresion for the first two can be found (using Integrate). The latter two are harder and can be obtained only numerically.
Using NIntegrate, the exact values can be obtained (for example in the range (-infinity, -3) or (3, infinity).
Matlab can also compute these numerically, using quadgk (Gauss-Kronrod method supporting infinites).

As for the issues I was facing:
1. First, the integrate function was failing because of the fraction values in the integrand (Coersion exception or so). It was fixed by installing the latest 0.7.5.

2. Using integrate, it only display the expression, even when specifying a range as tuple.
For example (assume x is a symbol):
integrate(exp(-0.5*(-x**2 / 2 + x**4 / 10)), (x, -float("inf"), -3))
The resulting expression is displayed as two integrals, each with a different range.
Changing to -oo doesn't matter much.

If integrate returns an Integral, it just means it can't do it. You're hitting the limits of the integration algorithms implemented in SymPy.
 

3. Only using Integral(expression + parameters) gives the correct textual expression (instead of integrate).

That's right. Integral is an unevaluated integral.
 

4. Evaluating the 1st and 3rd integrals with evalf() (of Integral) over the range (-infinity, -3) yields negative values which are clearly incorrect. Just as a testing example.

This sounds like a bug. Can you report it at https://github.com/sympy/sympy/issues. For me, though, I get

In [4]: Integral(exp(-0.5*(-x**2/2 + x**4/10)), (x, -oo, -3)).evalf()
Out[4]: 0.0339709308483502

So please report the exact code you ran, so we can reproduce it.

Aaron Meurer
 

Any idea or maybe my usage is incorrect?

Thanks!,
Moti.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b11925fe-267c-4bea-8189-e4deccddecbb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages