# AC-DC Full-Wave Rectifier

#### Plot the input voltage vi and the voltage rectified by the diodes, vr.

 In[1]:= Xvi[t_] = Sin[t] ; vr[t_] = Abs[vi[t]] // PiecewiseExpand[#, t \[Element] Reals] &; Plot[{vi[t], vr[t]}, {t, 0, 16}]
 Out[1]=

#### When vr increases, vo[t]=vr[t]; it charges the capacitor and supplies current to the load. When vr starts to decrease (vr'[t]<0), the capacitor discharges through the load and the output voltage follows vo'[t]=vo[t]/cr.

 In[2]:= Xsystem = {vo'[t] == charging[t] vr'[t] - (1 - charging[t]) vo[t]/(r c), vo[0] == 0}; control = {charging[0] == 1, WhenEvent[vr'[t] < 0, charging[t] -> 0], WhenEvent[And[vo[t] < vr[t], charging[t] == 0, vr'[t] > 0], charging[t] -> 1]};

#### Simulate the system.

 In[3]:= Xpars = {r -> 0.1, c -> 1000}; sol = NDSolve[{system, control} /. pars, vo, {t, 0, 16}, DiscreteVariables -> charging \[Element] {0, 1}];

#### Plot the output voltage smoothened by the capacitor.

 In[4]:= XPlot[{vo[t] /. sol, vr[t]}, {t, 0, 16}]
 Out[4]=