Change in potential energy of a parallel plate capacitor as a dielectric slab is moved in the space between plates 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?
 A: I will give an answer in general terms for you to get the value of the force $F$ drawing the dielectric into the capacitor.  

The constant charge situation is easier and let the capacitor and the dielectric be the system.  
If the dielectric moves a distance $\Delta x$ the work done is $F \Delta x$ and the change in potential energy of the system is $\Delta U$ so $F\Delta x + \Delta U = 0$.  
Now $U = \frac 12 \frac{Q^2}{C}$ and so as the dielectric is drawn in, the capacitance increases and the potential energy decreases so $\Delta U$ is negative and $F\Delta x$ is positive - the force $F$  is in the direction of the displacement $\Delta x$  which is means that the force is inwards.  

Now consider the constant voltage regime with a battery connected to the capacitor and the dielectric system system.  
$U = \frac 12 C V^2$ and as the capacitance of the system increases as the dielectric goes further into the capacitor so does the potential energy stored in the system and if the sum of work done and energy is left here as you did, ie $F\Delta x + \Delta U = 0$, that would indeed imply that the force is repulsive because $F\Delta x$ would have to be negative.
Your error is omitting the fact that the battery has done work on the system equal to $V \Delta Q$ and so you now must write $F\Delta x + \Delta U = V\Delta Q$.  
When you do the sums you will find that $V \Delta Q =2\Delta U$ and so the final equation for this situation will again be $F\Delta x + \Delta U = 2\Delta U \Rightarrow F\Delta x = \Delta U$ which again gives an inward (attractive) force.
