I try to implement the Fourier series function according to the following formulas:
Consider developing your code in a different way, block by block. You should be surprised if a code like this would work at the first try. Debugging is one option, as @tom10 said. The other option is rapid prototyping the code step by step in the interpreter, even better with ipython.
Above, you are expecting that b_1000
is non-zero, since the input f(x)
is a sinusoid with a 1000
in it. You're also expecting that all other coefficients are zero right?
Then you should focus on the function b(n, L, accuracy = 1000)
only. Looking at it, 3 things are going wrong. Here are some hints.
dx
is within the loop. Sure about that?i
is supposed to be an integer right? Is it really an integer? by prototyping or debugging you would discover this(1/L)
or a similar expression. If you're using python2.7, you're doing likely wrong. If not, at least use a from __future__ import division
at the top of your source. Read this PEP if you don't know what I am talking about.If you address these 3 points, b()
will work. Then think of a
in a similar fashion.