MATH02, Integral calculus
Back to the previous page
List of posts to read before reading this article
Contents
Indefinite integral
Single-variable function
INPUT
import sympy
#symypy.init_printing()
from sympy import symbols, Function, integrate
x = symbols("x")
f = Function("f")(x)
integrate(f)
OUTPUT
: \(\displaystyle \int f{\left(x \right)}\, dx\)
Explicit function
import sympy
#symypy.init_printing()
from sympy import symbols, sin, integrate
x = symbols("x")
integrate(sin(x))
OUTPUT
: \(\displaystyle - \cos{\left(x \right)}\)
Fail to evaluate an integral, Representing the formal integral
import sympy
#symypy.init_printing()
from sympy import symbols, sin, ,cos, integrate
x = symbols("x")
integrate(sin(x * cos(x)))
OUTPUT
: \(\displaystyle \int \sin{\left(x \cos{\left(x \right)} \right)}\, dx\)
Symbolically represent an integral
import sympy
#symypy.init_printing()
from sympy import symbols, Integral
x = symbols("x")
Integral(x)
OUTPUT
: \(\displaystyle \int x\, dx\)
Evalutation for an integral
import sympy
#symypy.init_printing()
from sympy import symbols, Integral
x = symbols("x")
i = Integral(x)
i.doit()
OUTPUT
: \(\displaystyle \frac{x^{2}}{2}\)
Integral of a multivariable expression
import sympy
#symypy.init_printing()
from sympy import symbols, integrate
x, y = symbols("x, y")
expr = (x + y)**2
integrate(expr, x, y)
OUTPUT
: \(\displaystyle \frac{x^{3} y}{3} + \frac{x^{2} y^{2}}{2} + \frac{x y^{3}}{3}\)
Definite integral
Single-variable function
import sympy
#symypy.init_printing()
from sympy import symbols, Function, integrate
a, b, x = symbols("a, b, x")
f = Function("f")(x)
integrate(f, (x, a, b))
OUTPUT
: \(\displaystyle \int\limits_{a}^{b} f{\left(x \right)}\, dx\)
Explicit function
import sympy
#symypy.init_printing()
from sympy import symbols, sin, integrate
a, b, x = symbols("a, b, x")
integrate(sin(x), (x, a, b))
OUTPUT
: \(\displaystyle \cos{\left(a \right)} - \cos{\left(b \right)}\)
Infinity
import sympy
#symypy.init_printing()
from sympy import symbols, exp, integrate, oo
x = symbols("x")
integrate(exp(-x**2), (x, 0, oo))
OUTPUT
: \(\displaystyle \frac{\sqrt{\pi}}{2}\)
Fail to evaluate an integral, Representing the formal integral
import sympy
#symypy.init_printing()
from sympy import symbols, sin, cos, integrate, oo
a, b, x = symbols("a, b, x")
integrate(sin(x * cos(x)),(x,a,b))
OUTPUT
: \(\displaystyle \int\limits_{a}^{b} \sin{\left(x \cos{\left(x \right)} \right)}\, dx\)
Symbolically represent an integral
import sympy
#symypy.init_printing()
from sympy import symbols, Integral
a, b, x = symbols("a, b, x")
Integral(x, (x, a, b))
OUTPUT
: \(\displaystyle \int\limits_{a}^{b} x\, dx\)
Evalutation for an integral
import sympy
#symypy.init_printing()
from sympy import symbols, Integral
a, b, x = symbols("a, b, x")
i = Integral(x, (x, a, b))
i.doit()
OUTPUT
: \(\displaystyle - \frac{a^{2}}{2} + \frac{b^{2}}{2}\)
Integral of a multivariable expression
import sympy
#symypy.init_printing()
from sympy import symbols, integrate
x, y = symbols("x, y")
expr = (x + y)**2
integrate(expr, (x, 0, 1), (y, 0, 1))
OUTPUT
: \(\displaystyle \frac{7}{6}\)
List of posts followed by this article
Reference