Dynamic analysis and control of microgrids