Transforming to non-intertial frames in Hamiltonian Mechanics I have a translating potential $V(q,t) = V(q-x(t))$ (i.e. a potential which is following some trajectory $x(t)$) that I can write down the Lagrangian for, $\mathcal{L}=T_q-U_q=\frac{1}{2}m\dot{q}^2-V(q-x(t))$. I now want to transform to the coordinate $Q=q-x(t)$. I can arrive at the appropriate equations of motion for this system as derived in this question, which shows that there arises a pseudo-force $mQ\ddot{x}(t)$. In the Lagrangian Formalism, I get where the pseudoforce comes from. 
My issues is that I would now like to transform from a rest frame to a translating frame which follows $x(t)$, and write down the Hamiltonian. This should be identical to a particle in the static potential with some time-dependent force acting on it (where the time dependent force here depends on $\ddot{x}(t)$), a result I can arrive at if I work in a Newtownian frame, write down the equation of motion, and regroup things into a new potential with a time dependent homogeneous force. However, for completeness sake I want to be able to go from the Lagrangian formalism to the Hamiltonian formalism, which is how I feel this should actually be done.
If I carry out the analysis as in the question I linked earlier and define a new Lagrangian $L=\frac{1}{2}m\dot{Q}^2-mQ\ddot{x}(t)-V(Q)$, then this has the correct form to be transformed into a Hamiltonian in $Q,P$ coordinates as I would like (and is what I find working in a Newtownian formalism and then using $H=PQ-L$). However $L = T_Q-U_Q\neq \frac{1}{2}m(\dot{Q}+\dot{x(t)})^2-V(Q) = \frac{1}{2}m\left(\dot{Q}^2+2\dot{Q}\dot{x(t)}+\dot{x(t)}^2\right)- V(Q)$, which only has first order derivatives on $x(t)$.  
I have also tried to use the Hamiltonian EOM, since there is $\frac{\partial\mathcal{H}}{\partial t}= - \frac{\partial\mathcal{L}}{\partial t}$ and the derivatives eliminate everything but a $\dot{Q}\dot{x(t)}$ term, which when I integrate by parts to get $\mathcal{H}(t)$ gives me $Q\ddot{x(t)}+f(Q,P)$ after discarding an integral that should be a constant on the ground of it appearing in the action integral of the Lagrangian which is stationary and therefor constant. However, I am slightly reluctant to do this, as it seems like it may not be entirely rigorous and I don't want to just cherry pick what appears to give the correct answer.
I know that there is a justification for either re-defining the Lagrangian and more rigorously transforming from $\mathcal{L}$ to $L$, or a subtlety in the Legendre transformation that actually derives the Hamiltonian that I am missing. I believe this arises from the fact that I am working in a non-inertial frame, as otherwise $\dot{x}(t)=const$ and $\dot{Q}=\dot{q}$.
 A: Focusing on the theory of your specific case, and avoiding the more general discussions, here is how one could derive the Hamiltonian after a change of (curvilinear) coordinates. The beauty of Lagrangian mechanics is that it is covariant with respect to any change of coordinates, both related to inertial and non-inertial alike. This fact comes from the principle of least action formulation. If $L$ is the Lagrangian of a system (the Lagrangian doesn't have to be unique; as long as the critical value equation is the same for two different Lagrangians, they describe the same dynamics) the action functional associated with $L$ is
$$S[q] = \int_{t_1}^{t_2} L\big(q(t), \dot{q}(t), t\big) dt$$ for curves $q(t)$ defined for $t\in [t_1,t_2]$ such that $q(t_1) = q_1$ and $q(t_2) = q_2$ are two fixed points. Then for an arbitrary one parameter family of curves $q(t,s)$ (the so called variation of the curves) fixed at $q_1$ and $q_2$ we get
$$S[q](s) = \int_{t_1}^{t_2} L\big(q(t,s), \partial_t {q}(t,s), t\big) dt$$  so the critical curves, which are the trajectories of the dynamics, should satisfy the "zero functional gradient" condition, also known as principle of least action
$$\delta S[q] = \frac{\partial}{\partial s} \, S[q](s)\,{\Big|_{s=0}} = 0$$ which is equivalent to the Euler-Lagrange equations
$$\frac{d}{dt}\, \left(\frac{\partial L}{\partial \dot{q}}\big(q,\dot{q},t\big) \right)= \frac{\partial L}{\partial {q}}\big(q,\dot{q},t\big) $$ Therefore, if you change the coordinates $q,t$ to $Q,\tau$ of the integral one-form
 ${L}\big(q,\dot{q},t\big)dt$  to obtain a new one form  $\tilde{L}\big(Q,\dot{Q},\tau\big)d\tau$, where $\dot{Q} = \frac{d Q}{d\tau}$, the integral and thus $ S[q] =  S[Q]$ so $\delta S[q] = \delta S[Q] = 0$ which means that the equations $$\frac{d}{dt}\, \left(\frac{\partial L}{\partial \dot{q}}\big(q,\dot{q},t\big) \right)= \frac{\partial L}{\partial {q}}\big(q,\dot{q},t\big) \,\,\, \text{ and } \,\,\, \frac{d}{d\tau}\, \left(\frac{\partial \tilde{L}}{\partial \dot{Q}}\big(Q,\dot{Q},\tau\big) \right)= \frac{\partial \tilde{L}}{\partial {Q}}\big(Q,\dot{Q},\tau\big) $$ describe the same solutions but in different coordinates and possibly parametrized differently. In your case however, $\tau = t$ so it is enough to change the variables from $q$ to $Q$, while keeping the time $t$ parametrization the same, of the Lagrange function ${L}\big(q,\dot{q},t\big)$  to obtain the Lagrange function   $\tilde{L}\big(Q,\dot{Q},t\big)$ in the new coordinates.
In your case the change of variables is $Q = f(q,t)$, so $$\dot{Q} = D_q f(q,t) \dot{q} + \partial_t f(q,t)$$ thus
$${L}\big(q,\dot{q},t\big) = \tilde{L}\Big(f(q,t),\, D_q f(q,t) \dot{q} + \partial_t f(q,t), \, t\Big)$$ The Euler-Lagrange equations with $L$ turn into the equations with $\tilde{L}$ and you are done (of course, you are allowed to manipulate the new Lagrangian $\tilde{L}$ by integrating by parts in the action $S[Q]$, if possible, to get an equivalent Lagrnagian, but that is not necessary). 
Now, the Hamiltonian picture. Recall the duality between Lagrangians and Hamiltonians:
\begin{align*}
{L}\big(q,\dot{q},t\big) &= p\cdot\dot{q} - H\big(q,p ,t\big)\\
\tilde{L}\big(Q,\dot{Q},t\big) &= P\cdot\dot{Q} - \tilde{H}\big(Q,P,t\big)
\end{align*} Since $${L}\big(q,\dot{q},t\big) = \tilde{L}\big(Q,\dot{Q},t\big) = \tilde{L}\Big(f(q,t),\, D_q f(q,t)\dot{q} + \partial_t f(q,t), \, t\Big) $$ we get that 
$$ p\cdot\dot{q} - H\big(q,p ,t\big) = P\cdot\dot{Q} - \tilde{H}\big(Q,P,t\big)$$ Moreover, for the conjugate momenta we have
\begin{align}
p &= \frac{\partial L}{\partial \dot{q}}\big(q,\dot{q},t\big)\\
P&=\frac{\partial \tilde{L}}{\partial \dot{Q}}\big(Q,\dot{Q},t\big)
\end{align} so for $p$ we have
$$p =\frac{\partial}{\partial \dot{q}} L\big(q,\dot{q},t\big) = \frac{\partial}{\partial \dot{q}} \tilde{L}\Big(f(q,t),\, D_q f(q,t)\dot{q} + \partial_t f(q,t), \, t\Big) = \Big(D_qf(q,t)\Big)^*\frac{\partial \tilde{L}}{\partial \dot{Q}} = \Big(D_qf(q,t)\Big)^* P$$ where the $*$ superscript means transposed of the linear transformation $D_qf(q,t)$ so  $$P = \Big(D_qf(q,t)^*\Big)^{-1} p$$ Thus
\begin{align}
 p\cdot\dot{q} - H\big(q,p ,t\big) &= P\cdot\dot{Q} - \tilde{H}\big(Q,P,t\big)\\
&= \Big( \, \Big(D_qf(q,t)^*\Big)^{-1} p \Big) \cdot\Big( D_q f(q,t)\dot{q} + \partial_t f(q,t)\Big) - \tilde{H}\Big(f(q,t),P,t\Big)\\
&=  p \cdot\Big(  \big(D_qf(q,t)\big)^{-1} \big(\, D_q f(q,t)\dot{q} + \partial_t f(q,t) \, \big)\Big) - \tilde{H}\Big(f(q,t),P,t\Big)\\
&= p \cdot\Big(\dot{q} +\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big) - \tilde{H}\Big(f(q,t),P,t\Big)\\
&=  p \cdot \dot{q} + p \cdot \Big(\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big) - \tilde{H}\Big(f(q,t),P,t\Big)\\
&=  p \cdot \dot{q} - \Big[ \, \tilde{H}\Big(f(q,t),P,t\Big) -  p \cdot \Big(\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big) \, \Big] 
\end{align} which after cancelling the common terms $p\cdot \dot{q}$ on both sides of the equation yields $$ H\big(q,p ,t\big) = \tilde{H}\Big(f(q,t),P,t\Big) -  p \cdot \Big(\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big)$$
$$\tilde{H}\Big(f(q,t),P,t\Big) = H\big(q,p ,t\big) +   p \cdot \Big(\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big)$$
$$\tilde{H}\Big(f(q,t), \big(D_qf(q,t)^*\big)^{-1} p, t\Big) = H\big(q,p ,t\big) +   p \cdot \Big(\big(D_qf(q,t)\big)^{-1}  \partial_t f(q,t) \, \big)\Big)$$ $$\tilde{H}\Big(f(q,t), P, t\Big) = H\big(q,p ,t\big) +    \Big( \big(D_qf(q,t)^*\big)^{-1}  p \Big) \cdot \Big(\partial_t f(q,t) \, \big)\Big)$$ $$\tilde{H}\Big(f(q,t), P, t\Big) = H\big(q,p ,t\big) +   P \cdot \Big(\partial_t f(q,t) \, \big)\Big)$$ This is where the link between the two Hamiltonians come from. One can phrase it in terms of generating functions of canonical transformations. Let $G(q,P,t) = P \cdot f(q,t)$. Then $$Q = \frac{\partial G}{\partial P}\big(q, P, t\big) = f(q,t)\, ,\,\,\,\,\,\, p = \frac{\partial G}{\partial q}\big(q, P, t\big)$$  $$\frac{\partial G}{\partial t}\big(q,P,t\big) =
 \frac{\partial}{\partial t} \big( P \cdot f(q,t) \big) = P \cdot \partial_t
 f(q,t)$$
 Thus the identity between the two Hamiltonians becomes
 $$\tilde{H}\Big(f(q,t), P, t\Big)
 =
 H\Big(q,  \frac{\partial G}{\partial q} ,t\Big) +
 P \cdot \Big(\partial_t f(q,t) \, \big)\Big)$$
 $$\tilde{H}\left(\frac{\partial G}{\partial P}, P, t\right)
 =
 H\left(q, \frac{\partial G}{\partial q} ,t\right) +
 \frac{\partial G}{\partial t}$$
In your case $Q = f(q,t) = q - x(t)$ so
 $$G\big(q, P, t\big) = P \cdot \big(q - x(t)\big) =
 P \cdot q - P \cdot  x(t)$$ and thus $$P = p \,\,\,\, Q = q - x(t)$$
 Let me put $m=1$ for simplicity.
 The original Hamiltonian is $$H = \frac{1}{2} p^2 +
 V\big(q-x(t)\big)$$ and
 $$\frac{\partial G}{\partial t} =
 \frac{\partial }{\partial t} \, P \cdot \big(q - x(t)\big)
 = - P \cdot \dot{x}(t)$$
 Thus $$\tilde{H} = \frac{1}{2} P^2 +
 V\big(Q\big) - P \cdot \dot{x}(t)$$ which yields the Hamiltonian
 equations
 \begin{align*}
\dot{Q} &=  P - \dot{x}(t)\\
\dot{P} & = - \nabla\, V(Q)
 \end{align*} so
 $$\ddot{Q} = \dot{P} - \ddot{x}(t) =  - \nabla\, V(Q)  - \ddot{x}(t)$$
 $$\ddot{Q}  + \ddot{x}(t) = - \nabla\, V(Q) $$ which hare the
 Euler-Lagrange equations of the Lagrangian
 $$\tilde{L} = \frac{1}{2}\big(\dot{Q} + \dot{x}(t)\big)^2 - V(Q)$$
A: Let us assume we have two Lagriangians $L_1(Q,\dot{Q},t)$ and $L_2(Q,\dot{Q},t)$ as well as a function say $K(Q,t)$ such that the following identity holds:
$$L_2(Q,\dot{Q},t) = L_1(Q,\dot{Q},t) + \frac{\partial K}{\partial Q}(Q,t)\dot{Q} + \frac{\partial K}{\partial t}(Q,t)$$ Then, observing that $$ \frac{\partial K}{\partial Q}(Q,t)\dot{Q} + \frac{\partial K}{\partial t}(Q,t) =  \frac{d }{dt} K(Q,t)$$ we can rewrite the first identity above as
$$L_2(Q,\dot{Q},t) = L_1(Q,\dot{Q},t) +  \frac{d }{dt} K(Q,t)$$ Then the actions are
$$S_1[Q] = \int_{t_1}^{t_2} L_1\big(Q(t),\dot{Q}(t),t\big)dt $$ and
\begin{align}
S_2[Q] &= \int_{t_1}^{t_2} L_2\big(Q(t),\dot{Q}(t),t\big)dt \\ &= \int_{t_1}^{t_2} \Big( \, \,  L_1\big(Q(t),\dot{Q}(t),t\big) + \frac{\partial K}{\partial Q}\big(Q(t),t\big)\dot{Q}(t) + \frac{\partial K}{\partial t}\big(Q(t),t\big) \,\, \Big)dt\\
&= \int_{t_1}^{t_2} \Big( \, \,  L_1\big(Q(t),\dot{Q}(t),t\big) +   \frac{d }{dt} K\big(Q(t),t\big) \,\, \Big)dt\\
&= \int_{t_1}^{t_2}  L_1\big(Q(t),\dot{Q}(t),t\big) dt +  \int_{t_1}^{t_2} \frac{d }{dt} K\big(Q(t),t\big) \, dt\\
&= \int_{t_1}^{t_2}  L_1\big(Q(t),\dot{Q}(t),t\big) dt +  \int_{t_1}^{t_2} \frac{d }{dt} K\big(Q(t),t\big) \, dt\\
&= S_1[Q]  +  \int_{t_1}^{t_2} \frac{d }{dt} K\big(Q(t),t\big) \, dt\\
&= S_1[Q]  +  K\big(Q(t_2),t_2\big) -  K\big(Q(t_1),t_1\big)
\end{align}
To get the critical trajectories (the dynamical trajectories) of these actions, we need to go over all possible one-parameter families of curves $Q(t,s)$ such that $Q(t_1,s) \equiv Q_1$ and $Q(t_2,s) \equiv Q_2$ (fixed endpoints), form the variation of the functionals
$$S_j[Q](s) = \int_{t_1}^{t_2} L_j\big(Q(t,s),\dot{Q}(t,s),t\big)dt$$ for $j=1,2$ and find the critical (zero) variation equations
$$0 = \delta S_j[Q] = \frac{\partial}{\partial s} \, S_j[Q](s){\big|_{s=0}} $$ which are (equivalent to) the Euler-Lagrange equations. Consequently,
$$0 = \delta S_2[Q] = \delta S_1[Q] + \delta K(Q_2,t_2) - \delta K(Q_1,t_1) = \delta S_1[Q]$$ because $\delta K(Q_2,t_2) = \frac{\partial}{\partial s} \, K(Q_2,t_2) = 0$ because $K(Q_2,t_2)$ does not depend on $s$ due to the fact that $Q_2$ is a fixed end. Same for $K(Q_1,t_1)$. The identity
$$ 0 = \delta S_2[Q] = \delta S_1[Q]$$ means that the Euler-Lagrange equations of motion (equations for critical trajectories of the functionals) are the same despite the fact that the Lagriangians $L_1$ and $L_2$ are different.
In your case, say $$L_1 = \frac{1}{2} \dot{Q}^2  -   {Q}\cdot \ddot{x}(t)  - V(Q) $$ and \begin{align} L_2 &= \frac{1}{2} \dot{Q}^2 + \dot{Q}\cdot \dot{x}(t) + \frac{1}{2}\dot{x}(t)^2 - V(Q)\\
  &= \frac{1}{2} \dot{Q}^2 + \dot{Q}\cdot \dot{x}(t) + \frac{1}{2}\dot{x}(t)^2 - V(Q) + {Q}\cdot \ddot{x}(t) -   {Q}\cdot \ddot{x}(t)\\ 
&= \frac{1}{2} \dot{Q}^2  -   {Q}\cdot \ddot{x}(t)  - V(Q) + \Big( \, {Q}\cdot \ddot{x}(t)  + \dot{Q}\cdot \dot{x}(t)  + \frac{1}{2}\dot{x}(t)^2 \, \Big)\\ &= L_1 + \Big( \, {Q}\cdot \ddot{x}(t)  + \dot{Q}\cdot \dot{x}(t)  + \frac{1}{2}\dot{x}(t)^2 \, \Big) \\
&= L_1 + \frac{d}{dt}\Big( \, {Q}\cdot \dot{x}(t) + \frac{1}{2} \int_{t_0}^{t} \dot{x}(\tau)^2 d\tau \, \Big)\\
&= L_1 + \frac{d}{dt} K(Q,t) \end{align}  where 
$$K(Q,t) = {Q}\cdot \dot{x}(t) + \frac{1}{2} \int_{t_0}^{t} \dot{x}(\tau)^2 d\tau $$
In general, I would be very careful in using a Lagrangian of type kinetic minus potential energy in a non-inertial frame. In general, it's safer to start with a Lagrangian of type kinetic minus potential energy in a inertial frame and then change to a non-inertial one. In particular, 
if you change from inertial frame of reference to a more general non-inertial Cartesian frame, the change should look like $$Q = A(t) q + x(t)$$ where $A(t)$ is a time dependent rotation matrix. Then, the change of variables in the Lagrangian, performed according to the analysis from the previous and the current post, will reveal the presence of fictitious forces in the Euler-Lagrange equations written in terms of non-inertial variables.   
