Skip to content

Instantly share code, notes, and snippets.

@brilliant-ember
Created April 7, 2020 22:18
Show Gist options
  • Save brilliant-ember/eae69ace1797ab8c4b9d483e93a88472 to your computer and use it in GitHub Desktop.
Save brilliant-ember/eae69ace1797ab8c4b9d483e93a88472 to your computer and use it in GitHub Desktop.
Converts a control lib transfer function to sympy lib symbolic equation, uses x instead of s for the symbolic representation
def tf_to_symbolic_fraction(tf):
x = symbols('x')
num, den = tfdata(tf)
num = num[0][0]
den = den[0][0]
counter = 1
length_num = len(num)
length_den = len(den)
sym_num, sym_den = 0,0
if(length_num == 0):
raise ValueError ("The fraction num should not be empty")
elif(length_den == 0 ):
raise ValueError("The fraction den should not be empty")
for i in range(length_num):
sym_num+=num[i]*(x**(length_num - counter))
counter += counter
counter = 1
for i in range(length_den):
sym_den+=den[i]*(x**(length_den - counter))
counter += counter
return sym_num/sym_den
@brilliant-ember
Copy link
Author

make sure to do the following imports:

from control import *
from sympy import * 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment