0

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

enter image description here

Paul Rooney
21.7k9 gold badges47 silver badges64 bronze badges
asked Dec 19, 2016 at 10:57
1
  • range return integers, whereas linspace returns floats. E.g. try calling gaussian(40, 0, 1000, 50, 15) and gaussian(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). Commented Dec 19, 2016 at 11:06

1 Answer 1

2

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.

answered Dec 19, 2016 at 11:06
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.