import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x=np.linspace(-10,10,300)
y=np.linspace(-10,10,300)
X,Y=np.meshgrid(x,y)
r1=np.sqrt((X+3)**2+(Y+3)**2)
r2=np.sqrt((X-3)**2+(Y-3)**2)
Z=np.sin(r1)+np.sin(r2)
fig=plt.figure(figsize=(10,6))
ax=fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,Z,cmap='viridis',edgecolor='none')
ax.set_title("3D Wave Interferance Surface")
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
NumPy: Used for numerical calculations, array
manipulations, and math functions.
Matplotlib: Used to create static, animated, or
interactive plots.
Axes3D: Enables 3D plotting features in Matplotlib.
2. Define Grid Range for X and Y Axes
x = np.linspace(-10, 10, 300)
y = np.linspace(-10, 10, 300)
X, Y = np.meshgrid(x, y)
np.linspace(...) creates 300 evenly spaced values
from -10 to 10.
np.meshgrid(...) creates 2D grid coordinates from 1D
x and y arrays for surface plotting.
3. Calculate Distance from Two Wave Sources
r1 = np.sqrt((X + 3)**2 + (Y + 3)**2)
r2 = np.sqrt((X - 3)**2 + (Y - 3)**2)
r1: Distance from wave source 1 at point (-3, -3).
r2: Distance from wave source 2 at point (3, 3).
This is based on the Euclidean distance formula.
4. Compute Interference Pattern (Z-axis
Height)
Z = np.sin(r1) + np.sin(r2)
Simulates two wave fronts overlapping.
Adds sine waves from both sources to get
constructive and destructive interference.
Result: Ripple-like surface of varying height
values.
5. Create a 3D Plot Figure
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
fig: Initializes the figure (canvas).
figsize: Width = 10 inches, Height = 6 inches.
ax: Adds a single 3D subplot for plotting the
surface.
6. Plot the 3D Surface
ax.plot_surface(X, Y, Z, cmap='viridis',
edgecolor='none')
Renders the 3D surface using the X, Y, and Z values.
cmap='viridis': Applies a smooth color gradient.
edgecolor='none': Removes mesh edges for a cleaner
look.
7. Set Plot Title and Display
ax.set_title("3D Wave Interference
Surface")
plt.show()
Adds a title to the plot.
plt.show() displays the plot window with the 3D
surface.
0 Comments:
Post a Comment