Lagrangian formalism and collision with hard wall Assume that a particle with mass $m$ is colliding with a hard wall, making angle $\theta$ before and after the collision. Here are my questions: If we want to use Lagrangian formalism, should we write the Lagrangian as a step function, which accounts the motion before, at the time, and after the collision? Is it possible to derive the force exerted by hard wall as a generalized force? Is there a more general formalism for handling this kind of systems?
 A: In principle, you'd need to add a non-holonomic constraint.  If you are constraining your particle to the 2D region $y\geq 0$, then the appropriate Lagrangian would be
$$L = \frac{1}{2}(\dot x^2 + \dot y^2) + \lambda(y-s^2) $$
where I've set $m=1$, and $\lambda$ and $s$ are auxiliary parameters whose purpose is to enforce your constraint.  Note that they are in fact time-dependent functions which need to be considered when varying the action.
The equations of motion for this system are
$$\begin{align}x&:\quad \frac{d}{dt}(\dot x) = 0\\
y&:\quad \frac{d}{dt}(\dot y) = \lambda\\
\lambda&:\quad \frac{d}{dt}(0) = y-s^2\\
s&:\quad \frac{d}{dt}(0) = 2\lambda s\end{align}$$
From the equation for $\lambda$, we have that $y\geq 0$ (because $s$ is real) and that $s=\sqrt{y}$, which tells us that if $y>0$ then $\lambda=0$ so we have a free particle.  However, if $y=0$ then $\lambda$ is arbitrary, and we can see that it's actually the force being exerted by the wall.
If we start the ball at the point $(0,1)$ with velocity $\dot x = 0, \dot y= -1$, the equations of motion can trivially be solved up until the point where $y=0$ to give
$$y(t) = 1 - t, \qquad t<1$$
At the point $t=1$, we have that $y(t)=0$ and $\dot y(t) = 0$ (where the latter can be obtained by differentiating e.g. the $\lambda$ equation with respect to time).
By introducing a non-holonomic constraint, we have made the velocity discontinuous.  This is a problem, because the principles underlying our variational techniques assume that the curves are nice and smooth$^\dagger$. Our only real option is to accept that the curve will be singular at the moment the particle touches the wall and see if we can salvage a solution for later times.  The answer will be yes if, for all $t>1$, we have $\dot y \geq 0$, which can formally be accomplished by letting $\lambda(t) = \lambda_0 \delta(t-1)$ where $\lambda_0 \geq 1$.
Having made this generalization, the solution to the equations of motion becomes
$$x(t) = 0$$
$$y(t) = \begin{cases}1-t & t<1 \\ (\lambda_0-1)(t-1) & t>1\end{cases}$$
In this solution, $\lambda_0$ is still arbitrary.  If it's equal to 1, then the particle simply sticks to the wall.  If it's between $1$ and $2$, the particle rebounds with less energy than it started with.  If it's equal to $2$ then the particle rebounds elastically, and if it's greater than $2$ the particle rebounds superelastically, with more energy than it started with.
Just as in Newtonian mechanics, you'll need to provide this information to obtain a solution.  You can see, however, that Lagrangian mechanics really isn't cut out for constraints of this kind, and you have to bend over backward in most cases in order to accommodate them.

$^\dagger$Strictly speaking, this is not quite true; it is only the variations in the curves which need to be smooth.  However, the EL equations will need to be interpreted in a weak sense (i.e. including step functions and delta functions).  A generic way to solve such equations is to solve them in parts - one prior to the collision and one after the collision - and join them together afterward.
