Integration by Trigonometric Substitution using Sympy
Question:
I am learning integration by trigonometric substitution and want to perform this using sympy, but am having trouble with differential manipulation and applying the boundary conditions after back-substitution.
I am trying to emulate this logic using the Python module Sympy:
Here is the code I have so far:
import sympy as sy
import sympy.diffgeom as dg
sy.init_printing(use_latex=True)
x, r, t = sy.symbols('x r t')
dx = dg.Differential(x)
dt = dg.Differential(t)
y = sy.sqrt(r ** 2 - x ** 2) * dx
y = y.subs({x: r * sy.sin(t), dx: r * sy.cos(t) * dt})
y = sy.simplify(y)
#here's where the code breaks down
y = sy.integrate(y, t)
y = y.subs(t, sy.asin(x / r))
sy.pprint(y)
I am having trouble with two things:
- performing the integration adds another differential
- I don’t know how to apply the initial boundary conditions after back-substituting
I just believe that a CAS such as Sympy should be capable of basic integration strategies.
Answers:
I wouldn’t recommend using diffgeom to do this. I am not sure how the Differential is supposed to behave but the normal way to do integration is to use an implicit dx
for the integration variable:
>>> from sympy import *
>>> r, t, x = var('r t x', positive=True)
>>> r**2*sin(t)*cos(t)/2+integrate(sqrt(r**2-x**2),(x, r*cos(t), r))
r**2*sin(t)*cos(t)/2 - r**2*asin(cos(t))/2 + pi*r**2/4 - r*sqrt(-r**2*cos(t)**2 + r**2)*cos(t)/2
>>> factor_terms(_)
r**2*(-2*sqrt(1 - cos(t)**2)*cos(t) + 2*sin(t)*cos(t) - 2*asin(cos(t)) + pi)/4
>>> _.subs(sqrt(1-cos(t)**2),sin(t))
r**2*(pi - 2*asin(cos(t)))/4
>>> _.subs(asin(cos(t)),pi/2-t)
r**2*t/2
I am learning integration by trigonometric substitution and want to perform this using sympy, but am having trouble with differential manipulation and applying the boundary conditions after back-substitution.
I am trying to emulate this logic using the Python module Sympy:
Here is the code I have so far:
import sympy as sy
import sympy.diffgeom as dg
sy.init_printing(use_latex=True)
x, r, t = sy.symbols('x r t')
dx = dg.Differential(x)
dt = dg.Differential(t)
y = sy.sqrt(r ** 2 - x ** 2) * dx
y = y.subs({x: r * sy.sin(t), dx: r * sy.cos(t) * dt})
y = sy.simplify(y)
#here's where the code breaks down
y = sy.integrate(y, t)
y = y.subs(t, sy.asin(x / r))
sy.pprint(y)
I am having trouble with two things:
- performing the integration adds another differential
- I don’t know how to apply the initial boundary conditions after back-substituting
I just believe that a CAS such as Sympy should be capable of basic integration strategies.
I wouldn’t recommend using diffgeom to do this. I am not sure how the Differential is supposed to behave but the normal way to do integration is to use an implicit dx
for the integration variable:
>>> from sympy import *
>>> r, t, x = var('r t x', positive=True)
>>> r**2*sin(t)*cos(t)/2+integrate(sqrt(r**2-x**2),(x, r*cos(t), r))
r**2*sin(t)*cos(t)/2 - r**2*asin(cos(t))/2 + pi*r**2/4 - r*sqrt(-r**2*cos(t)**2 + r**2)*cos(t)/2
>>> factor_terms(_)
r**2*(-2*sqrt(1 - cos(t)**2)*cos(t) + 2*sin(t)*cos(t) - 2*asin(cos(t)) + pi)/4
>>> _.subs(sqrt(1-cos(t)**2),sin(t))
r**2*(pi - 2*asin(cos(t)))/4
>>> _.subs(asin(cos(t)),pi/2-t)
r**2*t/2