Hamiltonians and Lagrangians for a given law of motion
The ability to derive a Lagrangian and Hamiltonian for a given 2nd order system of equations of motion is not automatic. So, this is not equivalent to Newton's second law or to systems whose equations of motion come ouf of that law. The extra conditions amount to what are known as Helmholtz's conditions.
If you start out with a given system q = (q1,q2,...,qN) with q''(t) = A(q(t),q'(t)), there are certain conditions that apply to the "acceleration function" A before the system can have a Lagrangian or Hamiltonian formulation.
These conditions give you the integrability conditions for certain partial differential equations whose solution ultimately gives you the Lagrangian in question (and from that, the Hamiltonian).
For a system of the form q'(t) = v(t), m v'(t) = Q(t), where m is a NxN matrix and Q an N-dimensional vector (the generalized coefficients of inertia and generalized force, respectively), a similar set of conditions can be established that m and Q must satisfy for a Lagrangian or Hamiltonian to exist. It's this form of the law of motion that Helmholtz's conditions are usually stated in terms of, though the (q'=v,mv'=Q) formulation is weaker than the (q''=A) formulation.
The (q''=A) formulation requires that there EXIST a matrix m ... that will ultimately be one and the same as the m in the 2nd formulation. It may depend on v = q' as well as q, but must satisfy the conditions
dm_{ab}/dv^c = dm_{ac}/dv^b; m_{ab} = m_{ba}
where a, b, c index the individual components. Once you have a suitable m, Q may be defined in terms of a by Q_a = sum (m_{ab} A^b). The remaining conditions are then those which apply (in either formulation) to Q:
dQ_a/dv^b + dQ_b/dv^a = -2 D m_{ab}
D (dQ_a/dv^b - dQ_b/dv^a) = 1/2 (dQ_a/dq^b - dQ_b/dq^a).
where
D = d/dt + sum (v^c d/dq^c)
(The derivatives above are partial derivatives)
(I'm doing this off the top of my head, so I might have the 1/2's, 2's switched around).
If you can either (a) find a set of coefficients m for the formulation (q''=A) that satisfy these 4 conditions; or (b) already have a set of coefficients (m,Q) in the formulation (q'=v,mv'=Q) that satisfy these conditions, then there is a set procedure to work backwards from here to get a Lagrangian. If the matrix m is non-singular, that determines whether a Hamiltonian can also be defined.
I won't take this too far, but will point out that because of the two conditions on m, Frobenius's theorem already guarantees you the ability to integrate the system
dp_a/dv^b = m_{ab}; p_a = dT/dv^a.
This "T" plays the analogous role of kinetic energy and will form the core of what will become the Lagrangian. If you substitute this into the first condition on Q (and perform various manipulations to make it symmetric in the a and b indices), you get a system of the form
dF_a/dv^b + dF_b/dv^a = 0
where F = Q + (other terms). This proves that F is at most linear in v and ius of the form
F_a = E_a(q) + sum (B_{ab}(q) v^b)
where B_{ab} = -B_{ba}. The 2nd condition on Q will prove that E, B are derivable from "potentials" much like the E and B maxwell fields would be
E_a = -d(phi)/dq^a - dA_a/dt
B_{ab} = dA_b/dq^a - dA_a/dq^a.
These can be used in the equation (F = Q + (other terms)), and the law of form mq'' = Q, ultimately to systematically build up the Lagrangian.
All of these points apply also field equations. But only the (q'=v,mv'=Q) formulation is available. Instead of there being just a single evolution variable (t), field equations depend on a set of coordinates (x1,x2,...,xn). So, the "velocity" components are indexed but just by the system's index "a,b,c...", but by a coordinate index, "m,n,p,..."; and the system for which a Lagrangian is to be derived, must have the form:
v^a_m = dq^a/dx^m
sum (m_{ab}^{mn} d(v^b_n)/dx^m) = Q_a
the latter sum taken over the indices b, m and n.
The conditions are similar
m_{ab}^{mn} = m_{ba}^{nm}
d(m_{ab}^{mn})/dv^c_p = d(m_{ac}^{mp})/dv^b_n
but more elaborate for Q:
dQ_a/dv^b_n + dQ_b/dv^a_n =
- sum D_m (m^{ab}_{mn} + m^{ba}_{mn})
and
D_n (dQ_a/dv^b_n - dQ_b/dv^a_n) = 1/2 (dQ_a/dq^b - dQ_b/dq^a)
the sum above is over the index m.
Once you have this, you can proceed to integrate to arrive at a Lagrangian density.
The process of building up a Lagrangian density and (if m is non-singular) a Hamiltonian density from this is more elaborate for the case of field equations than for the case of ordinary equations of motion.