% Simulation of an RC (series) circuit CHARGING
% See section 5-4 in Ulaby
clear all % just becuase I am always messing up if I don't!
Vs = 10 % (V) source voltage
R = 1000 % (ohms) series resistance
C = 1e-6 % (farads) series capacitance

%DC value, capacitor is open
I0 = 0 % initial current
V0 = Vs % initial voltage

%RC time constant
tau = R*C % seconds

% maximum simulation time & resolution
tmax = 8*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)*exp(-t(i)/tau); %(amps) (see eqn 5.83 in Ulaby)
V(i) = Vs*(1-exp(-t(i)/tau)); % (volts) (see eqn 5.78 in Ulaby)
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,I) %plot xaxis,yaxis
title('Current through RC circuit')
xlabel('time (seconds)')
ylabel('current (amps)')
% Plot voltage and label your plot
figure; %new figure
plot(t,V) %plot xaxis,yaxis
title('Voltage through RC circuit')
xlabel('time (seconds)')
ylabel('voltage (V)')