A method is presented to construct nonconvex free energies that are invariant under a symmetry group. Algebraic and geometric methods are used to determine invariant functions with the right location of minimizers. The methods are illustrated for symmetry-breaking martensitic phase transformations. Computer algebra is used to compute a basis of the corresponding class of invariant functions. Several phase transitions, such as cubic-to-orthorhombic, are discussed. An explicit example of an energy for the cubic-to-tetragonal phase transition is given.