Gossen's second law

Gossen's Second “Law”, named for Hermann Heinrich Gossen (1810–1858), is the assertion that an economic agent will allocate his or her expenditures such that the ratio of the marginal utility of each good or service to its price (the marginal expenditure necessary for its acquisition) is equal to that for every other good or service. Formally,
 * $$\frac{\partial U/\partial x_i}{p_i}=\frac{\partial U/\partial x_j}{p_j}~\forall\left(i,j\right)$$

where
 * $$U$$ is utility
 * $$x_i$$ is quantity of the $$i$$-th good or service
 * $$p_i$$ is the price of the $$i$$-th good or service

Informal derivation
Imagine that an agent has spent money on various sorts of goods or services. If the last unit of currency spent on goods or services of one sort bought a quantity with less marginal utility than that which would have been associated with the quantity of another sort that could have been bought with the money, then the agent would have been better off instead buying more of that other good or service. Assuming that goods and services are continuously divisible, the only way that it is possible that the marginal expenditure on one good or service should not yield more utility than the marginal expenditure on the other (or vice versa) is if the marginal expenditures yield equal utility.

Formal derivation
Assume that utility, goods, and services have the requisite properties so that $$\partial U/\partial x_i$$ is well defined for each good or service. An agent then optimizes
 * $$U\left(x_1 ,x_2 ,\dots,x_n\right)$$

subject to a budget constraint
 * $$W\geq\sum_{i=1}^n \left(p_i\cdot x_i \right)$$

where $$W$$ is the total available sum of money. Using the method of Lagrange multipliers, one constructs the function
 * $$\mathcal{L}\left(x_1 ,x_2 ,\dots,x_n ,\lambda\right)=U\left(x_1 ,x_2 ,\dots,x_n\right)+\lambda\cdot\left[W-\sum_{i=1}^n \left(p_i\cdot x_i \right)\right]$$

and finds the first-order conditions for optimization as
 * $$\frac{\partial\mathcal{L}}{\partial\lambda}=0$$

(which simply implies that all of $$W$$ will be spent) and
 * $$\frac{\partial\mathcal{L}}{\partial x_i}=0\forall i$$

so that
 * $$\frac{\partial U}{\partial x_i}-\lambda\cdot p_i =0\forall i$$

which is algebraically equivalent to
 * $$\frac{\partial U/\partial x_i}{p_i}=\lambda\forall i$$

Since every such ratio is equal to $$\lambda$$, the ratios are all equal one to another:
 * $$\frac{\partial U/\partial x_i}{p_i}=\frac{\partial U/\partial x_j}{p_j}~\forall\left(i,j\right)$$

(Note that, as with any maximization using first-order conditions, the equations will hold only if the utility function satisfies specific concavity requirements and does not have maxima on the edges of the set over which one is maximizing.)