Sympy unable to plot Fourier series, bug

103 views
Skip to first unread message

Areeb Sherjil

unread,
Apr 23, 2021, 11:09:20 AM4/23/21
to sympy
Hi all, 
I hope everyone is enjoying themselves!
I am trying to compute the Fourier series of a simple signal(1 Volt square with freq of 1hz and 0.5T pulse width). 

This is what I need help with:
1- How can I make sympy display the results of the sigma summation as : Ao+An1+An2+An3....... etc instead of the way it now?Like how do I expand it?
2- When I try to plot this, I get a warning message telling me to report this as a bug? Why is this ?(look at screenshot)

Any response is very much appreciated 

Areeb Sherjil

unread,
May 5, 2021, 8:07:42 AM5/5/21
to sympy
No one is replying, lemme paste the code here:


import sympy as syms
import matplotlib.pyplot as plot
import numpy as linspace
n,t= syms.symbols('n,t')
T= 1
w= 2*syms.pi/T
V=1   # square wave of 1volts with 1second period
Ao= (w/syms.pi)* syms.integrate(V,(t,0,0.5))
An= (w/syms.pi)*syms.integrate(V*syms.cos(w*n*t),(t,0,T/2))
Bn= (w/syms.pi)*syms.integrate(V*syms.sin(w*n*t),(t,0,T/2))
f= Ao/2 +syms.Sum(An*syms.cos(n*w*t),(n,1,5))+ syms.Sum(Bn*syms.sin(n*w*t),(n,1,5))
f

syms.plot(f,(t,0,2*T))

Davide Sandona'

unread,
May 5, 2021, 8:31:05 AM5/5/21
to sy...@googlegroups.com
Whenever it makes sense, you should use assumptions on symbols. Since you are dealing with a Fourier Series, you should set:

import sympy as syms
import matplotlib.pyplot as plot
import numpy as linspace
n = syms.symbols('n', real=True, integer=True, positive=True)
t = syms.symbols('t', real=True)

T= 1
w= 2*syms.pi/T
V=1   # square wave of 1volts with 1second period
Ao= (w/syms.pi)* syms.integrate(V,(t,0,0.5))
An= (w/syms.pi)*syms.integrate(V*syms.cos(w*n*t),(t,0,T/2))
Bn= (w/syms.pi)*syms.integrate(V*syms.sin(w*n*t),(t,0,T/2))
f= Ao/2 +syms.Sum(An*syms.cos(n*w*t),(n,1,5))+ syms.Sum(Bn*syms.sin(n*w*t),(n,1,5))
f

At this point, the expression f looks like can be simplified. Then:

syms.plot(f.simplify(), (t, 0, 4*T))

Davide.


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/05c2fc4e-82a6-447f-aca3-3694af254938n%40googlegroups.com.

Areeb Sherjil

unread,
May 5, 2021, 8:52:16 AM5/5/21
to sympy
Respond to  Davide

Hi,
Thanks for this.
1-I want to ask: how do I expand the sigma summation like : cos+cos2+cos3..... etc 
2- Also how do I open the plot in a separate window to make it bigger/zoom etc?

Areeb Sherjil

unread,
May 5, 2021, 8:58:30 AM5/5/21
to sympy
Oh no sorry, I used %matplotlib tk at the start and now it opens the graph in separate window.(Google groups does not allow to edit messages )

But how to expand the summation series?

Davide Sandona'

unread,
May 5, 2021, 9:02:48 AM5/5/21
to sy...@googlegroups.com
Use the doit() method, something like the following: f.doit()

Davide.


Areeb Sherjil

unread,
May 5, 2021, 9:07:13 AM5/5/21
to sympy
Thanks  Davide!
It works now 

Reply all
Reply to author
Forward
0 new messages