Sunday, 6 April 2025

Wave Interference Pattern using Python

 


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.


Related Posts:

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (98) AI (41) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (199) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (86) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1057) Python Coding Challenge (461) Python Quiz (129) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)