Verifying formula for Work;
$$W = \int \vec{F} \cdot d\vec{x} \quad(i)$$
Let us consider a very simple scenario; I will solve first by simple Maths and then by calculus.
Scenario 1: (Force vector is always $5\vec{i} N$) [Solving by simple maths,]
A block of mass 2kg is initially at origin($\vec{x}=0\vec{i}$) is displaced to $\vec{x}=8m\vec{i}$
$W_{1}=\vec{F} \cdot \vec{s} \quad$ ; where $\quad \vec{s}=displacement \ vector$
$W_{1}=5*8* \cos(0^{\circ})=40 J$
$-------------------------------------------------$
Scenario 2: (Force vector is always $5\vec{i} N$) [Solving by simple maths,]
A block of mass 2kg is initially at $x=8m\vec{i}$ is displaced to origin ($\vec{x}=0\vec{i}$)
$W_{2}=\vec{F} \cdot \vec{s} $
$W_{2}=5*8* \cos(180^{\circ})=-40 J$
Work came negative as displacement and force are in opposite direction, here force is opposing in nature like frictional force.
$------------------------------------------------$
Now solving same above two scenarios using calculus $$W = \int \vec{F} \cdot d\vec{x} \quad(i)$$
Scenario 1C: (Force vector is always $5\vec{i} N$)
A block of mass 2kg is initially at origin($\vec{x}=0\vec{i}$) is displaced to $\vec{x}=8m\vec{i}$
$$W = \int \vec{F} \cdot d\vec{x} $$ $$W = \int^8_0 F dx\cos(0^{\circ}) $$ $$W_{1c}=F(8-0)=40$$
$-------------------------------------------------$
Scenario 2C: (Force vector is always $5\vec{i} N$)
A block of mass 2kg is initially at $\vec{x}=8m\vec{i}$ is displaced to origin($\vec{x}=0\vec{i}$).
$$W = \int \vec{F} \cdot d\vec{x} \quad(i)$$ $$W = \int^0_8 F dx\cos(180^{\circ}) $$ $$W_{2c}=-F(0-8)=40J$$
But, previously we obtained $W_2=-40J$ $-------------------------------------------------$
Why are answer for $W_{2}$ and $W_{2C}$ different though they represent same scenario?
This question is simplified form for confusion in Potential energies for Gravity and Electrostatics.
For Sign convention I read this also Potential energy sign conventions