When a weak acid is very dilute, it is necessary to take into account the autoionization of water. We are going to write the two equilibria that take place in solution:

$H_2O + H_2O \rightleftharpoons H_3O^+ + OH^-$

x             x

$HA +H_2O\rightleftharpoons H_3O^+ + A^-$

M-y                        y              y

Both M, x and y are expressed in molarity. It must be taken into account that the concentration of protons in the medium is given by x+y.

We write the equilibrium constants:

$$K_w=[H_3O^+][OH^-]=(x+y)x$$

$$K_a=\frac{[H_3O^+][A^-]}{[AH]}=\frac{(x+y)y}{My}$$

Solving x from the equation of $K_a$:

$$x=\frac{K_a(My)}{y}-y$$

$$x+y=\frac{K_a(My)}{(MY)}$$

Replacing these values in the $K_w$:

$$K_w=10^{14}=\frac{K_a(My)}{y}\cdot\left[\frac{K_a(My)}{y}-y\right]$$

The value of y is obtained by solving the equation by the method of successive approximations.

Once y is obtained, its value is replaced in the equation of x. The sum x+y gives us the $[H_3O^+]$ and therefore the pH of the dilute weak acid solution.