Sunday, 18 May 2025

Sand Dune Ripple Pattern using Python

 

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

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)