Calculating the energy for a capacitor A capacitor connected to a voltage got charged. After it has charged, it was taken off and put in a different circuit with another capacitor that is uncharged in parallel in the absence of a voltage. Then they both were charged. When we calculate the energy, first in the case of where there was voltage and second in the case of where the voltage was absent and uncharged capacitor was present the energy of the first is greater than the second. My teacher reasoned that it was due to spark created when we separated and connected the capacitor. We had used the formula 1/2CV^2 to calculate the energy for both. In the second we used the equivalent capacitor of the two capacitors connected in parallel [C-eq=c-1+c-2]. The charged capacitor had less capacitance than the uncharged when it was plugged into the new circuit.The charges were conserved. My question, How did the formula know to calculate the correct energy? Is it how it is drived? And can one assume absolute none resistance and no sparks and calculate an energy that is equal to the prior one(similar to the first circuit)? Or is there, like thermodynamics, no 100% efficient circuit, if there is such a thing, how do one reason mathematically?
 A: This is a problem that often causes conceptual difficulties. You have two separated capacitors with capacitances $C_1$ and $C_2$, the charges $Q_1$ and $Q_2$, and thus the voltages $V_1=Q_1/C_1$ and $V_2=Q_2/C_2$. $V_1$ is (initially) higher than $V_2$. The sum of potential energies $U_A$of these capacitors is $$ U_A=\frac{C_1 V_1^2}{2}+\frac{C_2 V_2^2}{2}=\frac{Q_1 V_1}{2}+\frac{Q_2 V_2}{2} \tag{1}$$ Then you connect them in parallel. The charges combine to $Q_1+Q_2$, the capacitances add to $C=C_1+C_2$, and a common voltage $V$ is established $$V=\frac{Q}{C}=\frac{Q_1+Q_2}{C_1+C_2}=\frac{C_1V_1+C_2V_2}{C_1+C_2} \tag{2}$$ $V$ is a weighted mean of the voltages $V_1$ and $V_2$. Therefore for $V$ always holds $$V_1 \ge V\ge V_2 \tag{3}$$ Then the potential energy  $U_B$ of the combined capacitor with the sum of the charges and capacitances becomes $$ U_B=\frac {C V^2}{2}=\frac {VQ}{2}=\frac{(C_1V_1+C_2V_2)(Q_1+Q_2)}{2(C_1+C_2)} \tag{4}$$ One can easily verify that the electrostatic potential energy $U_B$ of the capacitors combined in parallel is always smaller than or equal to the sum of potential energies $U_A$ of the separated capacitors $$U_B \le U_A$$ This is rather perplexing, because the law of energy conservation should also hold in this case. So where has the energy difference $$W =U_A-U_B$$ gone to? 
To understand this, it is important to initially consider this as a purely electrostatic problem of moving charges in a conservative electrostatic field from one conductor with potential $V_1$ to another conductor with lower potential $V_2$ until both conductors are at the same potential $V$ given by (2). When moving a small charge element $dQ$ from a conductor with potential $V_1$ to a conductor with a lower potential $V_2$, an energy $dW$ is released $$dW=dQ[V_1(Q_1)-V_2(Q_2)]$$ which corresponds to the work done on the charge by moving it the opposite way. This energy $dW$ can be put out in any form compatible with the conservation of energy. Thus it can be mechanical kinetic energy imparted to the object that transfers the charge, it can be another form of potential mechanical energy like a spring, or lifting a mass in the gravitational field, it can also be other forms of electromagnetic energy like EM waves, or chemical energy. It can, of course, also be heat, but this is only one of many options! 
Because the field is conservative, the path of this charge transfer is completely arbitrary. Thus you can move $dQ$ in the first capacitor from the electrode at potential $V_1$ to the other at potential $0$ and then move it in the other capacitor from the electrode at potential $0$ to the electrode with potential $V_2$. And you can continue doing this until the voltage (potential) of the first capacitor becomes $V_1=V$ and the voltage of the second becomes $V_2=V$. Thus at the end of the repetitive movements of small charges $dQ$ you can join the plates (electrodes) of the two capacitors in parallel with no further charge transfer because the plate potentials are equal $V_1=V_2=V$. 
By this you can see that the total energy $W$ released by the charge movements leading to total charge transfer is $$W= \frac{C_1 (V_1-V)^2}{2}-\frac{C_2 (V-V_2)^2}{2}$$ which according to equations (1) and (4) is $$W=U_A-U_B$$ 
The conclusion is that the joining of the two charged capacitors in parallel corresponds to the situation where electrostatic potential energy is  freed by a transport of charges from a conductor at higher potential to a conductor at lower potential. This potential energy decrease can be transformed into any other (kinetic or potential) energy form, not only heat, according to the laws of conservation of energy.
Added later: In two capacitors joined by  a resistor the energy $W$ will be mostly transformed to Joule's heat. If you join two plate capacitors (or coaxial capacitors) with perfect conductors, you will get two joined transmission lines with TEM electromagnetic wave fronts bouncing back and forth laterally between the ends of the capacitor with a little damping by radiation loss. With good conductors you also get such waves with additional resistive Joule damping by the currents in the conductors. In sparks you have a partial transformation into light energy.    
A: In pure circuit theory, i.e. without any wires, resistors or inductivities, it is a contradiction to connect 2 capacitors with different voltage. A (loaded) capacitor is equivalent to an ideal voltage source in the first infinite short time of connection. So the connection of both C's with different voltage u1 and u2  is like connecting 2 ideal voltage sources with different voltage u1 and u2 - at the very first moment. In this very first moment the contradiction is:  the potential between the 2 sides is defined to be u1 and at the same time u2. 
So the scenario is not possible in theory, unless u1=u2. 
Many explanations are based on heat or wave emissions to explain the loss of energy. But the connection of the 2 capacities is not a connection of (each) 2  wires. The connection in a circuit diagram means "=", a pure mathematical equivalence without any wires involved. Both Capacitors have instantly the same potential as no "wire" or time delays are involved, resulting in the contradiction. In reality there is always a finite length of wire having some inductivity, capacity and resistance.
The circuit theory does not imply the current to be transported via discrete charges as well.
To make it more clear, a circuit diagram is just another way to describe (mostly) differential and integral equations, with initial values at t0. In our case, we have two initial values: voltage of C1= u1, of C2= u2. If you connect both capacitors, remember this connection does only mean "=", it will yield a contradiction, since we have u1<>u2 and at the same time (at t=t0) we set u1=u2. It would mathematically  be the same situation as to define a variable  x(t0) with  x(t0)=u and x(t0)<>u, and then to calculate the time domain function of  x = x(t) for t>= t0. In reality, there are always inductivities and resistances involved, if 2 capacitors are connected. Thats why there is no contradiction anymore, since the 2 capacitors are no more strictly parallel, but instead define a complex network of Ls, Rs,Cs, where both capacitors are only connected via other elements.
