In [3]:
import numpy as np
import sympy as sy
from sympy import I, exp
from sympy.utilities.codegen import codegen

In [2]:
z = sy.symbols("z", real=False)
wn, zeta, a,h,r1,s0,s1 = sy.symbols("wn, zeta,a,h,r1,s0,s1", real=True)

In [5]:
pc1 = -wn*zeta + I*wn*sy.sqrt(1-zeta**2)
pc2 = -wn*zeta - I*wn*sy.sqrt(1-zeta**2)

rd = exp(-wn*zeta*h)
argd = wn*sy.sqrt(1-zeta**2)*h
reald = rd*sy.cos(argd)
imd = rd*sy.sin(argd)
Ac = sy.poly(z**2 - 2*reald*z + rd**2, z)
ad = sy.exp(-a*h)
Ao = sy.poly(z-ad, z)
Acl = Ac*Ao

A = sy.poly((z-1)*(z-exp(-2*h)), z)
B = sy.poly((-1+h+exp(-2*h))*z + 1-(1+h)*exp(-2*h), z)

print Ac


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-6c8d634889f5> in <module>()
----> 1 pc1 = -wn*zeta + I*wn*sy.sqrt(1-zeta**2)
      2 pc2 = -wn*zeta - I*wn*sy.sqrt(1-zeta**2)
      3 
      4 rd = exp(-wn*zeta*h)
      5 argd = wn*sy.sqrt(1-zeta**2)*h

NameError: name 'wn' is not defined

In [50]:
A.subs(h,0.14).subs(wn, 2*np.sqrt(2)).subs(zeta, np.cos(np.pi/4.0))


Out[50]:
z**2 - 1.75578374145573*z + 0.755783741455725

In [51]:
R = sy.poly(z+r1, z)
S = sy.poly(s0*z + s1, z)
dioph=(A*R+B*S-Acl).all_coeffs()

In [52]:
sol=sy.solve(dioph, (r1,s0,s1))

In [53]:
print sy.simplify(sy.expand(sol[r1].subs(h,0.14).subs(wn, 2*np.sqrt(2)).subs(zeta, np.cos(np.pi/4.0))))
print sy.simplify(sy.expand(sol[s0].subs(h,0.14).subs(wn, 2*np.sqrt(2)).subs(zeta, np.cos(np.pi/4.0))))
print sy.simplify(sy.expand(sol[s1].subs(h,0.14).subs(wn, 2*np.sqrt(2)).subs(zeta, np.cos(np.pi/4.0))))


-2.16080564088232*exp(-0.14*a) + 1.54163168497804
-11.1384313455197*exp(-0.14*a) + 11.8844037451403
7.67227291664397*exp(-0.14*a) - 8.4182453162646

In [4]:
# Reorganize solution expression for matlab code generation
sol_expr = ('RST_example', [sol[r1], sol[s0], sol[s1], Ac.subs(z, 1)/B.subs(z,1), ad])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-bfab13b90b4b> in <module>()
      1 # Reorganize solution expression for matlab code generation
----> 2 sol_expr = ('RST_example', [sol[r1], sol[s0], sol[s1], Ac.subs(z, 1)/B.subs(z,1), ad])

NameError: name 'sol' is not defined

In [2]:
# Export to matlab code
[(m_name, m_code)] = codegen(sol_expr, 'octave')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-05a7278ed72d> in <module>()
      1 # Export to matlab code
----> 2 [(m_name, m_code)] = codegen(sol_expr, 'octave')

NameError: name 'codegen' is not defined

In [1]:
print m_code
with open("/home/kjartan/Dropbox/undervisning/tec/MR2007/matlab/RST_example.m", "w") as text_file:
    text_file.write(m_code)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-d9031db0c528> in <module>()
----> 1 print m_code
      2 with open("/home/kjartan/Dropbox/undervisning/tec/MR2007/lab/dc_rst_design.m", "w") as text_file:
      3     text_file.write(m_code)

NameError: name 'm_code' is not defined

In [57]:



Out[57]:
[s1, s0, r1]

In [ ]:
np.exp(-0.28)

In [ ]:
0.28*180.0/np.pi

In [ ]:
hh=0.14
-1+hh+np.exp(-2*hh)

In [ ]:
1-(1+hh)*np.exp(-2*hh)

In [ ]:
np.exp(-2*hh)

In [ ]:
sy.expand()