pythonsympyexponentialnumerical-integration

Integral of Sympy exponential function is non-numeric


I want to integrate xe^(-4(x^2)) from x=0 to x=1. When i get integral result, result seems not numerical.

My code is:

import sympy as sp
def f(x):
     return x * sp.exp(-4 * (x ** 2))
x = sp.symbols('x')
integral = sp.integrate(f(x), (x, 0, 1))
print(f'Result of analytic integration = {integral}')

Output is:

Result of analytic integration = 1/8 - exp(-4)/8

Solution

  • The return type is an instance of sympy.core.add.Add. You can convert it to float by doing:

    >>> integral
    1/8 - exp(-4)/8
    >>> float(integral)
    0.12271054513890822