You can plot [sin(nx)/sin(x)]^2 如何绘制?

huangapple go评论56阅读模式
英文:

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()

我获得以下输出:

You can plot [sin(nx)/sin(x)]^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 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:

You can plot [sin(nx)/sin(x)]^2 如何绘制?

huangapple
  • 本文由 发表于 2023年6月30日 03:37:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定