When should I use $U=QV$ as opposed to $U=\frac{QV}2$? In my electricity course, I am having trouble understanding the difference in between $U=QV$ and $U=\frac{QV}2$ when talking about energy stored in a system. 
My idea was that when the potential is created by the charges arriving to the system, we would use $U=\frac{QV}2$, as the charges themselves are building the system potential as they arrive; on the other hand, when a potential is imposed from the outside, we would use $U=QV$.  
 A: As you said, if you have for example a particle of charge $q$ in an external electric potential $V$, then its energy is given by
$$E=qV$$
On the other hand, take a capacitor for example. The charge $Q$ that accumulates and the voltage across it $V$ satisfy the relation
$$Q=CV$$
where $C$ is the capacitance of the capacitor - merely a proportionality constant. Then if you charge the capacitor from $Q=0$ to $Q=q$, the energy you get is an integral over the infinitesimal contributions
$$E=\int_{0}^{q} V{\rm d}Q=\frac{1}{C}\int_{0}^{q}Q{\rm d}Q=\frac{q^{2}}{2C}=\frac{qV}{2}$$
I hope those concrete examples made it clearer.
A: You use $U=QV$ when V is being supplied by charges other than the one in your formula (we call these "external"). When you're talking about the the amount of energy stored in charges and the voltage is supplied by the same charges your asking about, then you use $U=\frac{QV}{2}$.
In either case, the correct formula is $U=\int_0^Q V \operatorname{d}q$. This is just a mathematical way of saying, "Add up the energy from building up the charge piece by piece (each piece is $\operatorname{d}q$)." When $V$ isn't changed by the charges you're adding, then it's a constant that can come outside of the integral, giving you $U=QV$. When you're adding charges to a capacitor, on the other hand, then the voltage and charge are related by $V= q/C$, and when you put that into the formula and do the integral, you get $U=\frac{Q^2}{2C} = \frac{QV}{2}$. For another example, if you can set up the situation somehow to make $V=kq^2$ then the energy would be $U=k \frac{Q^3}{3}$.
A: You have already given the correct answer in your question.
