I want to determine the charge in a capacitor. I didn't remember the formula for it, \$Q = C \cdot V\$, so I tried to derive it. This is how I went forwards. I've made the assumption that the capacitor has no initial charge. \$v = \frac{dw}{dq} \Leftrightarrow dw = v \space dq \Rightarrow \int dw = \int v \space dq \Rightarrow w = v \space q\$.
Since the energy stored in a capacitor is \$w = \frac{1}{2} C v_c^2\$, I tried to plug this in which results in the equation for charge: \$q = \frac{1}{2} C v_c\$.
This is half of the expected value, so where it the faulty logic here?
If I start by using the definition of current, I get the correct equation:
\$i = \frac{dq}{dt} \Leftrightarrow dq = i \space dt \$
\$i_c = C \frac{dv_c}{dt} \Leftrightarrow i_c \space dt = C \space dv_c\$
\$dq = C \space dv_c \Rightarrow q = C \space v_c\$