0% found this document useful (0 votes)
482 views

Graphing The Voltage Across An LRC Circuit Using Python

This document summarizes a study that aimed to graph voltages across an RLC circuit using Python. The researcher used a numerical method to solve the RLC circuit differential equation by discretely calculating the derivative of voltage over time. The numerical solution was compared to an analytical solution to check for accuracy. While the numerical method showed distorted amplitudes and phase shifts compared to the analytical results, the researcher concluded it had potential with improvements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
482 views

Graphing The Voltage Across An LRC Circuit Using Python

This document summarizes a study that aimed to graph voltages across an RLC circuit using Python. The researcher used a numerical method to solve the RLC circuit differential equation by discretely calculating the derivative of voltage over time. The numerical solution was compared to an analytical solution to check for accuracy. While the numerical method showed distorted amplitudes and phase shifts compared to the analytical results, the researcher concluded it had potential with improvements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Graphing The Voltages Across an RLC Circuit

Using Python
Jose Enrico V. Maxino

Department of Physics, University of San Carlos

Abstract

This study aimed to graph the voltages across an RLC circuit using Python. For this task, the
researcher used a method of calculating the derivative of a line graph discreetly. This
numerical method was used to solve the RLC circuit differential equation (equation 1). The
method is a powerful tool because it exploits the computing power of modern computers.
And if it's proven to be accurate, it can then be used for complicated wave forms such as
sound recordings or electromagnetic signals. But for the purposes of this paper, a simple
sinusoid was used as the waveform. It represented voltage from an AC power source. To
get a benchmark for the correctness of the numerical method, equation 1 was also solved
analytically. The analytical solution proved to be correct when its results were compared
with the source voltage using the relationship between source and RLC voltages found in
equation 1. Thus it was then used as a benchmark for the numerical method. The result
voltages of the numerical method was discovered to have distorted amplitudes and phase
shifts which made it incorrect when compared with the analytical results. Thus, correction
factors were introduced to correct the amplitudes. But due to time constraints and lack of
personnel, a solution was not found for the phase shift. Nevertheless, the researcher
concluded that the numerical method had a lot of potential for dissecting different
frequencies of a signal and recommended further improvements for future studies.

Key terms

For loop - a control flow statement for specifying iteration, which allows code to be
executed repeatedly.

List - an abstract data type that represents a countable number of ordered values, where
the same value may occur more than once.

Python - an interpreted, high-level, general-purpose programming language.

Introduction

An RLC circuit is an electrical circuit consisting of a resistor, an inductor, a capacitor, and a


voltage source connected in series. Series RLC circuits have applications in radio and
communication engineering. They can be used to select a certain narrow range of
frequencies from the total spectrum of ambient radio waves. In an RLC circuit, each
component has a voltage across it which is related to the material of the component, the the
amount of charge present in the circuit (and how it changes with time), and the source
voltage. And if those voltages are added, they will be equal to the source voltage. This
relationship is expressed in the RLC circuit differential equation (equation 1). The voltage
across each component varies with time. In this experiment, the researcher aims to
simulate the instantaneous voltage across each component of an RLC circuit. This will
require solving the 2nd order differential equation of an RLC circuit. A numerical method for
solving the ODE is used. This method exploits the high computing power of modern
computers and the Python programming language. The ODE will also be solved analytically
to serve as a benchmark for possible corrections of the numerical method. The solutions of
both methods will be compared to check the accuracy of the numerical method.
Limitations of the Study

The aim of this study is simply to try a numerical method for solving the LRC circuit DE.
This means presenting the derivative of a graph by computing the slope of lines between
any two consecutive data points. The derivative is defined as the slope between two
infinitely close points. Since the distance between two data points is not infinitely small,
but rather very small, the numerical method used for getting the derivative will not be
exactly accurate. But the precision of this method is enough for the purpose of
understanding the properties of an LRC circuit.

The analytical solution can only work for simple AC voltages of the form or
. Where is the maximum voltage and is the angular frequency. This is because
it was solved assuming that the voltage source is a simple sinusoid. For more complex
waveforms such as sound recordings, the analytical solution can’t give correct values.

The code for the analytical solution will not work if:

Where:

R = resistance of resistor

C = capacitance of capacitor

L = inductance of inductor

If R is not sufficiently greater than L and C, the code will encounter an imaginary number
which it cannot operate on (for more details, see methodology). The solution for such an
event exists and it can be included on the code, making the code about twice longer. But
due to lack of time and personnel, the researcher decided to leave it as a limitation of the
analytical solution.

Methodology

Deriving The Analytical Solution

The equation for voltage across an LRC circuit is:

equation 1

Where:

● Voltage across inductor: equation 2

● Voltage across resistor: equation 3

● Voltage across capacitor: equation 4


● Voltage source=
● q=charge on the circuit as a function of time
● L,R,C,V, and are constants

By finding q as a function of time, we can then differentiate it to get and . By plugging


these expressions into equations 2,3 and 4, we can know the voltages across each

component at any time. The expression for q, and solved through operator method
is:
equation 5

equation 6

equation 7

Where:

The constants on the equation are built upon each other. This is to simplify the code
because if they are expressed purely on L,R,C,V, and , the code will be unnecessarily
lengthy. Notice that in the expression for and , the equations will give an imaginary

number if because of a negative number inside the square root. The code that
will be created does not include a solution for imaginary roots for reasons mentioned in the
limitations of this study. To avoid this scenario, R must be greater than C and L. In practical
applications of RLC circuits, L and C are usually in microHenry and microFarads,
respectively. And R is usually in kiloOhms. So for simulating real world RLC circuits, the

code will work just fine. Now we have q, and as functions of time. Then we can
create the code in Python that calculates the voltages across each component across a time
interval.

The code that uses the analytical solution asks the user to input the capacitance, resistance
and inductance of the LRC circuit as well as the maximum voltage and frequency of the
sinusoidal voltage source. Then along with a time list, a for loop runs them into a set of

equations to get q, and for each moment of time in the time list.

Figure 1: flowchart of Python code for analytical solution


Figure 2: sample Voltage vs Time graph of the analytical solution

The graph in Figure 2 is the output of the code with voltage, resistance, capacitance, and
frequency=1 and resistance=3. Take note that all produced graphs of voltages in this study
will always have these values as their input.

To check if the analytical solution is correct, we add the three voltages together and
compare the result with the source voltage. According to equation 1, .
So we expect the the two graphs to be the same.

Figure 3: Comparing the combined voltages across RLC circuit and source voltage

Because the two graphs are the same, equation 1 is satisfied and we can be confident in
using the analytical solution as a benchmark for testing the correctness of the numerical
solution.

Deriving The Numerical Solution

The derivative of a function f(x) is defined as the limit of as h approaches


zero. To get a numerical solution for the equation, the derivative has to be calculated
approximately. This is done by setting h to a very small value to the point that it is almost
negligible.

equation 8

equation 9

equation 10

The above equations are numerical representations of the derivatives of q. h is measured in


seconds and q is the charge in the circuit at a particular time. refers to the current charge
in the circuit while and refers to the charge at h and 2h seconds ago, respectively.

These three expressions for q, and are plugged into the RLC circuit differential
equation (equation 1). Simplifying, the expression for q as a function of time becomes

The time interval in python is represented by a list of equally spaced increasing values. This
represents snapshots of time in close increments. The code inputs each value in the time
list to the equation for q and gets a value for q at that particular time. The result is another
set of values which represent q. If the space between elements in the time list is sufficiently
small, the resulting plot will look like a continuous curve. To achieve this, the time interval
has to be small and the number of elements as large as possible. At the first time increment,
and are zero because q does not have a value before this time. Therefore

At the second time increment, the value of is the previous value of which is

. because there is no value for q at 2h seconds ago. Therefore

At the proceeding increments,

equation 11

equation 12

equation 13

The expressions for , and (equations 8, 9 & 10) will be plugged into expressions

for q, and (equations 11, 12 & 13) to compute for the voltages across each circuit
component at any time.
Figure 4: flowchart of Python code for numerical solution

Figure 3 shows the workflow of the numerical solution code. Notice that an if loop is
present because the expressions for , and are different for the 1st and 2nd time
increments.

Figure 5: sample Voltage vs Time graph of the numerical solution

The graph in Figure 2 is the output of the code with voltage, resistance, capacitance, and
frequency=1 and resistance=3. The time list is composed of 100 equally spaced values
from 0 to 10.

Results and Discussion

The numerical solution has some possible unwanted properties which cannot be explained
by the researcher. First, the amplitude of voltages across resistor and capacitor decreases
as h decreases but the amplitude of voltages across inductor remains the same. (h is
decreased by decreasing the time interval and/or increasing the number of elements in the
time list.) It is also worth noting that as h decreases, the curves of the graph gets smoother
and thus more desirable.
Figure 6: For the numerical solution, the amplitudes of and decreases as h decreases.

Another thing that happens if h is decreased is that the voltages across the three circuit
components get more in phase with each other. Based on equation 1, this is not supposed
to be the case. Since contains the derivative of , must lag by 90° with respect to .
For the same reason, must also lag by 90° with respect to .

One possible solution to restore the amplitudes of the numerical solution to their realistic
values is to introduce correction factors for each of the three voltages. These correction
factors will be based on the graph of the analytical solution. This is because the analytical
solution did not use h to compute for the derivatives. Thus, the amplitudes remain the same
for any length of the time list.
Figure 7: For the analytical solution, the voltage amplitudes remain the same

Determining The Correction Factors for Amplitudes of Numerical Solution

In determining the correction factors, the peaks of the waves of both graphs are
determined. The equation to get the correction factor is:

The obtained list of for each pair of peaks is then averaged to get the for the numerical
solution. This factor will be multiplied to each element of the voltage list to make the
amplitudes similar to the analytical solution. Three correction factors will be obtained for
the three voltages.
Figure 8: Code workflow to get correction factor for voltages in numerical solution

The correction factors for a time list of 10000 elements from 0 to 10 are:

It is important to note that these correction factors will only work for a time list of 10000
elements from 0 to 10 or for another time list which has the same value of h with the time
list above.

Figure 9: plot of numerical solution with applied (time list of 10000 elements from 0 to
10)

Making the Voltages Correctly Out of Phase

Because of lack of time and personnel, the researcher was not able to find a way to make
the voltages correctly out of phase.

Conclusion

The numerical solution for solving the RLC circuit DE is a powerful tool with many possible
applications for filtering different frequencies of a waveform. These waveforms can
represent sound, voltages, or electromagnetic signals. But the numerical solution has some
unwanted properties. As h decreases, the voltage waveforms phase shift and become more
in phase. Also, the amplitudes of and decreases as h decreases. These changes make
the voltages inaccurate.They can be offset by using the analytical solution as a benchmark
and modifying the numerical solution so that its results will look more similar with the
analytical solution. If through these modifications, the numerical solution will be able to
give an accurate result, it can then be used to analyze more complex waveforms such as
sound and radio.

Recommendation

The researcher recommends the following for future works on this study:
● Solve the phase shift problem of the numerical solution.
● Create a code that computes the correction factor for any value of h. This will be
important for analyzing audio files or signals because the length of the time list will
not be set anymore unlike in this study.
● Create a code that uses the numerical solution complete with corrections based on
the analytical solution and run it on audio files (wav format).

You might also like