英文:
How can I plot [sin(nx)/sin(x)]^2?
问题
I'm trying to plot a function that includes a [sin(n x)/sin(x)]^2 term, however all I'm getting back is an array of nan and this warning:
invalid value encountered in divide
return (np.sin(n * alpha * x * np.pi) / np.sin(alpha * x * np.pi))**2
This is my function:
def fun(x):
return (np.sinc(beta * x)**2) * (np.sin(n * alpha * x * np.pi) / np.sin(alpha * x * np.pi))**2
I get the same problems if I simplify the function to just be (np.sin(n * x) / np.sin(x))**2
.
x
is an 1D np.array of 30000 elements ranging from .001 to 30 in steps of .001 and alpha is ~0.452
I tried making a simple example with linspace
, but now it's working fine.
I'm not sure why I can't recreate this with different data points, but this solution worked decently:
return (np.sin(n * x[np.sin(x) != 0]) / np.sin(x[np.sin(x) != 0]))**2
英文:
I'm trying to plot a function that includes a [sin(n x)/sin(x)]^2 term, however all I'm getting back is an array of nan and this warning:
invalid value encountered in divide
return (np.sin(n * alpha * x * np.pi) / np.sin(alpha * x * np.pi))**2
This is my function:
def fun(x):
return (np.sinc(beta * x)**2) * (np.sin(n * alpha * x * np.pi) / np.sin(alpha * x * np.pi))**2
I get the same problems if I simplify the function to just be (np.sin(n * x) / np.sin(x))**2
.
x
is an 1D np.array of 30000 elements ranging from .001 to 30 in steps of .001 and alpha is ~0.452
I tried making a simple example with linspace
, but now it's working fine.
I'm not sure why I can't recreate this with different data points, but this solution worked decently:
return (np.sin(n * x[np.sin(x) != 0]) / np.sin(x[np.sin(x) != 0]))**2
答案1
得分: 2
The reason, you are getting the error, is because you divided by zero. That is trivial. As you know that your function is well defined and continuous at these critical points (I assume you did the mathematical proof on your own), you can play a little trick to get the plot, which is what you asked for:
只要 sin(x)!=0
的点。
例如,以下代码片段会重新创建您的错误:
x = linspace(-pi, pi, 1000)
for n in range(10):
y = (sin(n * x) / sin(x))**2
plot(x,y,label=n)
legend()
而以下代码不会出现错误,因为 x 与 pi 和零略有不同:
x = linspace(-3.1415, 3.1415, 1000)
for n in range(10):
y = (sin(n * x) / sin(x))**2
plot(x,y,label=n)
legend()
我获得以下输出:
英文:
The reason, you are getting the error, is because you divided by zero. That is trivial. As you know that your function is well defined and continuous at these critical points (I assume you did the mathetmatical proof on your own), you can play a little trick to get the plot, which is what you asked for:
Plot only points, where sin(x)!=0
.
For example, the following snippet will recreate you error:
x = linspace(-pi, pi, 1000)
for n in range(10):
y = (sin(n * x) / sin(x))**2
plot(x,y,label=n)
legend()
while the following won't, because x is slightly different from pi and zero:
x = linspace(-3.1415, 3.1415, 1000)
for n in range(10):
y = (sin(n * x) / sin(x))**2
plot(x,y,label=n)
legend()
I get the following output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论