Unit content
Center of mass of continuous mass distributions
For a system of discrete particles, the center of mass is the mass-weighted average
$$\mathbf R_{\rm CM}=\frac{1}{M}\sum_i m_i\mathbf r_i.$$
For a continuous body, the same idea becomes an integral over infinitesimal mass elements $dm$:
$$\boxed{\mathbf R_{\rm CM}=\frac{1}{M}\int \mathbf r,dm},$$
with total mass
$$\boxed{M=\int dm}.$$
The integral does not average positions uniformly. Each position is weighted by the amount of mass located there.
Choosing the mass element
The form of $dm$ depends on how the mass is distributed.
For a thin rod with linear mass density $\lambda$,
$$dm=\lambda,dx.$$
For a thin lamina with surface mass density $\sigma$,
$$dm=\sigma,dA.$$
For a three-dimensional body with volume mass density $\rho$,
$$dm=\rho,dV.$$
If the density varies with position, use the corresponding function such as $\lambda(x)$ or $\rho(\mathbf r)$ inside the integral.
One-dimensional form
For a rod lying along the $x$-axis,
$$\boxed{x_{\rm CM}=\frac{1}{M}\int x,dm}.$$
If its linear density is $\lambda(x)$,
$$M=\int \lambda(x),dx$$
and
$$\boxed{x_{\rm CM}=\frac{\int x\lambda(x),dx}{\int \lambda(x),dx}}.$$
The integration limits cover the physical extent of the body.
Example: uniform rod
Consider a uniform rod of length $L$ extending from $x=0$ to $x=L$. Its constant linear density is
$$\lambda=\frac{M}{L}.$$
Then
$$x_{\rm CM} =\frac{1}{M}\int_0^L x\lambda,dx =\frac{\lambda}{M}\left[\frac{x^2}{2}\right]_0^L.$$
Using $M=\lambda L$,
$$x_{\rm CM} =\frac{\lambda L^2/2}{\lambda L} =\boxed{\frac L2}.$$
The result agrees with symmetry: a uniform rod balances at its midpoint.
Example: rod with increasing density
Now let a rod extend from $x=0$ to $x=L$ with
$$\lambda(x)=kx,$$
so the rod becomes progressively denser toward $x=L$.
Its mass is
$$M=\int_0^L kx,dx =\frac{kL^2}{2}.$$
The first moment of mass is
$$\int_0^L x,dm =\int_0^L x(kx),dx =k\int_0^L x^2,dx =\frac{kL^3}{3}.$$
Therefore
$$x_{\rm CM} =\frac{kL^3/3}{kL^2/2} =\boxed{\frac{2L}{3}}.$$
The center of mass lies to the right of the midpoint because more mass is concentrated there.
Components in two and three dimensions
The vector formula can be evaluated component by component:
$$x_{\rm CM}=\frac1M\int x,dm,$$
$$y_{\rm CM}=\frac1M\int y,dm,$$
$$z_{\rm CM}=\frac1M\int z,dm.$$
Symmetry can often eliminate much of the calculation. For example, if a body's mass distribution is mirror-symmetric about a plane, its center of mass must lie in that plane.
For a body of uniform density, the density factor cancels from numerator and denominator. The center of mass then coincides with the geometric centroid of the shape. If density is nonuniform, the geometric center need not be the center of mass.
The continuous formula is therefore the direct generalization of the discrete mass-weighted average: replace the sum over particles by an integral over the body's mass.