Skip to main content

Tutorial 10

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:

  • Double integrals over rectangles and general regions
  • Order of integration and why it matters for the difficulty of the calculation
  • Triple integrals over boxes and solids
  • Mass and centre of mass of a lamina and of a solid

Introduction​

SymPy evaluates iterated integrals directly: write integrate(f, (y, y_low, y_high), (x, x_low, x_high)) and it works from the innermost variable outwards, exactly as you would by hand.

import sympy as sy
from sympy.abc import x, y, z, t
from sympy import (sqrt, sin, cos, pi, ln, exp, integrate, simplify, diff, expand,
Rational, N, Matrix, S)
sy.init_printing(use_latex=True)

Question 1​

Evaluate the iterated integrals.

a. ∫12∫23(2x−y) dy dx\displaystyle\int_{1}^{2}\int_{2}^{3}(2x-y)\,dy\,dx

display(integrate(2*x - y, (y, 2, 3), (x, 1, 2)))        # 1/2

b. ∫15∫−12(x−5y) dx dy\displaystyle\int_{1}^{5}\int_{-1}^{2}(x-5y)\,dx\,dy

display(integrate(x - 5*y, (x, -1, 2), (y, 1, 5)))       # -174

c. ∫13∫25(2x−3y) dx dy\displaystyle\int_{1}^{3}\int_{2}^{5}(2x-3y)\,dx\,dy

display(integrate(2*x - 3*y, (x, 2, 5), (y, 1, 3)))      # 6

Question 2​

Evaluate over the given region DD.

a. ∬D(1−12x2−12y2)dA\displaystyle\iint_{D}\left(1-\frac{1}{2}x^{2}-\frac{1}{2}y^{2}\right)dA, DD is the unit square 0≤x≤10\le x\le1, 0≤y≤10\le y\le1.

display(integrate(1 - Rational(1, 2)*x**2 - Rational(1, 2)*y**2,
(y, 0, 1), (x, 0, 1))) # 2/3

b. ∬D(4xy−y3)dA\displaystyle\iint_{D}\left(4xy-y^{3}\right)dA, where DD is bounded by y=xy=\sqrt{x} and y=x3y=x^{3}.

The curves meet at x=0x=0 and x=1x=1, and on that interval x3≤y≤xx^{3}\le y\le\sqrt{x}.

# solve over the reals only, so the complex roots are not returned
display(list(sy.solveset(sqrt(x) - x**3, x, domain=S.Reals))) # [0, 1]
display(integrate(4*x*y - y**3, (y, x**3, sqrt(x)), (x, 0, 1))) # 55/156

Question 3​

∬D42y2−12x dA\displaystyle\iint_{D}42y^{2}-12x\,dA where D={(x,y)∣0≤x≤4, (x−2)2≤y≤6}D=\{(x,y)\mid 0\le x\le4,\ (x-2)^{2}\le y\le6\}.

display(integrate(42*y**2 - 12*x, (y, (x - 2)**2, 6), (x, 0, 4)))  # 11136

Question 4​

∬R12x−18y dA\displaystyle\iint_{R}12x-18y\,dA over R=[−1,4]×[2,3]R=[-1,4]\times[2,3], both ways round.

display(integrate(12*x - 18*y, (x, -1, 4), (y, 2, 3)))   # -135
display(integrate(12*x - 18*y, (y, 2, 3), (x, -1, 4))) # -135

Because the region is a rectangle and the integrand is a sum of separate functions of xx and yy, both orders give the same answer.

Question 5​

Compute the double integrals over the indicated rectangles.

a. ∬R6yx−2y3 dA\displaystyle\iint_{R}6y\sqrt{x}-2y^{3}\,dA, R=[1,4]×[0,3]R=[1,4]\times[0,3]

display(integrate(6*y*sqrt(x) - 2*y**3, (x, 1, 4), (y, 0, 3)))    # 9/2

b. ∬Ry e y2−4x dA\displaystyle\iint_{R}y\,e^{\,y^{2}-4x}\,dA, R=[0,2]×[0,8]R=[0,2]\times[0,\sqrt{8}]

R5 = integrate(y*exp(y**2 - 4*x), (y, 0, sqrt(8)), (x, 0, 2))
display(expand(R5), sy.N(R5, 8)) # -1/4 + e^-8/8 + e^8/8 = 372.36979

∬Ry e y2−4x dA=18(e8+e−8−2)≈372.37\iint_{R}y\,e^{\,y^{2}-4x}\,dA=\frac{1}{8}\left(e^{8}+e^{-8}-2\right)\approx372.37

note

The printed solution for 5(b) is a copy-paste of 5(a)'s working. The value 372.3698372.3698 is correct, but the integral shown above it is not the one being evaluated.

Question 6​

Evaluate the triple integrals.

a. ∫−31∫−12∫016xyz dy dx dz\displaystyle\int_{-3}^{1}\int_{-1}^{2}\int_{0}^{1}6xyz\,dy\,dx\,dz

display(integrate(6*x*y*z, (y, 0, 1), (x, -1, 2), (z, -3, 1)))   # -18

b. ∫04∫−2−1∫12xy dx dy dz\displaystyle\int_{0}^{4}\int_{-2}^{-1}\int_{1}^{2}xy\,dx\,dy\,dz

display(integrate(x*y, (x, 1, 2), (y, -2, -1), (z, 0, 4)))       # -9

Question 7​

Evaluate the triple integrals over the given boxes.

a. ∭Bxyz2 dV\displaystyle\iiint_{B}xyz^{2}\,dV, 0≤x≤10\le x\le1, −1≤y≤2-1\le y\le2, 0≤z≤30\le z\le3

display(integrate(x*y*z**2, (x, 0, 1), (y, -1, 2), (z, 0, 3)))   # 27/4

b. ∭B(4x2y−z3)dz dy dx\displaystyle\iiint_{B}\left(4x^{2}y-z^{3}\right)dz\,dy\,dx, 2≤x≤32\le x\le3, −1≤y≤4-1\le y\le4, 0≤z≤10\le z\le1

B7 = integrate(4*x**2*y - z**3, (z, 0, 1), (y, -1, 4), (x, 2, 3))
display(B7) # 755/4
display(sy.N(B7, 8)) # 188.75

∭B(4x2y−z3)dV=7554=188.75\iiint_{B}\left(4x^{2}y-z^{3}\right)dV=\frac{755}{4}=188.75

note

The printed solution flips the sign after the zz-integration, writing 14−4x2y\frac{1}{4}-4x^{2}y instead of 4x2y−144x^{2}y-\frac{1}{4}, and so obtains −755/4-755/4. Substituting the zz-limits gives [4x2yz−z44]01=4x2y−14\left[4x^{2}yz-\frac{z^{4}}{4}\right]_{0}^{1}=4x^{2}y-\frac{1}{4}. Note this integrand is not everywhere positive: it is negative for y<116x2y<\frac{1}{16x^{2}} (for instance −65/4-65/4 at x=2,y=−1x=2,y=-1), so the printed sign flip cannot be argued away by a positivity claim. The correct answer is +755/4+755/4.

Question 8​

Integrate f(x,y,z)=xyf(x,y,z)=xy over the volume enclosed by z=x+yz=x+y, z=0z=0, y=x2y=x^{2} and x=y2x=y^{2}.

The curves y=x2y=x^{2} and x=y2x=y^{2} meet at (0,0)(0,0) and (1,1)(1,1); integrating zz first gives the limits 0≤z≤x+y0\le z\le x+y.

V8 = integrate(x*y, (z, 0, x + y), (y, x**2, sqrt(x)), (x, 0, 1))
display(V8) # 3/28

Question 9​

Mass of the unit-square lamina with density ρ(x,y)=x+y+2 g/cm2\rho(x,y)=x+y+2\ \text{g/cm}^{2}.

m = integrate(x + y + 2, (x, 0, 1), (y, 0, 1))
display(m) # 3 g

Question 10​

Mass and centre of mass of the tetrahedron QQ bounded by x+2y+3z=6x+2y+3z=6 and the coordinate planes, with density ρ(x,y,z)=x2yz\rho(x,y,z)=x^{2}yz.

The tetrahedron meets the axes at (6,0,0)(6,0,0), (0,3,0)(0,3,0) and (0,0,2)(0,0,2); projecting onto the xyxy-plane gives 0≤x≤60\le x\le6, 0≤y≤12(6−x)0\le y\le\frac{1}{2}(6-x) and 0≤z≤13(6−x−2y)0\le z\le\frac{1}{3}(6-x-2y).

ztop = Rational(1, 3)*(6 - x - 2*y)
ytop = Rational(1, 2)*(6 - x)

m = integrate(x**2*y*z, (z, 0, ztop), (y, 0, ytop), (x, 0, 6))
Mxy = integrate(x**2*y*z**2, (z, 0, ztop), (y, 0, ytop), (x, 0, 6)) # about xy-plane
Mxz = integrate(x**2*y**2*z, (z, 0, ztop), (y, 0, ytop), (x, 0, 6)) # about xz-plane
Myz = integrate(x**3*y*z, (z, 0, ztop), (y, 0, ytop), (x, 0, 6)) # about yz-plane

display(m, Mxy, Mxz, Myz) # 108/35, 54/35, 81/35, 243/35
display(simplify(Myz/m), simplify(Mxz/m), simplify(Mxy/m)) # 9/4, 3/4, 1/2
m=10835,(xˉ,yˉ,zˉ)=(94,34,12)=(2.25, 0.75, 0.5)m=\frac{108}{35},\qquad (\bar{x},\bar{y},\bar{z})=\left(\frac{9}{4},\frac{3}{4},\frac{1}{2}\right)=(2.25,\ 0.75,\ 0.5)
note

The printed solution labels the last coordinate yˉ=Mxym\bar{y}=\dfrac{M_{xy}}{m}. By definition zˉ=Mxy/m\bar{z}=M_{xy}/m, yˉ=Mxz/m\bar{y}=M_{xz}/m and xˉ=Myz/m\bar{x}=M_{yz}/m; the numbers are right, only the label is wrong.


Extra Learning Resources​

Key Concepts to Master​

  1. Fubini's theorem: for a rectangle the order of integration can be swapped freely
  2. General regions: find the outer limits from the projection, then the inner limits from the bounding curves
  3. Order matters for effort: some integrands have no elementary antiderivative in one order but are easy in the other
  4. Triple integrals: integrate the innermost variable first, then work outwards
  5. Centre of mass: xˉ=Myz/m\bar{x}=M_{yz}/m, yˉ=Mxz/m\bar{y}=M_{xz}/m, zˉ=Mxy/m\bar{z}=M_{xy}/m

SymPy Resources​

  • SymPy integrate — nested definite integrals
  • SymPy Rational — exact fractions for limits such as 13(6−x−2y)\frac{1}{3}(6-x-2y)
  • SymPy solve — intersection points of the bounding curves

Common Pitfalls​

  • Use Rational(1, 2), not 1/2, inside an integrand or SymPy will switch to floats
  • The inner limits may depend on the outer variables — get the nesting order right
  • Watch the sign when the integrand changes sign over the region (as in 1(b), where the answer is −174-174)
  • Double-check which moment belongs to which coordinate: zˉ\bar{z} pairs with MxyM_{xy}