I was doing this question:
If a parallel plate capacitor made of square plates of side L and separation D is connected to voltage source V. And then a dielectric slab of width D is introduced into the space between the plates to a distance x, then the change in its potential energy with respect to x is
$${\displaystyle dU = + (K-1)\epsilon_{0} \frac{V^{2}L}{2D}dx}$$
and since this is stored in capacitor as the work done by the force due to the charges on the plate on the slab, so the force
$${\displaystyle \vec{F} = - \frac{dU}{dx} \hat{x} }$$
Which gives,
$${\displaystyle \vec{F} = - (K-1)\epsilon_{0} \frac{V^{2}L}{2D}\hat{x} }$$
But this is incorrect because when dielectric is partially inserted in capacitor the charges on the plates will try to pull it in the space of capacitor and not outward.
However if the voltage source is disconnected, then the change in potential energy with respect to x will be
$${\displaystyle dU = - (K-1)\epsilon_{0} \frac{V^{2}L}{2D}dx}$$
Which gives,
$${\displaystyle \vec{F} = (K-1)\epsilon_{0} \frac{V^{2}L}{2D}\hat{x} }$$
Which corresponds to correct result.
It's written in the question that in the first case potential difference remains constant across the plates and in the second case, the charges on the plates remain constant.
But I can't understand why is it so? And even if I agree on that then why the two cases gave completely different results?