With these it helps 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 nonzero, since the input f(x) is a sinusoid with a 1000 in it. You're also expecting that all other coefficients are zero right? code :
Share :

How to calculate a Fourier series in Numpy?
By : user2482111
Date : March 29 2020, 07:55 AM
I wish this help you In the end, the most simple thing (calculating the coefficient with a riemann sum) was the most portable/efficient/robust way to solve my problem: code :
def cn(n):
c = y*np.exp(1j*2*n*np.pi*time/period)
return c.sum()/c.size
def f(x, Nh):
f = np.array([2*cn(i)*np.exp(1j*2*i*np.pi*x/period) for i in range(1,Nh+1)])
return f.sum()
y2 = np.array([f(t,50).real for t in time])
plot(time, y)
plot(time, y2)

Fourier Series from Discrete Fourier Transform
By : user3015790
Date : March 29 2020, 07:55 AM

Trigonometry functions from series expansion
By : Manoj
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can avoid recalculating x**n and the factorial at each step by calculating the next term of the sum using the previous one: code :
def sin2(x, n=20):
curr = x
res = curr
for i in range(2, n, 2):
curr *=  x**2/(i*(i+1))
res += curr
return res
from math import factorial
def sin(x, n=20):
return sum(x**j/factorial(j)*(1 if i%2==0 else 1)
for i, j in enumerate(range(1, n, 2)))
%timeit sin(0.7)
# 100000 loops, best of 3: 8.52 µs per loop
%timeit sin2(0.7)
# 100000 loops, best of 3: 4.54 µs per loop
def sin3(x, n=20):
curr = x
res = 0
minus_x_squared =  x**2
for i in range(2, n, 2):
res += curr
curr *= minus_x_squared/(i*(i+1))
return res
%timeit sin2(0.7)
# 100000 loops, best of 3: 4.6 µs per loop
%timeit sin3(0.7)
# 100000 loops, best of 3: 3.54 µs per loop

Modeling a Fourier Series from Discrete Fourier Transform for Extrapolation
By : user1429804
Date : March 29 2020, 07:55 AM

r programming for loop not working for time series with fourier series
By : user3402506
Date : March 29 2020, 07:55 AM
With these it helps The issue seems to be connected with the iterator name i. When I use another iterator, the code works just fine. Unfortunately, I do not exactly understand why this is the case though. code :
for(j in c(1,2,3,4,5)){
fit < tslm(Gas.train~trend + fourier(Gas.train, K = j))
cat("k = ", j, sep = " ")
print(CV(fit))
}

