The Rayleigh-Ritz variation method I am currently reading Atkins and Friedman's "Molecular Quantum Mechanics" (4th ed), looking at the Rayleigh-Ritz variation method. Starting from the Schrödinger equation $\hat{H}\psi = E \psi$, we get the "Rayleigh ratio"

$$
E = \frac{\int \psi^*\hat{H}\psi d\tau}{\int \psi^*\psi d\tau}
$$

Setting a trial function to be the following linear combination (assuming real coefficients)

$$
\psi_{trial} = \sum_i c_i\psi_i
$$

we find that

$$
E = \frac{\sum_{i,j} c_ic_j H_{ij}}{\sum_{i,j} c_ic_j S_{ij}}
$$

where $H_{ij} = \int \psi_{trial}^*\hat{H}\psi_{trial}d\tau$ and $S_{ij} = \int \psi_{trial}^*\psi_{trial}d\tau$.

Now the goal is to minimize the expression for $E$. We should therefore "differentiate with respect to each coefficient in turn and set $\partial E / \partial c_k = 0$ in each case". Using the quotient rule, I get the following

$$
\frac{\partial E}{\partial c_k} = \frac{ \sum_{i,j} c_ic_jS_{ij} \cdot \frac{\partial}{\partial c_k} \left( \sum_{i,j}c_ic_jH_{ij} \right)  }{\left( \sum_{i,j}c_ic_jS_{ij} \right)^2} - \frac{   \sum_{i,j} c_ic_jH_{ij} \cdot \frac{\partial}{\partial c_k} \left( \sum_{i,j}c_ic_jS_{ij} \right)   }{\left( \sum_{i,j}c_ic_jS_{ij} \right)^2} = 0
$$

However, I am not sure how to simplify this to obtain the correct expression, which is

$$
\frac{\partial E}{\partial c_k}_{Correct} = \frac{\sum_j c_j (H_{kj} - ES_{kj})}{\sum_{i,j} c_ic_jS_{ij}} + \frac{\sum_i c_i (H_{ik} - ES_{ik})}{\sum_{i,j} c_ic_jS_{ij}} = 0
$$

Clearly I can cancel a factor in the first term of my expression, but I don't know how to handle the derivatives with respect to $c_k$.
Instead of doing the whole derivation, I will provide the following two hints usefull in the derivation:


Use the quotient rule (as is was already done by OP).
$$
  \left({\frac {f}{g}}\right)'={\frac {f'g-fg'}{g^{2}}} \, ,
$$
where in this particular case $f = \sum_{i,j} c_i c_j H_{ij}$ and $g = \sum_{i,j} c_i c_j S_{ij}$.
Then, when taking the derivatives of the above mentioned $f$ and $g$, take into account that for each and every $c_i c_j X_{ij}$ term (where $X$ is either $H$ or $S$) there are only three possibilities:


$i = k$ in which case $\partial (c_i c_j X_{ij})/\partial c_k = c_j X_{kj}$;
$j = k$ in which case $\partial (c_i c_j X_{ij})/\partial c_k = c_i X_{ik}$;
$i \neq k$ and $j \neq k$ in which case $\partial (c_i c_j X_{ij})/\partial c_k = 0$.