% Simulation of an RL (series) circuit CHARGING  
% See http://hyperphysics.phy-astr.gsu.edu/hbase/electric/indtra.html
clear all % just becuase I am always messing up if I don't!
Vs = 12 % (V) source voltage
R = 1000 % (ohms) series resistance
L = 1e-3 % (henries) series inductance

%initial value
I0 = 0 % initial current
V0 = Vs % initial voltage

%RL time constant
tau = L/R % seconds (Ulaby equation 5.104)

% maximum simulation time & resolution
tmax = 4*tau % seconds
dt=tau/10 % seconds 
imax = round(tmax/dt) % round to nearest integer

i=1% index for vectors
for i=1:imax
    t(i)=i*dt; % (seconds) Note that I am using semicolon to prevent showing on the screen
    I(i) = (Vs/R)*(1-exp(-t(i)/tau)); %(amps)
    V(i) = Vs*(exp(-t(i)/tau)); % (volts)
    i=i+1; % index for vectors
end

% to make figures with bigger labels and line widths
set(0,'DefaultAxesFontSize',18)
set(0,'DefaultLineLineWidth',2)
set(0,'DefaultTextFontSize',18)

% Plot current and label your plot
figure;%new figure
plot(t*1e6,I) %plot xaxis,yaxis
title('Current through RL circuit charging')
xlabel('time (microseconds)')
ylabel('current (amps)')
% Plot voltage and label your plot
figure; %new figure
plot(t*1e6,V) %plot xaxis,yaxis
title('Voltage through RL circuit charging')
xlabel('time (microseconds)')
ylabel('voltage (V)')