A variational approach for an inverse dynamical problem for composite beams