I have defined and function and calling it to store the computed values in an array. However the values in array are different than what they should be. I have plotted both the output of the function and stored array values. Can anyone help me resolve this issue. Here is the code and the output.
from numpy import linspace, exp
import matplotlib.pyplot as pl
def gaussian(x, off, amp, cen, wid):
return off+amp * exp(-(x-cen)**2 /wid**2)
PhaseArray = [0 for x in range (100)]
for x in range(100):
PhaseArray[x] = gaussian(x, 0, 1000, 50, 15)
x = linspace(0,99,100)
fig = pl.figure()
pl.plot(PhaseArray, 'go-')
pl.plot(x, gaussian(x, 0, 1000, 50, 15), 'ro-')
pl.show()
The output plot looks like
1 Answer 1
linspace provides a vector of float numbers that go to gaussian as a vector and are processed according to numpy operators over vectors. On the other hand, to fill PhaseArray you feed gaussian by integer x that is processed in a different way. It explains the difference.
rangereturn integers, whereaslinspacereturns floats. E.g. try callinggaussian(40, 0, 1000, 50, 15)andgaussian(40.0, 0, 1000, 50, 15)and you'll see the difference. When you perform a division of integers the result gets truncated (for Python 2.7 and older).