..

Python Script to Solve Phasor/Complex Equations for Circuit Analysis

Ditch your TI-Trash


meme about how TI does both terrible calculators and terrible weapons


TI sucks. What sucks even more than their weapons is the TI-89. How does this thing still retail for 100 new????? It sucks. I had to buy one so I could have phasor support, something that my open source numworks calculator currently lacks.

I wrote a quick python script for using python on my lovely keyboard, down below.

import electricpy as ep
from sympy import symbols, Eq, solve, I
from varname import varname
from __future__ import print_function
from io import StringIO
import sys
from sympy import init_session

init_session(quiet=True) 
#TODO fix making printing and enetering more expressions more elegant

def equation_print(name, variable):
    # weird codes are just bolding and unbolding
    print('\033[1m', name, '\033[0m', 'is ', end="")
    pprint(variable, imaginary_unit = "j")
    
def print_rect_and_phasor(name, value):
    value = complex(value)
    print('\033[1m', name, '\033[0m', "rectangular form is", value)
    print('\033[1m', name, '\033[0m', "phasor form is ", end ="")
    ep.cprint(value)
    print()
    
#declare what things you want to solve for. If I have i1, i2, v1, and v2, but I only need i1 and i2, 
#I will only add those to varlist
varlist = [i1,i2]

i1 = symbols('i1')
i2 = symbols('i2')

#write your equations here 
equations = [equation1, equation2]
equation1 = (ep.phasor(-450, 0) + ((6-8j) * (i1)) + ((6-8j) * (i1-i2)) + ep.phasor(450, -120))
equation_print('equation1', equation1)

equation2 = (ep.phasor(-450,-120) + (6-8j) * (i2-i1) + i2 * (6-8j) + (ep.phasor(450,120)))
equation_print('equation2', equation2)

#solve the equations
solutions = solve(equations, varlist, dict=True)
equation_print('solutions', solutions)

#print the solutions
print('\033[1m', "This will print the symbols of the variable, not the variable. Look at your symbols above.", '\033[0m')
solutions = solutions[0]
for count, value in enumerate(varlist):
    temp = solutions[value]
    temp = complex(temp)
    print_rect_and_phasor(value,temp)
    

#Get whatever else you need
#TODO: make this more elegant
s = solutions
print_rect_and_phasor('i2-i1', s[i2]-s[i1])
print_rect_and_phasor('i2 * -1', s[i2] * -1)

I also have it as a jupyter notebook, wrapped in a nix flake. Just run nix develop and use the script. Click here if you want to just download the notebook, or run

git clone https://git.sr.ht/~imadnyc/EE-env-python

I, of course, prefer the notebook, since I can check my work from mulitple questions.

That’s really it. You can tell my Valentines day is going well (I bombed the hell out of my digi sys test).