function for deriving equations of motion