A method of controlling and modulating a converter having N phases connected to an electrical system, the converter comprising controllable switches to form discrete output voltages, wherein the method comprises determining controlled variables, providing a mathematical representation of the electrical system comprising the converter, providing reference values for the controlled variables, the reference values being constant values for a duration of a modulation period or linearly changing values in the modulation period, determining possible N-phase switching sequences in the modulation period, the switching sequence defining the order in which the switches are switched at N switching time instants, each N-phase switching sequence having one switching transition per switching time instant, and each phase having one switching transition per modulation period, calculating gradients of controlled variables for the modulation period based on the mathematical representation of the electrical system, for each possible N-phase switching sequence, minimizing the error between the provided references and the corresponding controlled variables at least at the N switching time instants based on the calculated gradients by optimizing the switching time instants of the N-phase switching sequence by solving a constrained optimization problem such that the error between the provided references and the corresponding controlled variables is minimized at the optimized switching time instants, the optimization problem being subject to the constraints that the switching time instants are positive and occur consecutively in a predetermined order within the modulation period, selecting the N-phase switching sequence with the smallest error, and applying the N-phase switching sequence with the corresponding switching time instants in the modulation period to modulate the controllable switches. determining controlled variables, providing a mathematical representation of the electrical system comprising the converter, providing reference values for the controlled variables, the reference values being constant values for a duration of a modulation period or linearly changing values in the modulation period, determining possible N-phase switching sequences in the modulation period, the switching sequence defining the order in which the switches are switched at N switching time instants, each N-phase switching sequence having one switching transition per switching time instant, and each phase having one switching transition per modulation period, calculating gradients of controlled variables for the modulation period based on the mathematical representation of the electrical system, for each possible N-phase switching sequence, minimizing the error between the provided references and the corresponding controlled variables at least at the N switching time instants based on the calculated gradients by optimizing the switching time instants of the N-phase switching sequence by solving a constrained optimization problem such that the error between the provided references and the corresponding controlled variables is minimized at the optimized switching time instants, the optimization problem being subject to the constraints that the switching time instants are positive and occur consecutively in a predetermined order within the modulation period, selecting the N-phase switching sequence with the smallest error, and applying the N-phase switching sequence with the corresponding switching time instants in the modulation period to modulate the controllable switches. <EOS>