A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Complex Number Math – Sinusoidal Analysis… the much easier way.

Copy and Paste: Ctrl-C, Ctrl-V

Copy and Paste: Ctrl-C, Ctrl-V

If you have followed the last couple of posts about phasors,”Vectoring in on a Solution – Phasors” &  “Complex Number Math – Sinusoidal Analysis“,  you are probably in agreement that sinusoidal analysis it can produce some very nice detailed results. Hopefully you are also in agreement that the concept is not all that hard.  But more than likely, you also are thinking: “This arithmetic is a pain”.  I have to agree… it is a pain.  Tonight I will show you how I do it.  The worst problem with this method is getting callouses on a couple of fingertips from copying and pasting.

There are “canned programs” you can purchase and some share ware or open source programs you can download that will do the job.  However, I have yet to find one that will work with all operating systems.  The second problem with a special purpose program is there is always a learning curve associated with the program.  Unless you intend to use the program often, the learning will fade between uses.  I prefer instead to use a general purpose program and program it myself.  Since there are really few operations in sinusoidal analysis the programming is relatively easy.

The circuit we are going to solve.

The circuit we are going to solve.

In this post we are going to solve the same problem we solved in the last post but we will do it using Python to program the problem.  Python is my programming language of choice and I described why back in “Episode 44 – Programming to learn logical thinking”, and “Episode 45 – More about Programming and Python”.  I have placed the program I used for this analysis and the output it produced on the server for you to download and look at while I describe the program.  The program and the output file.  (Note: it may be necessary to change the extension of the program file to .doc or .txt to display it on Windows machines.  You will probably want to open it in WordPad if you do not have Python installed on your Windows Computer. In Linux open it in the text editor.  On Mac’s I have no idea.)

Python is written with the subroutines at the start of the program and then after all the subroutines, the final section is the “main program”.  The first thing I do in the main program is initialize all the values of the problem to be solved. This is shown below and matches the schematic diagram.

XV = 1000    #set X values at XV at resonance freq
fr = 100     #resonance freq in Hz
C = 1/(2*math.pi*fr*XV)
L = XV/(2*math.pi*fr)
R = 1000
Rl = 10000
fn =”/home/numbnuts/programming/PostCheck/sineanalout.txt”
f = 90


L and C were calculated so they would be the exact values needed to produce 1000 Ω reactance at 100 Hz.  fn is the complete path name to the output file on my computer system.   Should you decide to run this on your system you will have to edit that path. Also be aware, on Windows machines the path divider is \ because Windows uses a backslash.   (Oh Yeah, I chose my home directory name before I started blogging…and then when I realized what I had done, I had too much invested to rock the boat.)

The next step was to print these values to the output file:

#write the inital values to the file
fl=open(fn,’w’)  #this writes over the file
fl.write(‘Series-Parallel RLC Problem nn’)
fl.write(‘—–compontent values——n’)
fl.write(‘C = ‘+ str(C)+”n”)
fl.write(‘L = ‘+ str(L)+”n”)
fl.write(‘R = ‘+ str(R)+”n”)
fl.write(‘Rl = ‘+ str(Rl)+”n”)
fl.write(‘frequency where calculations are being performed ‘+ str(f)+” Hznn”)

These file write blocks are done many times in the program with small modifications.  Most of the length of the program is due to these. Probably this should be done in a well written subroutine. However it was easier for now to think less and use copy and paste to write the same block and modify.

Now we start the actual complex number math part. The first thing is to find the impedance values for each component.

#start the real calculations
w = f2omega(f)
Zc = cpxXc(C,w)
Zl = cpxXl(L,w)

The first three lines of that call subroutines.  The first one converts frequency in Hz to radians per second, the greek symbol ω (small omega) is used to represent radians per second, since Python and the text editor do not have greek symbols I used w instead. This saves me from writing 2*Π*f each time I calculate a reactive impedance.   These three subroutines are shown next:

# ———— f2omega ———————
def f2omega (f):
#changing from frequency to radians per second.
w =f*2*math.pi
#the following was used to check math in the post
#w =f*2*3.14
# ———— f2omega ———————
# ———— cpxXc ———————
def cpxXc(C,w):
idenom = w*C
denom = complex(0.0,idenom)
Xc = 1.0/denom
# ———— cpxXc ———————
# ———— cpxXl ———————
def cpxXl(L,w):
imj= w*L
Xl = complex(0.0,imj)
# ———— cpxXl ———————

The whole advantage of doing these with subroutines is I will never have to think about those calculations again.  This is especially helpful when doing the calculations over many frequencies.  Now all of the impedance values are complex number form and all I really have to think about is Ohm’s Law, Kirchhoff Laws, solving the problem and creating the output file.  NO MORE WORRYING ABOUT COMPLEX CONJUGATES AND KEEPING TRACK OF THE J’S!!!!! It is all done for me.

A portion of the output file shows how the Z values now look.

—–Impedance Values——
Zc = -1111.11111111j
Zl = 900j
ZR = (1000+0j)

The next step was to start combining values:

#combining ZR and ZL
ZRL = ZR + Zl
#combining ZC &ZRl
Zpar = parallelZ (Zc,ZRl)

This calls a subroutine to calculate the parallel impedance.  (Remember how much of a pain that was in the last post!).  The subroutine is:

# ———— parallelZ ———————
def parallelZ (Z1,Z2):
Zpar = (Z1*Z2)/(Z1 + Z2)
# ———— parallelZ ———————

Life just got a whole bunch better!

Next I combine the parallel impedance with the combination of ZR and Zl to get the total impedance. I then convert Vin into a complex number and calculate the total current.  (Again, complex division made a whole lot easier.)

# calculate the total impedance in the circuit
Ztot = Zpar+ZRL
Vin = complex(1,0)

#calculating the current through the total circuit.
Itot = Vin/Ztot

Next, in the real world we really don’t want to think of complex numbers.  We want to think of the absolute values like we would read on a voltmeter or ammeter and the phase shift from our reference phase of Vin.  Python has several ways of doing this.  The method I chose to use was a built-in function called cmath.polar that does both the absolute value and phase angle with one function.  However, it does provide the phase angle in radians so I used another built-in function to convert it to degrees.  The description of these and many other functions can be found the the Python “Reference Library Manual”. An example of my using this is the conversion of Itot.

#changing the curent back to the absolute value and the
#phase angle.
Ip =cmath.polar(Itot)
Iabs = Ip[0]   #the absolute value of the current
Iph = math.degrees(Ip[1]) #the phase angle of the current

The rest of the program calculated the voltage and current through each component and the power consumed by each resistor.   The voltage across R and L was calculated.  These were added and the sum subtracted from Vin to determine the voltage across C and Rl.  Then the current through each of these were calculated using Ohms Law.

As I said earlier, the length of this program is mostly the sections printing the values to the output file.   I did a lot of copy and paste to do that…almost so much that my hand is permanently stuck in the “ctrl-c ctrl-v” configuration.   I guess I need to find a rock concert to attend.

Rock on Dudes!

Rock on Dudes!

Python is a very user friendly language.  Sinusoidal Analysis is very powerful to understand a circuit.   Although there will be a small learning curve, programming is a great way to model these LRC circuits and really understand what is happening inside the circuit.



Print Friendly

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>