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
Create a Cartesian x,y grid by defining
x
andy
as arrays of evenly spaced numbers over the interval[-15, 15]
. Each of these arrays should have101
numbers.
# Your code goes here
Reshape the y array into a “column” vector by increasing the dimensionality using
np.newaxis
y = # Your code goes here
Create an array of
r
that corresponds to the following equation:
\[ \large r = \sqrt{x^2 + y^2} \]
# Your code goes here
Calculate the sinc function of
r
sinc = # Your code goes here
Replace any location in
sinc
wherer
is0
with1.0
.
# Your code goes here
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