MATLAB: How to plot a capacitor charging and discharging

capacitorcapacitor chargingcapacitor dischargingrc circuit

I'm trying to plot the charging and then discharging of a capacitor in a simple RC circuit. I need it to be 3 complete cycles a 1kHz. The code I wrote just gives me 6 separate line segments. I can see why I'm not getting the plot that I want but I don't know how to get the plot that I want. Could anyone help me out with this please? Here is my code. I have all of the component values listed in the code. Like I said, it's pretty obvious why it's wrong. I just have no idea how to make it right.
t1=0:0.00001:0.005;
t2=0.005:0.00001:0.01;
t3=0.01:0.00001:0.015;
t4=0.015:0.00001:0.02;
t5=0.02:0.00001:0.025;
t6=0.025:0.00001:0.03;
v=5;
r=10e3;
c=10e-7;
tau = r*c;
v1=v.*exp(t1/tau);
hold on
v2=v.*exp(-t2/tau);
hold on
v3=v.*exp(t3/tau);
hold on
v4=v.*exp(-t4/tau);
hold on
v5=v.*exp(t5/tau);
hold on
v6=v.*exp(-t6/tau);
hold on
plot(t1,v1,t2,v2,t3,v3,t4,v4,t5,v5,t6,v6)
hold on

Best Answer

Thank you. That did get me to one continuous plot. I have some serious work to do with the domains but I think I can handle that. Thanks.
Related Question