How to calculate the work done by a capacitor