In [1]:
from sympy import *
init_printing(use_latex='mathjax')
n, m = symbols('n,m', integer=True)
x, y, z = symbols('x,y,z')

Integrals

In the last section we learned symbolic differentiation with .diff. Here we'll cover symbolic integration with integrate.

Here is how we write the indefinite integral

$$ \int x^2 dx = \frac{x^3}{3}$$

In [2]:
# Indefinite integral
integrate(x**2, x)


Out[2]:
$$\frac{x^{3}}{3}$$

And the definite integral

$$ \int_0^3 x^2 dx = \left.\frac{x^3}{3} \right|_0^3 = \frac{3^3}{3} - \frac{0^3}{3} = 9 $$

In [3]:
# Definite integral
integrate(x**2, (x, 0, 3))


Out[3]:
$$9$$

As always, because we're using symbolics, we could use a symbol whenever we previously used a number

$$ \int_y^z x^n dx $$

In [4]:
integrate(x**n, (x, y, z))


Out[4]:
$$\begin{cases} - \log{\left (y \right )} + \log{\left (z \right )} & \text{for}\: n = -1 \\- \frac{y^{n + 1}}{n + 1} + \frac{z^{n + 1}}{n + 1} & \text{otherwise} \end{cases}$$

Exercise

Compute the following integrals:

$$ \int \sin(x) dx $$$$ \int_0^{\pi} \sin(x) dx $$$$ \int_0^y x^5 + 12x^3 - 2x + 1 $$$$ \int e^{\frac{(x - \mu)^2}{\sigma^2}} $$

Feel free to play with various parameters and settings and see how the results change.


In [7]:
# Use `integrate` to solve the integrals above

Are there some integrals that SymPy can't do? Find some.


In [ ]:
# Use `integrate` on other equations.  Symbolic integration has it limits, find them.