Skip to main content

Tutorial 8

Binder Open In Colab

Interactive Notebooks

Click the badges above to run this tutorial interactively in your browser without installing anything!

  • Binder: Free cloud-based Jupyter environment
  • Colab: Google's free Jupyter notebook environment

Topics Covered​

This tutorial covers the following topics using Python and SymPy:

  • Integration by parts
  • Partial fractions
  • Trigonometric integrals
  • Trigonometric substitution
  • Verifying an antiderivative by differentiating it back

Introduction​

integrate(f, x) returns an antiderivative of f with respect to x. Because an antiderivative is only defined up to an additive constant, SymPy's answer may look different from the printed solution while still being correct. The reliable check is always the same: differentiate the answer and see whether you get the integrand back.

import sympy as sy
from sympy.abc import x, t, u
from sympy import (sqrt, sin, cos, tan, sec, csc, cot, pi, ln, log, exp, atan, asin,
apart, integrate, diff, simplify, expand, Symbol)
sy.init_printing(use_latex=True)

def check(F, f):
"""Differentiate the antiderivative F and compare with the integrand f."""
return simplify(diff(F, x) - f)

Question 1​

∫ln⁡(x2+2) dx\int \ln\left(x^{2}+2\right)\,dx

res = integrate(ln(x**2 + 2), x)
display(res) # x*log(x^2+2) - 2*x + 2*sqrt(2)*atan(sqrt(2)*x/2)
display(check(res, ln(x**2 + 2))) # 0 -> correct

Rearranging gives x(ln⁡(x2+2)−2)+22tan⁡−1(x2)+Cx\left(\ln\left(x^{2}+2\right)-2\right)+2\sqrt{2}\tan^{-1}\left(\dfrac{x}{\sqrt{2}}\right)+C.

Question 2​

∫x2ln⁡x dx\int x^{2}\ln x\,dx

res = integrate(x**2*ln(x), x)
display(res) # x**3*(3*log(x) - 1)/9
display(check(res, x**2*ln(x))) # 0

∫x2ln⁡x dx=x33ln⁡x−x39+C\int x^{2}\ln x\,dx=\frac{x^{3}}{3}\ln x-\frac{x^{3}}{9}+C

Question 3​

∫x3ex2 dx\int x^{3}e^{x^{2}}\,dx

res = integrate(x**3*exp(x**2), x)
display(res) # (x**2 - 1)*exp(x**2)/2
display(check(res, x**3*exp(x**2))) # 0

∫x3ex2 dx=12ex2(x2−1)+C\int x^{3}e^{x^{2}}\,dx=\tfrac{1}{2}e^{x^{2}}\left(x^{2}-1\right)+C

Question 4​

∫x+1x3+x2−6x dx\int \frac{x+1}{x^{3}+x^{2}-6x}\,dx

Partial fractions first, then integrate term by term.

f = (x + 1)/(x**3 + x**2 - 6*x)
display(sy.factor(x**3 + x**2 - 6*x)) # x*(x - 2)*(x + 3)
display(apart(f, x)) # -2/(15*(x+3)) + 3/(10*(x-2)) - 1/(6*x)

display(integrate(f, x))
∫x+1x3+x2−6x dx=−16ln⁡∣x∣+310ln⁡∣x−2∣−215ln⁡∣x+3∣+C\int \frac{x+1}{x^{3}+x^{2}-6x}\,dx =-\frac{1}{6}\ln|x|+\frac{3}{10}\ln|x-2|-\frac{2}{15}\ln|x+3|+C

Question 5​

∫x3+x2+x+2x4+3x2+2 dx\int \frac{x^{3}+x^{2}+x+2}{x^{4}+3x^{2}+2}\,dx

f = (x**3 + x**2 + x + 2)/(x**4 + 3*x**2 + 2)
display(sy.factor(x**4 + 3*x**2 + 2)) # (x**2 + 1)*(x**2 + 2)
display(apart(f, x)) # x/(x**2 + 2) + 1/(x**2 + 1)

display(integrate(f, x)) # log(x**2 + 2)/2 + atan(x)
∫x3+x2+x+2(x2+1)(x2+2) dx=tan⁡−1x+12ln⁡(x2+2)+C\int \frac{x^{3}+x^{2}+x+2}{\left(x^{2}+1\right)\left(x^{2}+2\right)}\,dx =\tan^{-1}x+\tfrac{1}{2}\ln\left(x^{2}+2\right)+C

Question 6​

∫tan⁡3(3x)sec⁡4(3x) dx\int \tan^{3}(3x)\sec^{4}(3x)\,dx

f = tan(3*x)**3*sec(3*x)**4
res = integrate(f, x)
display(res)
display(check(res, f)) # 0

SymPy returns 2−3cos⁡2(3x)36cos⁡6(3x)\dfrac{2-3\cos^{2}(3x)}{36\cos^{6}(3x)}, which is the tutorial's answer up to a constant. Expanding both in powers of tan⁡(3x)\tan(3x) shows they agree:

expected = tan(3*x)**4/12 + tan(3*x)**6/18
display(simplify(res - expected)) # -1/36 (a constant -> same family)

∫tan⁡3(3x)sec⁡4(3x) dx=112tan⁡4(3x)+118tan⁡6(3x)+C\int \tan^{3}(3x)\sec^{4}(3x)\,dx=\tfrac{1}{12}\tan^{4}(3x)+\tfrac{1}{18}\tan^{6}(3x)+C

Question 7​

∫sin⁡4x cos⁡7x dx\int \sin^{4}x\,\cos^{7}x\,dx

f = sin(x)**4*cos(x)**7
res = expand(integrate(f, x))
display(res)

expected = sin(x)**5/5 - 3*sin(x)**7/7 + sin(x)**9/3 - sin(x)**11/11
display(simplify(res - expected)) # 0 -> identical
∫sin⁡4xcos⁡7x dx=15sin⁡5x−37sin⁡7x+13sin⁡9x−111sin⁡11x+C\int \sin^{4}x\cos^{7}x\,dx =\tfrac{1}{5}\sin^{5}x-\tfrac{3}{7}\sin^{7}x+\tfrac{1}{3}\sin^{9}x-\tfrac{1}{11}\sin^{11}x+C

Question 8​

∫dxx29−x2\int \frac{dx}{x^{2}\sqrt{9-x^{2}}}

SymPy's direct answer is a Piecewise, because the antiderivative is only real on −3<x<3-3<x<3. The textbook route is the substitution x=3sin⁡θx=3\sin\theta, which we can do step by step:

display(integrate(1/(x**2*sqrt(9 - x**2)), x))   # Piecewise - domain dependent

th = Symbol('theta', positive=True)
sub = (3*cos(th)) / ((3*sin(th))**2 * 3*cos(th)) # dx / (x^2 sqrt(9 - x^2))
display(simplify(sub)) # 1/(9*sin(theta)**2)
display(simplify(integrate(sub, th))) # -1/(9*tan(theta))

Since sin⁡θ=x3\sin\theta=\dfrac{x}{3}, we have cot⁡θ=9−x2x\cot\theta=\dfrac{\sqrt{9-x^{2}}}{x}, so

∫dxx29−x2=−19cot⁡θ+C=−9−x29x+C\int \frac{dx}{x^{2}\sqrt{9-x^{2}}}=-\frac{1}{9}\cot\theta+C =-\frac{\sqrt{9-x^{2}}}{9x}+C
res = -sqrt(9 - x**2)/(9*x)
display(check(res, 1/(x**2*sqrt(9 - x**2)))) # 0 -> correct

Additional exercises​

1. ∫x+7x2(x+2) dx\displaystyle\int \frac{x+7}{x^{2}(x+2)}\,dx​

There is a repeated linear factor, so the partial fraction decomposition needs a term Bx2\dfrac{B}{x^{2}} as well as Ax\dfrac{A}{x}.

f = (x + 7)/(x**2*(x + 2))
display(apart(f, x)) # 5/(4*(x+2)) - 5/(4*x) + 7/(2*x**2)
display(integrate(f, x))
∫x+7x2(x+2) dx=−54ln⁡∣x∣−72x+54ln⁡∣x+2∣+C\int \frac{x+7}{x^{2}(x+2)}\,dx =-\frac{5}{4}\ln|x|-\frac{7}{2x}+\frac{5}{4}\ln|x+2|+C

2. ∫cos⁡xsin⁡3x+sin⁡x dx\displaystyle\int \frac{\cos x}{\sin^{3}x+\sin x}\,dx​

Substitute u=sin⁡xu=\sin x, so du=cos⁡x dxdu=\cos x\,dx:

display(apart(1/(u**3 + u), u))                  # 1/u - u/(u^2 + 1)
display(integrate(1/(u**3 + u), u)) # log(u) - log(u^2 + 1)/2

Substituting back u=sin⁡xu=\sin x:

∫cos⁡xsin⁡3x+sin⁡x dx=ln⁡∣sin⁡x∣−12ln⁡∣sin⁡2x+1∣+C\int \frac{\cos x}{\sin^{3}x+\sin x}\,dx =\ln|\sin x|-\tfrac{1}{2}\ln\left|\sin^{2}x+1\right|+C
res = ln(sin(x)) - ln(sin(x)**2 + 1)/2
display(check(res, cos(x)/(sin(x)**3 + sin(x)))) # 0 -> correct

Extra Learning Resources​

Key Concepts to Master​

  1. Integration by parts: ∫u dv=uv−∫v du\int u\,dv=uv-\int v\,du
  2. Partial fractions: factor the denominator, then match coefficients (or substitute convenient values of xx)
  3. Repeated factors need one term per power: Ax+Bx2+…\frac{A}{x}+\frac{B}{x^{2}}+\ldots
  4. Odd power of sin⁡\sin or cos⁡\cos: split off one factor and substitute uu for the other
  5. Trigonometric substitution: a2−x2→x=asin⁡θa^{2}-x^{2}\to x=a\sin\theta, a2+x2→x=atan⁡θa^{2}+x^{2}\to x=a\tan\theta, x2−a2→x=asec⁡θx^{2}-a^{2}\to x=a\sec\theta

SymPy Resources​

Common Pitfalls​

  • SymPy does not add + C; you must write it in your final answer
  • An antiderivative that differs from the printed one by a constant is still correct — always verify by differentiating
  • log(x, 10) is base 10; use ln(x) for the natural logarithm
  • A Piecewise result just means SymPy had to split the domain; substitute the branch valid on your interval
  • Remember the absolute value inside logarithms for indefinite integrals