Skip to content

Commit 60c2351

Browse files
authored
616161
1 parent de63a7a commit 60c2351

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

docs/1 Physics/3 Waves/Problem_1.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,84 @@ plot_wave(Z1, "Single Source")
218218
plot_wave(Z4, "Four Sources (Square)")
219219
plot_wave(Z5, "Five Sources (Pentagon)")
220220
plt.show() # Moved plt.show() here to display all plots
221+
```
222+
223+
![alt text](def77386-04a4-4bdd-92b9-20d1744532cf.gif)
224+
225+
```python
226+
import numpy as np
227+
import matplotlib.pyplot as plt
228+
import imageio
229+
import os
230+
231+
# Define the grid
232+
x = np.linspace(-10, 10, 500)
233+
y = np.linspace(-10, 10, 500)
234+
X, Y = np.meshgrid(x, y)
235+
236+
# Define wave parameters
237+
wavelength = 2 # Wavelength
238+
k = 2 * np.pi / wavelength # Wave number
239+
omega = 2 * np.pi / 5 # Angular frequency
240+
241+
# Function to create a wave from a single source
242+
def single_wave(X, Y, source, t):
243+
r = np.sqrt((X - source[0])**2 + (Y - source[1])**2)
244+
return np.sin(k * r - omega * t)
245+
246+
# Function to sum multiple waves from different sources
247+
def multiple_waves(X, Y, sources, t):
248+
Z = np.zeros_like(X)
249+
for source in sources:
250+
Z += single_wave(X, Y, source, t)
251+
return Z
252+
253+
# Sources definitions for 5 sources (in a pentagon)
254+
radius = 5
255+
angles = np.linspace(0, 2 * np.pi, 6)[:-1]
256+
sources_5 = [(radius * np.cos(a), radius * np.sin(a)) for a in angles]
257+
258+
# Create 3_waves folder if it doesn't exist
259+
OUTPUT_DIR = "3_waves"
260+
if not os.path.exists(OUTPUT_DIR):
261+
os.makedirs(OUTPUT_DIR)
262+
263+
# Custom color map blending burgundy (#800020) and lighter blue (#4682B4)
264+
colors = ['#800020', '#400010', '#4682B4', '#87CEEB', '#800080']
265+
custom_cmap = plt.cm.colors.LinearSegmentedColormap.from_list('burgundy_lightblue', colors)
266+
267+
# Create GIF frames
268+
num_frames = 100
269+
gif_frames = []
270+
271+
# Create the GIF frames for time from 0 to 2*pi
272+
for i in range(num_frames):
273+
t = i * 2 * np.pi / num_frames # Varying time
274+
Z = multiple_waves(X, Y, sources_5, t)
275+
276+
# Plotting the frame
277+
fig, ax = plt.subplots(figsize=(8, 6))
278+
im = ax.imshow(Z, extent=[-10, 10, -10, 10], origin='lower', cmap=custom_cmap, animated=True)
279+
ax.set_title(f"Interference of 5 Sources - Time = {t:.2f}", fontsize=16)
280+
ax.set_xlabel('X axis', fontsize=14)
281+
ax.set_ylabel('Y axis', fontsize=14)
282+
plt.colorbar(im, ax=ax)
283+
284+
# Ensure proper rendering
285+
plt.tight_layout()
286+
287+
# Draw the figure to make sure it's rendered correctly before saving
288+
fig.canvas.draw()
289+
290+
# Convert to image and append to GIF frames
291+
gif_frames.append(np.array(fig.canvas.renderer.buffer_rgba()))
292+
293+
# Close the plot to avoid memory issues in the loop
294+
plt.close(fig)
295+
296+
# Create and save the GIF in the 3_waves folder
297+
gif_path = os.path.join(OUTPUT_DIR, "interference_5_sources.gif")
298+
imageio.mimsave(gif_path, gif_frames, duration=0.1)
299+
300+
print(f"GIF saved as {gif_path}")
221301
```
7.99 MB
Loading
139 KB
Loading

0 commit comments

Comments
 (0)