Numpy Lab: Sinc Function#

The sinc function is defined by:

\[ \large sinc(r) = \frac{sin(r)}{r} \]
  • Objective: Create a 2D plot of this function.

  • Topics: Broadcasting, Fancy Indexing, 2D plotting

Type your code below#

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import plotly.graph_objects as go 
  1. Create a Cartesian x,y grid by defining x and y as arrays of evenly spaced numbers over the interval [-15, 15]. Each of these arrays should have 101 numbers.

# Your code goes here
  1. Reshape the y array into a “column” vector by increasing the dimensionality using np.newaxis

y = # Your code goes here
  1. Create an array of r that corresponds to the following equation:

\[ \large r = \sqrt{x^2 + y^2} \]
# Your code goes here
  1. Calculate the sinc function of r

sinc = # Your code goes here
  1. Replace any location in sinc where r is 0 with 1.0.

# Your code goes here
  1. Plot the result of the sinc function:

plt.pcolormesh(X, Y, sinc) # for a matplotlib 2D plot
#3D surface plot using plotly
surface = go.Surface(x=x, 
                     y=y, 
                     z=sinc)

# Create fig using surface plot
fig = go.Figure(data=[surface])  
  
fig.show() 

Bonus question. Go back to cell of step 2. Instead of using np.newaxis on step 2 use meshgrid to create 2D arrays for x and y. Resulting r on step 3 should be the same