Dec-22-2018, 09:44 AM
Hey everyone, brand new here! I typically find all my answers within a 30min google search. But not this time!
I’m working on a basic Python program, mainly for practice after an intro course covering Python and Java. So I’m kind of a noob, keep that in mind! Anyway, I’ve been adding random functions to it as I have time. It is used for misc circuit troubleshooting (I troubleshoot board assemblies for work). I plan to add any equations I regularly use to it that I possibly can. So here’s my problem.
To solve parallel Impedance, you take a lot of reciprocals of numbers as well as a sqrt of the difference of, well. You’ll see. I decided to split this calculation into chunks, this way I could easily omit values that were not needed and to troubleshoot my code.
Near the end of my calculation, I take the sqrt of a typically small float, like 1.7E-10 ish, then take the reciprocal of the result. Problem is, the result of the sqrt is always 1.0. I’ve tried pow(x, 1/2) and **1/2 as well as sqrt() with no change. I haven’t had time to get this onto my computer yet, however. I’ve coded all of this using IOS with the Python Idle app.
I’ll paste my entire code below to help you help me! Just do me and yourself a favor, if you run the program, know there is really no error handling right now. And once I realized this sqrt problem, I began making it more unstable. So right now, it can only handle to ‘solve’ || impedance if all 3 (Resistance, Xl, Xc) are populated.
Feel free to make any suggestions as well! I’m sure there are a lot of issues this early in development, though. Thank you ahead of time! - RHT
Here’s my terminal for a run as well, followed by actual code. Problem function is:
def parallelZ()
*************************
Enter 0 to Quit
Enter 1 for || Resistance
Enter 2 for xC
Enter 3 for xL
Enter 4 for Z
Enter 5 for || Z
Enter 6 for Phase
*************************
5
====================
Solving || Z
====================
If you don't have R, Xl, Xc, enter 0 to acquire these before continuing. Otherwise, enter 1.
1
Enter 0 if R NOT present, 1 if present
1
Enter Resistance in Ohms: 100000
Enter 0 if Inductor NOT present, 1 if present
1
Enter Xl in Ohms: 150000
Enter 0 if Capacitor NOT present, 1 if present
1
Enter Xc in Ohms: 200000
Xl: 150000.0
Xc: 200000.0
R: 100000.0
rZ: 1e-10
xlZ: 6.66666666667e-06
xcZ: 5e-06
xlcZ: 2.77777777778e-12
prePreResult: 1.02777777778e-10
preResult: 1.0
Parallel Impedance: 1.0
*************************
Enter 0 to Quit
Enter 1 for || Resistance
Enter 2 for xC
Enter 3 for xL
Enter 4 for Z
Enter 5 for || Z
Enter 6 for Phase
*************************
I’m working on a basic Python program, mainly for practice after an intro course covering Python and Java. So I’m kind of a noob, keep that in mind! Anyway, I’ve been adding random functions to it as I have time. It is used for misc circuit troubleshooting (I troubleshoot board assemblies for work). I plan to add any equations I regularly use to it that I possibly can. So here’s my problem.
To solve parallel Impedance, you take a lot of reciprocals of numbers as well as a sqrt of the difference of, well. You’ll see. I decided to split this calculation into chunks, this way I could easily omit values that were not needed and to troubleshoot my code.
Near the end of my calculation, I take the sqrt of a typically small float, like 1.7E-10 ish, then take the reciprocal of the result. Problem is, the result of the sqrt is always 1.0. I’ve tried pow(x, 1/2) and **1/2 as well as sqrt() with no change. I haven’t had time to get this onto my computer yet, however. I’ve coded all of this using IOS with the Python Idle app.
I’ll paste my entire code below to help you help me! Just do me and yourself a favor, if you run the program, know there is really no error handling right now. And once I realized this sqrt problem, I began making it more unstable. So right now, it can only handle to ‘solve’ || impedance if all 3 (Resistance, Xl, Xc) are populated.
Feel free to make any suggestions as well! I’m sure there are a lot of issues this early in development, though. Thank you ahead of time! - RHT
Here’s my terminal for a run as well, followed by actual code. Problem function is:
def parallelZ()
*************************
Enter 0 to Quit
Enter 1 for || Resistance
Enter 2 for xC
Enter 3 for xL
Enter 4 for Z
Enter 5 for || Z
Enter 6 for Phase
*************************
5
====================
Solving || Z
====================
If you don't have R, Xl, Xc, enter 0 to acquire these before continuing. Otherwise, enter 1.
1
Enter 0 if R NOT present, 1 if present
1
Enter Resistance in Ohms: 100000
Enter 0 if Inductor NOT present, 1 if present
1
Enter Xl in Ohms: 150000
Enter 0 if Capacitor NOT present, 1 if present
1
Enter Xc in Ohms: 200000
Xl: 150000.0
Xc: 200000.0
R: 100000.0
rZ: 1e-10
xlZ: 6.66666666667e-06
xcZ: 5e-06
xlcZ: 2.77777777778e-12
prePreResult: 1.02777777778e-10
preResult: 1.0
Parallel Impedance: 1.0
*************************
Enter 0 to Quit
Enter 1 for || Resistance
Enter 2 for xC
Enter 3 for xL
Enter 4 for Z
Enter 5 for || Z
Enter 6 for Phase
*************************
import math import decimal # << necessary? will have to determine later on.. def solvePhase(): xOfL = "N/A" xOfC = "N/A" r = "N/A" print("If you don't have R, Xl, Xc, enter 0 to acquire these before continuing. Otherwise, enter 1.") haveValues = input() if haveValues == 0: print("Enter 0 if Inductor NOT present, 1 if present") needXl = input() if needXl == 1: xOfL = lReact(1) print("Enter 0 if Capacitor NOT present, 1 if present") needXc = input() if needXc == 1: xOfC = cReact(1) print(xOfL) print(xOfC) r = input("Enter Resistance in Ohms: ") if haveValues == 1: xOfL = float(input("Enter Xl in Ohms: ")) xOfC = float(input("Enter Xc in Ohms: ")) if r == "N/A": print("Setting R to 1") r = 1 if xOfL == "N/A": print("Setting Xl to 1") xOfL = 1 if xOfC == "N/A": print("Setting Xc to 1") xOfC = 1 prePhase = math.atan(((xOfL - xOfC) / r)) phase = math.degrees(prePhase) return "Phase: " + str(phase) def lReact(called): fHz = input("Enter Frequency in Hz: ") lH = input("Enter inductance in H: ") fHz = float(fHz) lH = float(lH) result = (2.0 * math.pi) * fHz * lH if called == 1: return result message = "Inductive Reactance: " completeResult = message + str(result) return completeResult + " Ohms Z" def cReact(called): fHz = input("Enter Frequency in Hz: ") cF = input("Enter capacitance in F: ") fHz = float(fHz) cF = float(cF) if cF == 0: print("Setting cF to 0.0000000001") cF = 0.0000000001 result = 1.0 / ((2.0 * math.pi) * fHz * cF) if called == 1: return result message = "Capacitive Reactance: " completeResult = message + str(result) return completeResult + " Ohms Z" def seriesZ(): print("If you don't have R, Xl, Xc, enter 0 to acquire these before continuing. Otherwise, enter 1.") solveZ = input() if solveZ == 0: xOfL = lReact() xOfC = cReact() print(xOfL) print(xOfC) r = input("Enter Resistance in Ohms: ") xl = input("Enter Xl in Ohms: ") xc = input("Enter Xc in Ohms: ") r = float(r) xl = float(xl) xc = float(xc) result = math.sqrt((r ** 2) + (((xl-xc)**2))) message = "Series Impedance: " completeResult = message + str(result) return completeResult + " Ohms Z" # reciprocal of the sqrt of the reciprocal of the resistance squared plus the difference of the reciprocal of inductive reactance and the reciprocal of capacitive reactance squared.. def parallelZ(): # Setting variables to N/A to check # whether to include in final calc # If user does not enter value for # any, N/A will remain in variable xOfL = "N/A" xOfC = "N/A" r = "N/A" # If reactances NOT known, call method # with argument(1) to return only # the reactance (without message) # Then assign returned value to # the reactance variable print("If you don't have R, Xl, Xc, enter 0 to acquire these before continuing. Otherwise, enter 1.") haveValues = input() if haveValues == 0: print("Enter 0 if Inductor NOT present, 1 if present") needXl = input() if needXl == 1: xOfL = lReact(1) print("Enter 0 if Capacitor NOT present, 1 if present") needXc = input() if needXc == 1: xOfC = cReact(1) # Always asks if R present print("Enter 0 if R NOT present, 1 if present") needR = input() if needR == 1: r = float(input("Enter Resistance in Ohms: ")) # If user has reactances, this # branch will execute, skipping # method calls to calculate each # reactance value if haveValues == 1: print("Enter 0 if Inductor NOT present, 1 if present") needXl = input() if needXl == 1: xOfL = float(input("Enter Xl in Ohms: ")) print("Enter 0 if Capacitor NOT present, 1 if present") needXc = input() if needXc == 1: xOfC = float(input("Enter Xc in Ohms: ")) includeR = 1 includeXOfL = 1 includeXOfC = 1 if r == "N/A": print("Omitting R") includeR = 0 if xOfL == "N/A": print("Omitting Xl") includeXOfL = 0 if xOfC == "N/A": print("Omitting Xc") includeXOfC = 0 print("Xl: " + str(xOfL)) print("Xc: " + str(xOfC)) print("R: " + str(r)) if includeR == 1: rZ = (r **2)**-1 if includeXOfL == 1: xlZ = (xOfL)**-1 if includeXOfC == 1: xcZ = (xOfC)**-1 print("rZ: " + str(rZ)) # testing print("xlZ: " + str(xlZ)) # testing print("xcZ: " + str(xcZ)) # testing if includeR == 1 and includeXOfL == 1 and includeXOfC == 1: xlcZ = float((xlZ - xcZ)**2) prePreResult = float((rZ + xlcZ)) print("xlcZ: " + str(xlcZ)) # testing print("prePreResult: " + str(prePreResult)) # testing preResult = pow(prePreResult, 1/2) print("preResult: " + str(preResult)) # testing result = float((preResult)**-1) message = "Parallel Impedance: " completeResult = message + str(result) return completeResult def parallelResistance(): resistor1 = input("Enter 1st resistance: ") resistor1 = float(resistor1) resistor2 = input("Enter 2nd resistance: ") resistor2 = float(resistor2) r1 = resistor1 r2 = resistor2 result = (1.0 / r1) + (1.0 / r2) result = 1.0 / result return result def mainMenu(): print("*************************") print("Enter 0 to Quit") print("Enter 1 for || Resistance") print("Enter 2 for xC") print("Enter 3 for xL") print("Enter 4 for Z") print("Enter 5 for || Z") print("Enter 6 for Phase") print("*************************") choice = input() if choice == 0: return choice if choice == 1: return choice if choice == 2: return choice if choice == 3: return choice if choice == 4: return choice if choice == 5: return choice if choice == 6: return choice # Main Code # programIsTerminated = 0 while programIsTerminated == 0: menuChoice = mainMenu() if menuChoice == 0: print("====================") print("Program Terminated") print("====================") # programIsTerminated = 1 break if menuChoice == 1: print("====================") print("Solving || Resistance") print("====================") print(parallelResistance()) continue if menuChoice == 2: print("====================") print("Solving Xc") print("====================") print(cReact()) continue if menuChoice == 3: print("====================") print("Solving Xl") print("====================") print(lReact()) continue if menuChoice == 4: print("====================") print("Solving Z") print("====================") print(seriesZ()) continue if menuChoice == 5: print("====================") print("Solving || Z") print("====================") print(parallelZ()) continue if menuChoice == 6: print("====================") print("Solving Phase") print("====================") print(solvePhase()) continue else: print("====================") print("Please enter a VALID choice.") print("====================") continue # print("Program Terminated")