Two-dimensional sinc function

I am trying to write a function for a 2D sinc function: y = (sin(x1)*sin(x2)) / (x1*x2) uniformly on the input range [-10, 10] for both x1 and x2. My approach looks like:

import numpy as np
import math 

x1 = np.linspace(-10, 10, 50)
x2 = np.linspace(-10, 10, 50)

array = []

for i in range(len(x1,x2)):
    array.append(math.sin(x1,x2[i]))
    i += 1
print("array : ", array) 

But I receive this error: len() takes exactly one argument (2 given).

Upvotes: 0

Views: 1661

Answers (2)

Woodford
Woodford

Reputation: 4449

There might a way to use np.sinc() directly but I'm not sure offhand how to generate the proper input matrix. Here's a more brute force approach that gets you where (I think) you want to be:

import numpy as np
import matplotlib.pyplot as plt

sinc2d = np.zeros((50, 50))
for x, x1 in enumerate(np.linspace(-10, 10, 50)):
     for y, x2 in enumerate(np.linspace(-10, 10, 50)):
         sinc2d[x,y] = np.sin(x1) * np.sin(x2) / (x1*x2)

# equivalently (thanks @Kelly Bundy):
x1 = np.linspace(-10, 10, 50)
x2 = np.linspace(-10, 10, 50)
sinc2d = np.outer(np.sin(x1), np.sin(x2)) / np.outer(x1, x2)

plt.imshow(sinc2d)
plt.show()

enter image description here

Upvotes: 2

RooRoo
RooRoo

Reputation: 16

As the error describes, the len() function only takes one input. On line 9, you have iterate from len(x1) to len(x2): for i in range(len(x1),len(x2)):

Upvotes: 0

Related Questions