A generalization of the method of harmonic balance