Link Search Menu Expand Document (external link)

How to write and evaluate indefinite integrals (in Python, using SymPy)

See all solutions.

Task

The antiderivative of a function is expressed using an indefinite integral, as in

\[\int f(x)\;dx.\]

How can we write and evaluate indefinite integrals using software?

Related tasks:

Solution

This answer assumes you have imported SymPy as follows.

1
2
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output

Let’s choose an example formula whose antiderivative we will compute.

1
2
3
var( 'x' )
formula = 3*sqrt(x)
formula

$\displaystyle 3 \sqrt{x}$

Use the Integral function to build a definite integral without evaluating it. The second parameter is the variable with respect to which you’re integrating.

1
Integral( formula, x )

$\displaystyle \int 3 \sqrt{x}\, dx$

Use the integrate function to perform the integration, showing the answer.

1
integrate( formula, x )

$\displaystyle 2 x^{\frac{3}{2}}$

1
integrate( formula, x ) + var('C')  # same, but with a constant of integration

$\displaystyle C + 2 x^{\frac{3}{2}}$

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)