You are on page 1of 4

Example 2 RC Transient Plot

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

Solution using loops


To create a plot in Matlab we evaluate these two equations in an m-file for a range of
values of time and plot the result. Matlab provides for several plot options and there are
several ways to arrange the program itself. Since you are familiar with the C
programming language the most obvious solution would be to write a simple for loop to
evaluate the equations. Figure E2-2 shows an m-file which does this.
%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 a loop to evaluate the voltage and current equations
for i=1:length(t);
vc(i) = 12*(1-exp(-t(i)/(R*C)));
ic(i) = 12*exp(-t(i)/(R*C));
end
figure(1);clf; %Assign a figure number and clear 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 fig 2
title('Capacitor current'); %add titles and axis labels.
xlabel('Time');
ylabel('milliamps');
Figure E2-2
An m-file named Example2.m which uses a for loop to evaluate and plot the capacitor
voltage and current against time in two different figures.
Note that the line t = 0:.0001:.01; creates an array named t which has 101 values
ranging from 0 to .0001 in steps of .01. Also note that in Matlab no variables need to be
declared before they are used. Even arrays can be used without declaring them or
assigning them a dimension. On occasion it is necessary to declare an array and give it a
size and you can use the zeros functions to do this, but for the most part, Matlab is able to
figure out the variable type and array size from the context in which the variable is used.

The loop given by


for i=1:length(t);
vc(i) = 12*(1-exp(-t(i)/(R*C)));
ic(i) = 12*exp(-t(i)/(R*C));
end
is a standard for loop structure which creates two new arrays called vc and ic. It fills
these arrays with the values of the capacitor voltage and the capacitor current from the
equations which it evaluates.

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.

Other plot options - Subplots


Matlab provides subplots and other options that allow a user to put more than one plot on
a figure. The subplot function takes three arguments that specify the number of rows and
columns of subplots to be shown and the number of the currently selected subplot. For
example subplot(3,2,1) specifies that the current figure should have 3 rows by 2
columns of subplots and that we have currently selected the first subplot. The subplots
are numbered from left to right and top to bottom beginning with 1.

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.

You might also like