Indeed you have the option of actually performing the integration as stated in (1).
$$ S = \int\left(\frac{1}{2}m\dot{x}^2 - \frac{1}{2}kx^2\right)dt.\tag{1} $$
That is to say:
The only way to actually evaluate that integral is by evaluating a trajectory that you have in the form of position as a function of time. That is, to evaluate the integral you must already have the solution to the problem.
Of course that means that this is only for exploration, to understand what Hamilton's stationary action is about. If you don't have the solution at your disposal then of course there is no way to evaluate the integral. It's just that the case of Hooke's law is mathematically straightforward; we already know the solution: harmonic oscillation. we can use the cases where we already known the solution as a window into how Hamilton's stationary action works
The standard approach is to set up differentiation of Hamilton's action with respect to the variation.
For simplicity of presentation: let the variation be implemented with a single factor $p$.
We are looking for the point where the derivative of Hamilton's action with respect to $p$ is zero:
$$ \frac{d(S)}{dp} = 0 \tag{2} $$
For the RHS of (1):
$$ \frac{\int\left(\frac{1}{2}m\dot{x}^2 - \frac{1}{2}kx^2\right)dt}{dp} \tag{3} $$
The standard approach is to rework (3) in such a way that it is no longer necessary to actually evaluate the integral. The final result of that reworking is the Euler-Lagrange equation.
Your question is: what happens if we keep the integration?
In my opinion: keeping the integration is very instructive.
We have that both integration and differentiation are linear operations, so we can perform the differentiation separately:
$$ \frac{\int\frac{1}{2}m\dot{x}^2dt}{dp} - \frac{\int\frac{1}{2}kx^2dt}{dp} \tag{4} $$
I recommend to first implement the case of a linear potential:
While the expression for the kinetic energy is for all cases the same (proportional to the square of the velocity) the potential energy is generally different case by case.
In the case of a linear potential the response of $\int E_p(p)dt$ to sweeping out variation will be linear in $p$
The response of the $\int E_k(p)dt$ to sweeping out variation is a given: a quadratic function of $p$
There is a single point in variation space such that the values of $\tfrac{\int E_p(p)dt}{dp}$ and $\tfrac{\int E_k(p)dt}{dp}$ match each other.
The point in variation space where those two match each other coincides with the true trajectory.
(Hooke's law is a special case. It is the one case where the potential energy has the same response to sweeping out variation as the kinetic energy: quadratic response.)
Discussion:
why the result is independent of the order of operations
Both integration and differentiation are linear operations.
About the variation:
Notice especially that with Hamilton's stationary action the variation is exclusively variation of position coordinate.
As a consequence: differentiation with respect to variation boils down to differentiation with respect to position coordinate.
That again confirms that the integration and the differentiation are independent. Integration with respect to time; differentiation with respect to position coordinate.
For actual implementation:
I recommend implementing two different potentials:
- linear potential: the potential increases linear with displacement
- quadratic potential (Hooke's law), the potential that give rise to harmonic oscillation.
(The reason for choosing these two: for both the evaluation of the integral is straightforward.)
On my own website I implemented that with a graphics library called JSXGraph.
If you want to implement something like that then to get started I think the DESMOS environment is a good fit. With DESMOS you can declare an expression, with one or more of the variables in that expression adjustable with a slider. Move the slider and the graph changes in response. That way you can use the slider input to sweep out variation.
In order to actually evaluate the integral you have to choose a start point and an end point.
I recommend that you choose limits so as to make the integration straightforward.
For the linear potential:
Starting height: zero height
End height: zero height
Choose a duration that fits well with the given potential
(For instance, I implemented an acceleration of 2 $m/s^2$, because then a duration of 2 units of time results in a trajectory that has the ballistic object climb to 1 unit of height and then fall back down again.)
For the quadratic potential:
Choose the potential such that the period of the oscillation is $2\pi$ units of time.
Then, to make the integration straightforward, perform the integration over half a period of that oscillation, from starting height zero to end height zero.
On my own website I used the true trajectory as seed for variation, with the variation implemented as linear variation.
So in the case of the ballistic trajectory (parabola) I implemented a variational parameter $p$, and I multiplied that parabola with a factor $(1+p)$, so that when $p$ is zero the variation is zero.
I recommend that you implement three plots:
- The value of the integral of the kinetic energy as a function of the variational parameter $p$
- The value of the integral of the potential energy as a function of the variational parameter $p$
- The value of $\int E_k(p) dt - \int E_p(p) dt $
About the standard approach:
That approach as well capitalizes on the property that the integration and the differentiation are independent.
Repeating (3)
$$ \frac{\int\left(\frac{1}{2}m\dot{x}^2 - \frac{1}{2}kx^2\right)dt}{dp} \tag{5} $$
The standard approach is to move the differentiation inside the integration.
(Think of it in this way: the Euler-Lagrange equation is the necessary core; the Euler-Lagrange equation is sufficient to solve the problem. The process of deriving the Euler-Lagrange equation is a process of removing all superfluous elements.)
(A link to my website is available on my stackexchange profile page.)