%% EE221 HW 13 Problem 4

t = (0:.10e-6:100e-6).'; % time

delay1 = 10e-6; % delay for rising edge
delay2 = 20e-6; % delay for falling edge
VDD = 3; % Power
u1 = VDD*ones(length(t),1); % Allocate Memory for Unitstep
u2 = VDD*ones(length(t),1);

% Making a unitstep
k = 0;
i = 1;
u1(1,1) = 0;
while k < delay1
   u1(i+1,1) = 0;
   i = i+1;
   k = t(i+1,1);
end

k = 0;
i = 1;
u2(1,1) = 0;
while k < delay2
   u2(i+1,1) = 0;
   i = i+1;
   k = t(i+1,1);
end

% Circuit components
L = 15e-3;
R = 1.5e3;

T = L/R; % Time konstant
Vin = u1-u2; % Input Voltage

% The output VL
VL = (1-1.*exp(-(t-delay1)./T)).*u1 - (1-1.*exp(-(t-delay2)./T)).*u2;

% The answer to prob 4
Vacross = Vin - VL;

figure(1)
subplot(2,1,1)
plot(t,Vin,'-b','linewidth',3)
title('The Input of the LR Circuit','fontname','Comic Sans MS')
xticks([10e-6 70e-6 100e-6])
xticklabels({'10 \mus','70 \mus','100 \mus'})
prettyGraph
subplot(2,1,2)
plot(t,VL,'-r','linewidth',3)
xticks([10e-6 70e-6 100e-6])
xticklabels({'10 \mus','70 \mus','100 \mus'})
prettyGraph

figure(2)
plot(t,Vacross,'-g','linewidth',3)
title('Voltage Across Inductor','fontname','Comic Sans MS')
xticks([10e-6 70e-6 100e-6])
xticklabels({'10 \mus','70 \mus','100 \mus'})
ylabel('Voltage \it(V)\rm')
xlabel('Time \it(\mus)\rm')

prettyGraph

%% Functions 

function prettyGraph
% This creates pretty graphs
grid on              %
ax = gca;            
ax.FontSize = 11;
ax.LineWidth = 2;
set(gcf,'color','w');
end

