Issue
You can see the loop below, which gives a different plot everytime, and approximately 1 out of 20 runs is the result that I want, which is a triangular Fourier series. How to make it so that the outcome is the same everytime?
L=1
N=1000
#Array for x
x=np.linspace(-3*L,3*L,N+1)
#Array for sum
s=np.empty(N+1)
#While loop for sum
i=1
while(i<N+1):
s=(1/((2*i-1)**2))*(np.cos((2*i-1)*np.pi*x/L))+s
i=i+1
print(s)
#f(x)
y=(L/2)-(4*L/((np.pi)**2))*s
#Settings for plot
plt.plot(x,y)
Solution
This line
s=np.empty(N+1)
produces an array filled with undefined(arbitrary) value. You should be using a new array filled with zero instead:
s=np.zeros(N+1)
Answered By - Z Li
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.