A9 · Lesson 20 · Lab: multiple integrals
Grids, random points and coordinate changes
Lessons 14 to 19 evaluated integrals over regions by slicing and by changing coordinates. Most real regions and integrands don't have antiderivatives, and engineers integrate them numerically. In this lab you'll integrate over a non-rectangular region on a grid, estimate a volume by throwing random points, check the Gaussian-beam power formula in polar coordinates, find the centroid of a semicircular plate, and confirm the spherical Jacobian numerically.
- Monte Carlo integration estimates an area or volume as the fraction of random points that land inside, times the box's size.
- The curve shows how its typical error shrinks with the number of points : as .
- A grid rule's error in two dimensions shrinks much faster for smooth integrands, but in ten dimensions Monte Carlo wins, because its rate doesn't depend on the dimension.
Predict
A Monte Carlo estimate uses 10 000 points and has about 1% error. How many points would give 0.1%?
A million. The error falls as , so ten times better needs a hundred times more points. That slow convergence is Monte Carlo's weakness in low dimensions, where a grid is far better, and its strength in high dimensions, where a grid would need impossibly many points.
Name it
Grid rules in two dimensions
The midpoint rule samples the integrand at the center of each small cell and adds . A region that isn't a rectangle can be handled by multiplying by an indicator that is 1 inside and 0 outside, though the jagged edge limits the accuracy to about the cell size.
Monte Carlo
Draw uniform random points in a box of volume . The fraction inside a region estimates its volume as , with a relative error of about . To integrate , average at the points and multiply by .
Polar and spherical integrals in code
Changing coordinates puts the Jacobian into the integrand: in polar, in spherical. A numerical integral in those coordinates uses a rectangular grid in or , weighted by the Jacobian.
Worked example
Estimate the volume of the unit sphere by Monte Carlo with 200 000 points.
Show the solution
- Sample the cube. Draw points uniformly in the cube of volume 8, and count those with .
- Expected fraction.
- One run. It gives a fraction of 0.5217, so the estimate is against the true 4.189: 0.4% low.
- Compare with the expected error. The run is within twice that.
Answer one run estimates the volume at 4.173, 0.4% below the true 4.189.
Worked example
Find the centroid of a semicircular plate of radius .
Show the solution
- Set up in polar coordinates. The half disk is , , with
- Integrate .
- Divide by the area.
- Interpret. Its balance point is a little under halfway up, because the plate is wider near the base.
Answer the centroid sits on the axis of symmetry, above the straight edge.
Check
A Monte Carlo region catches a fraction 0.8 of the points. About how many points give a relative error of 1%?
Check
How far above its flat edge is the centroid of a semicircular plate of radius 3 cm?
Write units the way you normally would: 1.5 cm, 15 mm and 0.015 m are the same answer.
Check
Integrate over the triangle with the midpoint rule on an grid over the unit square ( = 400), using an indicator for the triangle. Set I and exact ().
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Estimate the unit sphere's volume by Monte Carlo: 200 000 points in from np.random.default_rng(1). Set vol.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A Gaussian beam has with = 2 W/cm² and = 1 mm. Integrate numerically in polar coordinates ( from 0 to 5 mm, 2000 steps, trapezoid) and set P in watts and formula to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Find the centroid height of a semicircular plate of radius 1 numerically: integrate and 1 over , with the midpoint rule on a 300 × 300 polar grid (Jacobian ), and set ybar.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Check the spherical Jacobian numerically: write the map , compute its 3 × 3 Jacobian matrix by central differences at , and set det to its determinant and formula to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does Monte Carlo integration beat a grid in high dimensions, even though it converges so slowly?
A grid rule with points per side in dimensions needs points, and its error for a smooth integrand shrinks like a power of . In two dimensions that's cheap, but in ten dimensions even means points. Monte Carlo's error is whatever the dimension, because it's a statistical average, and averages don't care how many coordinates each sample has. So beyond a handful of dimensions, a million random points give a usable answer where a grid can't even be built. Circuit yield analysis, with dozens of independently varying components, is a high-dimensional integral of exactly this kind.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.