MATLAB TUTORIAL. Part 1.2: RC&RL circuits

Prof. Vladimir A. Dobrushkin

This tutorial contains many matlab scripts.
You, as the user, are free to use all codes for your needs, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. Any comments and/or contributions for this tutorial are welcome; you can send your remarks to

Email Vladimir Dobrushkin

The fundamental passive linear circuit elements are the resistor (R), capacitor (C) and inductor (L) or coil. These circuit elements can be combined to form an electrical circuit in four distinct ways: the RC circuit, the RL circuit, the LC circuit and the RLC circuit with the abbreviations indicating which components are used. RC and RL are one of the most basics examples of electric circuits and yet they are very rich in content. The major difference between RC and RL circuits is that the RC circuit stores energy in the form of the electric field while the RL circuit stores energy in the form of magnetic field. Another significant difference between RC and RL circuits is that RC circuit initially offers zero resistance to the current flowing through it and when the capacitor is fully charged, it offers infinite resistance to the current. While the RL circuit initially opposes the current flowing through it but when the steady state is reached it offers zero resistance to the current across the coil. Let’s examine each one carefully.

RC circuits


Suppose that we wish to analyze how an electric current flows through a circuit. An RC circuit is a very simple circuit might contain a voltage source, a capacitor, and a resistor (see Figure). A battery or generator is an example of a voltage source. The glowing red heating element in a toaster or an electric stove is an example of something that provides resistance in a circuit. A capacitor stores an electrical charge and can be made by separating two metal plates with an insulating material. Capacitors are used to power the electronic flashes for cameras. Current, I(t), is the rate at which a charge flows through this circuit and is measured in amperes or amps (A). We assign a direction to the current. A current flowing in the opposite direction will be given negative values.


% Wires and Window
plot([-3;3],[-1;-1],'black')
hold
plot([1;3],[1;1],'black')
xlim([-5 5])
ylim([-2 2])
set(gca,'xtick',[])
set(gca,'ytick',[])
%Resistor
plot([-3;-0.8],[1;1],'black')
plot([0.8;1],[1;1],'black')
t = -0.8:1/1000:0.8;
r = 0.1*sawtooth(2*pi*3*t,0.5)+0.95;
plot(t,r,'black')

%Capacitor
plot([3;3],[1;0.05],'black')
plot([3;3],[-.05;-1],'black')
plot([2.7;3.3],[.05;.05],'black')
plot([2.7;3.3],[-.05;-.05],'black')

% circle
plot([-3;-3],[1;0.2],'black')
plot([-3;-3],[-0.2;-1],'black')
viscircles([-3 0],0.2,'Color','black')

The source voltage, E(t), is measured in volts (V). Kirchhoff's Second Law tells us that the impressed voltage in a closed circuit is equal to the sum of the voltage drops in the rest of the circuit. Thus, we need only compute the voltage drop across the resistor, ER, and the voltage drop across the capacitor, EC. According to Kirchhoff's Law, this is

\[ E_C + E_R = E . \]
Resistance, R, to the current is measured in ohms (Ω). Ohm's Law tells us that the voltage drop across a resistor is given by
\[ E_R = I\,R . \]
Finally, capacitance, C, is measured in farads (F). Coulomb's Law tells us how current flows across a capacitor,
\[ C\,\frac{{\text d} E_C}{{\text d} t} = I . \]
Thus, if we combine the above equations, our equation \( E_R + E_C = E \) becomes
\[ RC\,\frac{{\text d} E_C}{{\text d} t} + E_C = E(t) . \]

We will now investigate how our circuit reacts under different voltage sources. For example, we might have a zero voltage source (the capacitor could still hold a charge). We could also have a constant nonzero source of voltage such as a battery or a fluctuating source of voltage such as a generator. We might even have a series of pulses of voltage where the current is periodically turned on and off. We would like to be able to understand the solutions to the above differential equation for different voltage sources E(t). If we view the differential equation as an expression for computing how fast current is flowing across the capacitor, we can analyze our circuit from a geometric point of view and can actually say a great deal about circuits without solving a differential equation.

Example: We consider a simplest case when there is no voltage source in the circuit. In this case, the equation

\[ RC\,\frac{{\text d} E_C}{{\text d} t} + E_C = 0 \]
has explicit solution
\[ E_C (t) = E_C (0)\, e^{-t/(RC)} . \]

Example: If we assume that we have a nonzero constant source of voltage, E(t) = K, in our circuit such as a battery, then we obtain the separable differential equation

\[ RC\,\frac{{\text d} E_C}{{\text d} t} + E_C = K . \]
It has explicit solution
\[ E_C (t) = K\left( 1 - e^{-t/(RC)} \right) + E_C (0)\, e^{-t/(RC)} . \]

Example: If we attach a battery to our circuit at time t=0 and then disconnect the battery at t=5, then we obtain a piecwise continuous voltage function. For example, if

\[ E (t) = \begin{cases} 6, & \ \mbox{ if } 0 \le t \le 5 , \\ 0, & \ \mbox{ for } t > 5, \end{cases} \]
then we get the initial value problem
\[ RC\,\frac{{\text d} E_C}{{\text d} t} + E_C = E(t), \qquad E_C (0) = E_0 . \]
We can set E0 to zero because the corresponding solution was found in the first example. The direction field fo such differetial equation is
st = StreamPlot[{1, -y + 6}, {t, 0, 5}, {y, -3, 12}, PlotRange -> {{Full, 12}, Full}]
st5 = StreamPlot[{1, -y}, {t, 5, 12}, {y, -3, 12}, PlotRange -> {{Full, {5, 12}}, Full}]
Show[st, st5]
Now we show some solutions along with slope field:
EC[t_] = Piecewise[{{6, 0 < t < 5}}]
s1 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == 1}, y, {t, 0, 30}]
p1 = Plot[Evaluate[y[t] /. s1], {t, 0, 20}, PlotStyle -> {Thick, Red}]
s2 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == -2}, y, {t, 0, 30}]
p2 = Plot[Evaluate[y[t] /. s2], {t, 0, 20}, PlotStyle -> {Thick, Orange}]
s3 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == 10}, y, {t, 0, 30}]
p3 = Plot[Evaluate[y[t] /. s3], {t, 0, 20}, PlotStyle -> {Thick, Green}]
Show[st, st5, p1, p2, p3]

Example: Suppose our voltage source emits a series of pulses, say

\[ E (t) = \begin{cases} 6, & \ \mbox{ if } 0 \le t \le 4 , \\ 0, & \ \mbox{ if } 4 < t < 10, \\ 6, & \ \mbox{ if } 10 \le t \le 14 , \\ 0, & \ \mbox{ if } 14 < t < 20, \\ & \vdots . \end{cases} \]
Then we plot several solutions using NDSolve command. Although explicit formula is possibleto find, w postpone its derivation to chapter 6 when Laplace transform will be available.
EC[t_] = Piecewise[{{6, 0 < t < 4}, {0, 4 < t < 10}, {6, 10 < t < 14}, {0, 14 < t < 20}, {6, 20 < t < 24}}]
s1 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == 3}, y, {t, 0, 30}]
p1 = Plot[Evaluate[y[t] /. s1], {t, 0, 30}, PlotStyle -> {Thick, Red}]
s2 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == -2}, y, {t, 0, 30}]
p2 = Plot[Evaluate[y[t] /. s2], {t, 0, 30}, PlotStyle -> {Thick, Orange}]
s3 = NDSolve[{y'[t] + y[t] == EC[t], y[0] == 10}, y, {t, 0, 30}]
p3 = Plot[Evaluate[y[t] /. s3], {t, 0, 30}, PlotStyle -> {Thick, Green}]
Show[p1, p2, p3]

Example: Finally, we consider a generator for a voltage source that might be given by a function such as \( E(t) = \cos \left( 3\pi /2 \right) . \) The direction field for this circuit is given in Figure:

s1 = NDSolve[{y'[t] + y[t] == Cos[3*Pi*t/2], y[0] == 1}, y, {t, 0, 15}]
p1 = Plot[Evaluate[y[t] /. s1], {t, 0, 15}, PlotStyle -> {Thick, Red}]
s2 = NDSolve[{y'[t] + y[t] == Cos[3*Pi*t/2], y[0] == -2}, y, {t, 0, 15}]
p2 = Plot[Evaluate[y[t] /. s2], {t, 0, 15}, PlotStyle -> {Thick, Orange}]
s3 = NDSolve[{y'[t] + y[t] == Cos[3*Pi*t/2], y[0] == 5}, y, {t, 0, 15}]
p3 = Plot[Evaluate[y[t] /. s3], {t, 0, 15}, PlotStyle -> {Thick, Green}]
Show[st, p1, p2, p3]

RL circuits


A resistor–inductor circuit (or RL circuitfor short) is a circuit that consists of a series combination of resistance and inductance. Energy is stored in the magnetic field generated by a current flowing through the inductor. The induced emf opposes the flow of the current through it. RL circuit contains an inductor, which creates hysteresis and noise in the circuit. Since inductors are large in size, the corresponding RL circuits are bulky and expensive. They are appropriate for filtering of high power signals because of low power dissipation.

An inductor or coil represents the ‘electrical inertia’ of the circuit. As currents flows into the circuit, it generates a magnetic field, that change in the magnetic field causes a change in the flux of the field concatenated to the circuit, this in turn, by the Faraday-Neumann-Lenz law generates a voltage in the circuit that is opposite to the voltage that is generating the magnetic field. As a result, the current in the circuit will not immediately jump to its full value of V/R given by Ohm’s law. What we would expect is that the current will obey the following differential equation given by Ohm’s law at each point in time:

\[ V - L\, \frac{{\text d}I}{{\text d}t} = R\,I(t) , \]
where I is current (in amperes), V is voltage supply (in volts), L (in henries) is inductance, and R is resitence (in ohms). If V is constant, we solve the above differential equation to obtain
\[ I(t) = \frac{V}{R} \left( 1 - e^{- t/\tau} \right) , \]
where time constant τ = L/R.

 


function RC1()

% declare constants
C=input('Input the capacitance: ');
R=input('Input the resistance: ');
V=input('Input the battery voltage: ');
% declare first initial conditions
q0=0;
t0=0;

% solve ODE
[time1,Q1]=ode45(@(t,q) diffeq(t,q,C,R,V),[t0 10000],q0,odeset('RelTol',0.00001));

% find second initial conditions' index
for j=1:length(Q1)-1
    dQ1=Q1(j+1)-Q1(j);
    if abs(dQ1)<=0.0001
        index=j;
        break;
    end
end

% declare second intital conditions
q1=Q1(index);
t1=time1(index);
% battery disconnected
V1=0;
% solve ODE
[time2,Q2]=ode45(@(t,q) diffeq(t,q,C,R,V1),[t1 10],q1,odeset('RelTol',0.00001));

% find plot end index
for j=1:length(Q2)-1
    dQ2=Q2(j+1)-Q2(j);
    if abs(dQ2)<=0.0001
        plot_end=j;
        break;
    end
end

%create figure
figure('Name','HW1 #5','NumberTitle','off');
%plot first direction field
dirfieldplot(C,R,V,t0,t1,q0,q1,1);
hold on;
%plot second direction field
dirfieldplot(C,R,V1,t1,time2(plot_end),q1,Q2(plot_end),0);
hold on;
%plot function
plot(time1(1:index),Q1(1:index),'-',time2(1:plot_end),Q2(1:plot_end),'--');
% add title to function
title('Capacitor Charge vs. Time');
% add label to x axis
xlabel('t (s)');
% add label to y axis
ylabel('Q (C)');
% create gridlines
grid on;
%grid minor;
% hold plot
hold on;
end

function dQdt=diffeq(~,q,C,R,V)
%define diff eq
dQdt=(C*V-q)/(R*C);
end

% add direction field to graph of Q(t)
function dirfieldplot(C,R,V,t1,t2,q1,q2,c) % calls variables in script for function dirfield
[T q]=meshgrid(t1:(t2-t1)/20:t2,q1:(q2-q1)/20:q2);
dq=(C*V-q)/(R*C);
dT=ones(size(dq));
scale=sqrt(1+dq.^2);
quiver(T, q, dT./scale, dq./scale,.5,'Color',[0 1 c]);  % plots vectors at each point w direction
end

% input the C=.1, R=6, and V=12 when prompted in command window