In this case, we consider an infinitesimal slice of the bar and
perform a balance of forces on that slice (see Figure 1).

Figure 1. Continuum approach for the axial loading of a bar.

A balance of forces gives

$A{\boldsymbol {\sigma }}=\mathbf {q} (\mathbf {x} )~\Delta x+A({\boldsymbol {\sigma }}+\Delta {\boldsymbol {\sigma }})\qquad \implies \qquad A\left({\cfrac {({\boldsymbol {\sigma }}+\Delta {\boldsymbol {\sigma }})-{\boldsymbol {\sigma }}}{\Delta x}}\right)+\mathbf {q} (\mathbf {x} )=0~.$ Taking the limit as $\Delta x\rightarrow 0$ , we get

$A\lim _{\Delta x\rightarrow 0}\left({\cfrac {({\boldsymbol {\sigma }}+\Delta {\boldsymbol {\sigma }})-{\boldsymbol {\sigma }}}{\Delta x}}\right)+\mathbf {q} (\mathbf {x} )=0\qquad \implies \qquad {A{\cfrac {d{\boldsymbol {\sigma }}}{dx}}+\mathbf {q} (\mathbf {x} )=0~.}$ This is a differential equation written in terms of stress. We want to
express it in terms of displacements. How do we do this?

First, we convert the stresses into strains using the constitutive equation
(${\boldsymbol {\sigma }}=E{\boldsymbol {\varepsilon }}$ ).

${AE{\cfrac {d{\boldsymbol {\varepsilon }}}{dx}}+\mathbf {q} (\mathbf {x} )=0~.}$ Then, we convert the strains into displacements using the
strain-displacement relations :

${\boldsymbol {\varepsilon }}:=\lim _{l\rightarrow 0}{\cfrac {\Delta l}{l}}\qquad \implies \qquad {\boldsymbol {\varepsilon }}=\lim _{\Delta x\rightarrow 0}{\cfrac {(\mathbf {u} +\Delta \mathbf {u} )-\mathbf {u} }{\Delta x}}\equiv {{\boldsymbol {\varepsilon }}={\cfrac {d\mathbf {u} }{dx}}}~.$ The differential equation in terms of the displacement $\mathbf {u}$ is

${AE{\cfrac {d^{2}\mathbf {u} }{dx^{2}}}+\mathbf {q} (\mathbf {x} )=0~.}$ Since $\mathbf {q} (\mathbf {x} )=a\mathbf {x}$ , we have an inhomogeneous ordinary differential equation for the displacements in the bar.

${AE{\cfrac {d^{2}\mathbf {u} }{dx^{2}}}+a\mathbf {x} =0~.}$ If we can find the displacements in the bar, then we can solve for the strains and therefore the stresses. But to do that, we have to solve the governing differential equation .

To get a unique solution, we need to provide boundary conditions .

In this case, these are

At $\mathbf {x} =0$ (wall), $\mathbf {u} =0$ . This is an essential BC.
At $\mathbf {x} =L$ (end), $\mathbf {f} =\mathbf {R}$ . This is a natural BC. Since the ODE is in terms of $\mathbf {u}$ , the BCs must also be in terms of $\mathbf {u}$ . But we have a force at one end. This force has to be converted into an equivalent displacement. How?

$\mathbf {f} =A{\boldsymbol {\sigma }}=AE{\boldsymbol {\varepsilon }}=AE{\cfrac {d\mathbf {u} }{dx}}~.$ Therefore, the BCs become

At $\mathbf {x} =0$ (wall), $\mathbf {u} =0$ .
At $\mathbf {x} =L$ (end), $AE{\cfrac {d\mathbf {u} }{dx}}=\mathbf {R}$ . The problem can then be stated as

${\begin{aligned}{\text{Find}}~&\mathbf {u} (\mathbf {x} )~{\text{such that it satisfies}}\\&AE{\cfrac {d^{2}\mathbf {u} }{dx^{2}}}=-a\mathbf {x} \\&\mathbf {u} (0)=0\\&\left.{\cfrac {d\mathbf {u} }{dx}}\right|_{\mathbf {x} =L}={\cfrac {\mathbf {R} }{AE}}\end{aligned}}$
The differential formulation is also called the strong form of the
problem.

Analytical Solution Edit
The analytical solution strategy is as follows:

Set right hand side to zero and solve ( Homogeneous solution ).
Find one solution with RHS = $-a\mathbf {x}$ ( Particular solution ).
General solution = homogeneous + particular.
Apply BCs. Homogeneous solution Edit
The homogeneous ODE is
$AE{\cfrac {d^{2}\mathbf {u} }{dx^{2}}}=0~.$
Integrate twice to get the homogeneous solution
$AE\mathbf {u} =C_{1}\mathbf {x} +C_{2}\quad \implies \quad \mathbf {u} (\mathbf {x} )={\cfrac {C_{1}\mathbf {x} +C_{2}}{AE}}~.$ Particular solution Edit
The ODE is
$AE{\cfrac {d^{2}\mathbf {u} }{dx^{2}}}=-a\mathbf {x} ~.$
Integrate twice to get the particular solution (and assume that the constants of integration are zero)
$AE\mathbf {u} =-{\cfrac {a\mathbf {x} ^{3}}{6}}\quad \implies \quad \mathbf {u} (\mathbf {x} )=-{\cfrac {a\mathbf {x} ^{3}}{6AE}}~.$ General solution Edit
Add the homogeneous and particular parts to get
$\mathbf {u} (\mathbf {x} )={\cfrac {-a\mathbf {x} ^{3}+D_{1}\mathbf {x} +D_{2}}{6AE}}~.$ Apply BCs Edit
At $\mathbf {x} =0$ , $\mathbf {u} =0$ . Therefore, $D_{2}=0$ .
At $\mathbf {x} =L$ ,
${\cfrac {d\mathbf {u} }{dx}}={\cfrac {-3aL^{2}+D_{1}}{6AE}}={\cfrac {R}{AE}}\quad \implies \quad D_{1}=6R+3aL^{2}~.$
The displacement field in the bar is
${\mathbf {u} (\mathbf {x} )={\cfrac {-a\mathbf {x} ^{3}+(6\mathbf {R} +3aL^{2})\mathbf {x} }{6AE}}~.}$
The strain in the bar is
${{\boldsymbol {\varepsilon }}(\mathbf {x} )={\cfrac {-a\mathbf {x} ^{2}+(2\mathbf {R} +aL^{2})}{2AE}}~.}$
The stress in the bar is
${\boldsymbol {\sigma }}(\mathbf {x} )={\cfrac {-a\mathbf {x} ^{2}+(2\mathbf {R} +aL^{2})}{2A}}~.$ A plot of this solution is shown in Figure 2. All known quantities have been taken to be 1 for the plot.

Figure 2. Exact solution for the axial loading of a bar.