% Program to model RLC circuit with programmable input voltage Vs.

clear
C =.2;  % capacitor capacitance
L =.2;  % inductor inductance
R=.2;   % resistor resisance

vc(1) = 0;  % initial capacitor voltage
I(1) = 0;   % initial circuit current

Vs=zeros(1,1001);
Vs(101)=100;

t(1) = 0;
dt = .01;

for i=1:1000
    t(i+1) = t(i)+dt; 
    vc(i+1) = vc(i) + I(i)/C*dt;
    I(i+1) = I(i) + (Vs(i) - (vc(i) + I(i)*R)/L)*dt;
end
plot(t,vc)
xlabel('t')
ylabel('vc')
title('RLC oscillator, unit impulse input')

