Skip to content
Advertisement

Trying to simplify a stubborn expression in SymPy – complex exponential / trigonometric space vector equation

I am trying to simplify

cos(phi) + cos(phi - 2*pi/3)*e^(I*2*pi/3) + cos(phi - 4*pi/3)*e^(I*4*pi/3)

which I know reduces down to 1.5e^(I*phi)

I cannot get SymPy to recognize this. I have tried simplify, trigsimp, expand, etc. But nothing seems to work. Any suggestions?

Here is my code:

import numpy as np
%matplotlib inline 
import matplotlib.pyplot as plt

import sympy as sp
from sympy import I 
sp.init_printing()
phi = sp.symbols('phi', real = True)

vec = sp.cos(phi) + sp.cos(phi - 2*sp.pi/3)*sp.exp(I*2*sp.pi/3) + sp.cos(phi - 4*sp.pi/3)*sp.exp(I*4*sp.pi/3)

vec.simplify()
vec.rewrite(sp.exp).simplify()
vec.rewrite(sp.exp).expand().simplify()

None of these produce the expected result. I can confirm my result manually, by substituting values in for phi like this:

sp.simplify(vec.rewrite(sp.exp).simplify() - 3/2*sp.exp(I*phi)).evalf(subs={phi:3})

Advertisement

Answer

It’s not obvious but you can get there like this:

In [40]: phi = symbols('phi', real=True)

In [41]: e = cos(phi) + cos(phi - 2*pi/3)*E**(I*2*pi/3) + cos(phi - 4*pi/3)*E**(I*4*pi/3)

In [42]: e
Out[42]: 
   -2⋅ⅈ⋅π                         2⋅ⅈ⋅π           
   ───────                        ─────           
      3       ⎛    π⎞               3      ⎛    π⎞
- ℯ       ⋅sin⎜φ + ─⎟ + cos(φ) - ℯ     ⋅cos⎜φ + ─⎟
              ⎝    6⎠                      ⎝    3⎠

In [43]: e.rewrite(exp).expand().rewrite(sin).expand().rewrite(exp)
Out[43]: 
   ⅈ⋅φ
3⋅ℯ   
──────
  2 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement