% Plot of oscillator waveform across capacitor.
% R. Kozick

R = 1e3;
C = 1e-6;
tau = R*C;

Ra = 1e3;
Rb = 1e3;

T = 2*R*C*log(1+2*Rb/Ra);  % Period

t1 = linspace(0,T/2,201);
t2 = linspace(T/2,T,201);

K = (Ra + 2*Rb)/(Ra + Rb);
vc1 =  10 - 10*K*exp(-t1/tau);
vc2 = -10 + 10*K*exp(-(t2-T/2)/tau);
ax = [0 t2(length(t2)) -10 10];

figure(1)
clf
plot(t1, vc1, 'r-', t2, vc2, 'r-', ...
    t1, 10*Rb/(Ra+Rb), 'b:', t2, 10*Rb/(Ra+Rb), 'b:', ...
    t1, -10*Rb/(Ra+Rb), 'g:', t2, -10*Rb/(Ra+Rb), 'g:')
xlabel('Time (s)')
ylabel('v_c(t) (V)')
s = sprintf('R = %g, C = %g, Ra = %g, Rb = %g, T = %g (ms)', R, C, Ra, Rb, T*1000);
title(s)
axis(ax)

return

vc1a = 10 - 10*K*(1-t1/tau);
vc2a = -10 + 10*K*(1-(t2-T/2)/tau);

figure(2)
clf
plot(t1, vc1, 'r-', t2, vc2, 'r-', ...
    t1, 10*Rb/(Ra+Rb), 'b:', t2, 10*Rb/(Ra+Rb), 'b:', ...
    t1, -10*Rb/(Ra+Rb), 'g:', t2, -10*Rb/(Ra+Rb), 'g:', ...
    t1, vc1a, 'k--', t2, vc2a, 'k--')
xlabel('Time (s)')
ylabel('v_c(t) (V)')
s = sprintf('R = %g, C = %g, Ra = %g, Rb = %g, T = %g (ms)', R, C, Ra, Rb, T*1000);
title(s)
axis(ax)
