Derivation of work done by capacitor charging and discharging