DC-DC Buck Converter
Model a DC-to-DC buck converter from input voltage level vi to desired lower output voltage level vd using a pulse-width modulated feedback control q[t].
system = {vo'[t] == -vo[t] / (r c) + i[t] / c , i'[t] == -vo[t] / l + q[t]vi / l, vo[0] == 0, i[0] == 0};control = {q[0] == 1, WhenEvent[Mod[t, τ] == (vd / vi)τ, q[t] -> 0], WhenEvent[Mod[t, τ] == 0, q[t] -> 1]};pars = {vi -> 24, vd -> 16, r -> 22, l -> 2 10 ^ -2, c -> 4 10 ^ -6, τ -> 2 10 ^ -4};sol = NDSolve[{system, control} /. pars, {vo, i, q}, {t, 0, .01}, DiscreteVariables -> q, MaxSteps -> Infinity];Row[{Plot[{vo[t] /. sol, vd /. pars}, {t, 0, .01}, PlotRange -> All, PlotLegends -> {"vo(t)", "vd"}], Spacer[50], Plot[q[t] /. sol, {t, 0, .001}, AxesLabel -> {t, q[t]}]}]