%  This function solves homework problem [H9.7]
%  --------------------------------------------
%
echo on
diary nsds_hw9_7.dry
%
%  Set initial conditions
%
tf = 0.2;
dt = tf/100;
t = 0:dt:tf;
tol = 1e-5;
%
%  The current through the inductor serves as continuous state
%
iL0 = 0;
xc0 = iL0;
%
%  There are three discrete state variables:
%
%    m0 = 1 (true)  : diode is blocking
%         0 (false) : diode is conducting
%
%    m1 = 1 (true)  : thyristor control is on
%         0 (false) : thyristor control is off
%
%    ms = 1 (true)  : diode switch is on (s < 0)
%         0 (false) : diode switch is off (s >= 0)
%
m0 = 1;
m1 = 0;
ms = 0;
xd0 = [m0;m1;ms];
%
%  Insert initial time events into the event calendar
%
tp = 1/16.667;
ton0 = 30*tp/360;
toff0 = 45*tp/360;
global evt_cal
evt_cal = [];
push_evt(ton0,1);
push_evt(toff0,2);
%
%  Simulate using RKF4/5 with root solver
%
echo off
[y,xc,xd,tout] = rkf45rt(xc0,xd0,t,tol);
echo on
%
%  Plot the results
%
subplot(2,1,1)
plot(tout,y(1,:))
grid on
title('Homework [H9.7]')
xlabel('Time')
ylabel('vLoad')
subplot(2,1,2)
plot(tout,y(2,:))
grid on
xlabel('Time')
ylabel('iLoad')
%
print -deps nsds_hw9_7.eps
diary off
echo off
%
return