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$.