-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpendulum motion.py
53 lines (43 loc) · 1.43 KB
/
pendulum motion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import matplotlib.pyplot as plt
def simple_pendulum(length, mass, gravity, dt, total_time):
# Initial conditions
theta = 0.1 # Initial angle (radians)
omega = 0.0 # Initial angular velocity
# Lists to store results
time_values = np.arange(0, total_time, dt)
theta_values = []
omega_values = []
# Euler's method for numerical integration
for t in time_values:
alpha = -gravity / length * np.sin(theta)
omega += alpha * dt
theta += omega * dt
theta_values.append(theta)
omega_values.append(omega)
return time_values, theta_values, omega_values
def plot_pendulum_motion(time, theta, omega):
# Convert theta to x and y positions
length = 1.0 # You can change the length of the string here
x = length * np.sin(theta)
y = -length * np.cos(theta)
# Plot the motion
plt.figure(figsize=(8, 6))
plt.plot(time, x, label='x-position')
plt.plot(time, y, label='y-position')
plt.xlabel('Time (s)')
plt.ylabel('Position (m)')
plt.title('Pendulum Motion')
plt.legend()
plt.grid(True)
plt.show()
# Parameters
string_length = 1.0
pendulum_mass = 0.1
gravity_constant = 9.81
time_step = 0.01
total_simulation_time = 14
# Run simulation
time, theta, omega = simple_pendulum(string_length, pendulum_mass, gravity_constant, time_step, total_simulation_time)
# Plot results
plot_pendulum_motion(time, theta, omega)