Ascendant0
- 175
- 38
- TL;DR Summary
- We were given an assignment to produce a plot of the function ## f(x) = sin(x)/x ##, and set it up visually the way he has (including a red dashed line on y = 0). I have completed the code, but I'm getting an error message and not sure why
This is my code:
-----------------------
Now, while that is showing my plot correctly just as he had on the example, it is giving me this error message when I run it:
"RuntimeWarning: invalid value encountered in divide return np.where(x == 0, 1, np.sin(x)/x)"
I'm not sure why though, and I don't want to mess it up. Please keep in mind this is my VERY first time EVER using Python (and I don't know MATLAB either), so I compiled that part based off the best way I could figure out to do it. Would there be a better way to code that part, or is there just something slight I need to adjust in it? Again, please keep in mind it is plotting correctly as-is, so I have what I need, I just want to clean up whatever the problem is. Help would be greatly appreciated, as I can't figure it out. Every time I try to adjust it, it ends up not working anymore.
Python:
import numpy as np
import matplotlib.pyplot as plt
def sinc(x):
return np.where(x == 0, 1, np.sin(x)/x)
x = np.linspace(0, 25, 500) # mainly for setting the number of values generated
y = sinc(x)
plt.xlim(0, 25) # set range for x axis
plt.ylim(-0.4, 1) # set range for y axis
plt.plot(x, y, color='green', linewidth=1.5, label='sinc(x)') # sin(x)/x graph
plt.axhline(0, color='red', linewidth=1, linestyle='--', label='y=0') # y=0 graph
plt.xlabel('x')
plt.ylabel('sinc(x)')
# obviously these 2 are for the x and y axes labels
plt.show()
Now, while that is showing my plot correctly just as he had on the example, it is giving me this error message when I run it:
"RuntimeWarning: invalid value encountered in divide return np.where(x == 0, 1, np.sin(x)/x)"
I'm not sure why though, and I don't want to mess it up. Please keep in mind this is my VERY first time EVER using Python (and I don't know MATLAB either), so I compiled that part based off the best way I could figure out to do it. Would there be a better way to code that part, or is there just something slight I need to adjust in it? Again, please keep in mind it is plotting correctly as-is, so I have what I need, I just want to clean up whatever the problem is. Help would be greatly appreciated, as I can't figure it out. Every time I try to adjust it, it ends up not working anymore.
Last edited by a moderator: