Dynamic response of mechanical systems by a weak Hamiltonian formulation