import numpy as np
import matplotlib.pyplot as plt
width, height = 800, 400
frequency = 0.1
amplitude = 10
x = np.linspace(0, 10, width)
y = np.linspace(0, 5, height)
X, Y = np.meshgrid(x, y)
Z = amplitude*np.sin(2*np.pi*frequency*X+np.pi/4*np.sin(2*np.pi*frequency* Y))
plt.figure(figsize=(6,6))
plt.imshow(Z,cmap='copper',extent=(0,10,0,5))
plt.colorbar(label='Height')
plt.title('Sand Dune Ripple Pattern')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Import Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
Import essential libraries for numerical operations
and 3D plotting.
2. Create 2D Grid
x = np.linspace(-10, 10, 200)
y = np.linspace(-10, 10, 200)
X, Y = np.meshgrid(x, y)
Define linearly spaced points for x and y axes.
Create a mesh grid covering the 2D plane where waves
will be calculated.
3. Define Signal Sources
sources = [
{'center':
(-3, -3), 'freq': 2.5},
{'center':
(3, 3), 'freq': 3.0},
{'center':
(-3, 3), 'freq': 1.8},
]
Define multiple wave sources with positions and
frequencies.
4. Initialize Amplitude Matrix
Z = np.zeros_like(X)
Initialize a zero matrix to store combined wave
amplitudes for each grid point.
5. Calculate Wave Contributions from
Each Source
for src in sources:
dx = X -
src['center'][0]
dy = Y -
src['center'][1]
r =
np.sqrt(dx**2 + dy**2) + 1e-6
Z +=
np.sin(src['freq'] * r) / r
For each source:
Compute distance from every grid point to the
source.
Calculate decaying sine wave amplitude.
Add this to the total amplitude matrix.
6. Set Up 3D Plot
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
Create a figure and add a 3D subplot.
7. Plot the Signal Interference Mesh
ax.plot_wireframe(X, Y, Z, rstride=3, cstride=3,
color='mediumblue', alpha=0.8, linewidth=0.5)
Plot the wireframe mesh representing the combined
wave interference pattern.
8. Add Titles and Labels
ax.set_title("Signal Interference Mesh",
fontsize=16)
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Amplitude")
Label the plot and axes.
9. Adjust Aspect Ratio
ax.set_box_aspect([1,1,0.5])
Adjust the 3D box aspect ratio for better
visualization.
10. Display the Plot
plt.tight_layout()
plt.show()
Optimize layout and render the final plot on screen.


0 Comments:
Post a Comment