% Program for calculating the RL circuit transient response
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
clear all; close all; clc;
global E2 R L tau
l_width = 2; f_size = 16;
E2=1.0; R=1.0; L=100e-3; tau=L/R; E1=-1;
i_L0 = E1/R;  % initial (conditions) inductor current
N=2^12; t0 = 0; tf = 500e-3; 
[t, i_Ldiff] = ode45('diff_RL',[t0 tf],i_L0); % solution with flexible step
t=t'; i_Ldiff=i_Ldiff';
i_Lana = E2/R+(i_L0-E2/R)*exp(-t/tau); % the analytical solution
% plot two solutions
figure('Name','RL Transient response','NumberTitle','off');
subplot(211), plot(t,i_Ldiff,'b',t,i_Lana,'r','linewidth',l_width)
set(gca,'FontSize',f_size),title('(a)'),
legend('i_{Lana}(t)','i_{Ldiff}(t)');grid on;
ylabel('Inductor current,i_{L}(t)');
subplot(212), plot(t,i_Ldiff-i_Lana,'linewidth',l_width);
set(gca,'FontSize',f_size), grid on, title('(b)'),
xlabel('Time [s]'), ylabel('i_{Lana}(t)-i_{Ldiff}(t)');
