Inserting dielectric slab into a capacitor I studied that inserting the slab into a capacitor which is connected to a battery is difficult and we have to do the work, and inserting the slab into a disconnected capacitor is easy and we don't have to do any work. is it right? if it is right then how the direction of the force on slab in both situation differs? I thought that in both situations slab is attracted by the capacitor and we don't have to do the work in both situations. why the capacitor is pushing the slab outside in first situation?  
 A: 
how the direction of the force on slab in both situation differs?

Recall that the energy stored in a capacitor is given by
$$W = \frac{1}{2}CV^2 = \frac{1}{2}\frac{Q^2}{C}$$
where $V$ is the voltage across the capacitor and $Q$ is the magnitude of the electric charge on either plate.
For the case that a constant voltage source (e.g., battery) is connected across the capacitor, $V$ is fixed and so the stored energy is proportional to the capacitance; the stored energy increases as a dielectric is inserted (since the capacitance increases due to the dielectric).
However, in the case that the (charged) capacitor is disconnected, $Q$ is fixed and so the stored energy is inversely proportional to the capacitance; the stored energy decreases as a dielectric is inserted.
Thus, at this point, we might conclude that the direction of the force on the slab differs since the 'direction' of the change in stored energy differs.
But, in the case that a battery is connected, we must also consider the change in energy of the battery.  Assume that, after the dielectric is inserted, the capacitance is increase by a factor of $\beta \gt 1$:
$$C' = \beta C$$
Recalling $Q = CV$ and that $V$ is fixed, we have
$$Q' = C'V = \beta CV = \beta Q$$
The work done by the battery in 'pumping' charge $(\beta - 1)Q$ at constant voltage $V$ is
$$W_\mathrm{BAT} = (\beta - 1)QV$$
The change in the energy stored in the capacitor is
$$\Delta W = \frac{1}{2}\left(C'V^2 - CV^2\right) = \frac{1}{2}(\beta - 1)CV^2  = \frac{1}{2}(\beta - 1)QV$$
Thus, the battery lost twice as much energy as the capacitor gained; the change in energy of the system is
$$\Delta W_\mathrm{SYS} = - \frac{1}{2}(\beta - 1)QV$$
In the case of the isolated, charged capacitor, the change in the energy is
$$\Delta W = \frac{1}{2}\left(\frac{Q^2}{C'} -  \frac{Q^2}{C}\right) = \frac{1}{2}\left(\frac{1}{\beta} - 1 \right)QV = -\frac{1}{2}\left(\frac{\beta - 1}{\beta}\right)QV$$
In summary, in both cases, the system loses energy and so the dielectric slab is attracted in both cases.
A: You are correct, force associated with potential energy is given by $-\frac{\partial U}{\partial x}$, net force on dielectric is zero since the process is quasistatic and now you can calculate work done by external force. Remember there  maybe energy losses in this process.
