Friday, 28 March 2025

Pendulum Wave Art Pattern using Python

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

num_pendulums = 15

lengths = np.linspace(1, 2, num_pendulums)

g = 9.81  

time = np.linspace(0, 20, 1000)

periods = 2 * np.pi * np.sqrt(lengths / g)

angles = [np.cos(2 * np.pi * time / T) for T in periods]

x_pos = [l * np.sin(angle) for l, angle in zip(lengths, angles)]

y_pos = [-l * np.cos(angle) for l, angle in zip(lengths, angles)]

fig, ax = plt.subplots(figsize=(8, 8))

ax.set_xlim(-2.5, 2.5)

ax.set_ylim(-2.5, 0.5)

ax.set_aspect('equal')

ax.set_title('Pendulum Wave Art Plot')

lines = [ax.plot([], [], 'o-', markersize=10)[0] for _ in range(num_pendulums)]

def animate(i):

    for j, line in enumerate(lines):

        line.set_data([0, x_pos[j][i]], [0, y_pos[j][i]])

    return lines

ani = animation.FuncAnimation(fig, animate, frames=len(time), interval=30, blit=True)

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
NumPy: For efficient mathematical operations.
Matplotlib: For plotting and visualizing.
Matplotlib Animation: For creating the pendulum wave animation.

2. Initialization
num_pendulums = 15
lengths = np.linspace(1, 2, num_pendulums)
g = 9.81
time = np.linspace(0, 20, 1000)
num_pendulums: Number of pendulums in the animation (15 in this case).
lengths: Linearly spaced pendulum lengths from 1 to 2 meters using np.linspace.
g: Gravitational acceleration (9.81 m/s²).
time: Array representing 1000 time steps from 0 to 20 seconds.

3. Calculating Pendulum Motion
periods = 2 * np.pi * np.sqrt(lengths / g)
Formula Used: The period of a simple pendulum is given by:
T=2π root l/g
T = Period of oscillation
L = Length of the pendulum
g = Gravitational acceleration

4. Calculating Angular Position (θ)
angles = [np.cos(2 * np.pi * time / T) for T in periods]
Using the equation:
θ(t)=cos(2πt/T)
The angles are calculated for each pendulum over time using cosine function for oscillatory motion.

5. Converting to Cartesian Coordinates
x_pos = [l * np.sin(angle) for l, angle in zip(lengths, angles)]
y_pos = [-l * np.cos(angle) for l, angle in zip(lengths, angles)]
The x and y positions of the pendulum are calculated using trigonometry:
x=Lsin(θ)
y=−Lcos(θ)
The negative y ensures the pendulum moves downwards.

6. Plot Setup
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 0.5)
ax.set_aspect('equal')
ax.set_title('Pendulum Wave Art Plot')
A figure of size 8x8 is created.
x and y limits are adjusted to fit all pendulums.
ax.set_aspect('equal') ensures equal scaling on both axes.
A title is added.

7. Creating the Pendulums
lines = [ax.plot([], [], 'o-', markersize=10)[0] for _ in range(num_pendulums)]
The pendulums are visualized using markers ('o-') with a size of 10.
Each pendulum is represented as a line starting from the origin (0, 0).

8. Animation Function
def animate(i):
    for j, line in enumerate(lines):
        line.set_data([0, x_pos[j][i]], [0, y_pos[j][i]])
    return lines
animate() updates the pendulum positions for each frame (i).

set_data() updates the line data to simulate the swinging motion.

9. Creating and Displaying Animation
ani = animation.FuncAnimation(fig, animate, frames=len(time), interval=30, blit=True)
plt.show()
FuncAnimation: Handles the animation by repeatedly calling the animate() function.

interval=30 means each frame is displayed for 30 milliseconds.

blit=True optimizes the rendering for smoother animations.

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) 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 (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1042) Python Coding Challenge (456) Python Quiz (117) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) 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)