Professional Documents
Culture Documents
Use Matlab to plot the capacitor voltage and current for the circuit shown in Figure E2-1
below.
Figure E2-1
An RC circuit with a step input.
We assume that the capacitor is completely discharged for t < 0. When the switch closes,
the voltage across the capacitor is given by vc = 12(1 e t /( RC ) ) . Likewise, the capacitor
dv
current is given by ic = C c = (12 / R)e t /( RC ) .
dt
Since t and vc are both arrays of the same size they can be plotted against one another
with the simple statement plot(t, vc); The program also adds titles and labels to the
plots. The result of the program is two new figures called figure(1) and figure(2) which
are shown below in Figure E2-3.
Capacitor voltage
12
10
8
Volts
0
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
Time
Capacitor current
12
10
8
milliamps
0
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
Time
Figure E2-3
The voltage (top) and the current (bottom) for the capacitor in this example. These two
figures were produced by Matlab running the m-file shown in Figure E2-2.
Solution using vectors
For those who are accustomed to other programming languages the looping structure
used in this example is a natural way to create the solution. But Matlab executes loops
rather slowly and it provides a much cleaner way of solving this problem. In Matlab it's
possible to write a single equation that operates on a vector or a matrix instead of having
to put the vector in a loop and operating on its elements piecemeal. Thus in Matlab we
can write
t = 0:.0001:.01; %t is a vector from 0 to .01 in steps of .0001
vc = 12*(1-exp(-t/(R*C)));
ic = 12*exp(-t/(R*C));
No loop is necessary. Since t is a vector, Matlab will make vc and ic vectors and evaluate
the equation for each element of t and store the results in vc and ic. Matlab is much more
efficient at evaluating vectors than it is at doing loops. It's not always possible to avoid a
loop and make use of vectors but you should do so when you can. Figure E2-4 shows the
m-file using the vector operations. It produces the same results as before.
%Example2.m
R = 1000; %set the value of R and C as 1k and 1uf
C = 1e-6;
t = 0:.0001:.01; %t is a vector from 0 to .01 in steps of .0001
%use vector operations to find vc and ic.
vc = 12*(1-exp(-t/(R*C)));
ic = 12*exp(-t/(R*C));
figure(1);clf; %Assign a figure number and clear the figure
plot(t, vc); %plot time vs capacitor voltage
title('Capacitor voltage'); %Create a title for this graph
xlabel('Time'); %label the x axis
ylabel('Volts'); %label the y axis
figure(2);clf; %A second figure for the current
plot(t, ic); %plot time vs capacitor current on figure 2
title('Capacitor current'); %add titles and axis labels.
xlabel('Time');
ylabel('milliamps');
Figure E2-4
An m-file which evaluates the equations for the capacitor voltage and current as vector
equations instead of loops.
For this example we have just 2 plots so we can specify 2 rows and 1 column of subplots
for our figure. The new m-file to use subplots and the results are shown below.
%Example2.m
R = 1000; %set the value of R and C as 1k and 1uf
C = 1e-6;
t = 0:.0001:.01; %t is a vector from 0 to .01 in steps of .0001
vc = 12*(1-exp(-t/(R*C)));
ic = 12*exp(-t/(R*C));
figure(1);clf; %Assign a figure number and clear the figure
subplot(2, 1, 1); %specify subplots and choose the first
plot(t, vc); %plot time vs capacitor voltage
title('Capacitor voltage'); %Create a title for this graph
xlabel('Time'); %label the x axis
ylabel('Volts'); %label the y axis
subplot(2, 1, 2); %choose the second subplot in figure 1
plot(t, ic); %plot time vs capacitor current on
figure
title('Capacitor current'); %add titles and axis labels.
xlabel('Time');
ylabel('milliamps');
Figure E2-5
This version of the solution plots the results in subplots as shown in Figure E2-6.
Capacitor voltage
15
10
Volts
0
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
Time
Capacitor current
15
10
milliamps
0
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
Time
Figure E2-6
This is all one figure in Matlab but the figure has 2 rows and 1 column of subplots. The
top subplot shows the capacitor voltage vs time while the bottom subplot shows the
capacitor current vs. time.