How to plot a capacitor charging and discharging

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

 Accepted 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.

More Answers (1)

hi,
Since you are using separate variables which doesn't have any common values you might be getting separate lines. Try this at the end, you can remove 'hold on' also
t=[t1,t2,t3,t4,t5,t6];
vf = [v1,v2,v3,v4,v5,v6];
plot(t,vf)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!