0% found this document useful (0 votes)
10 views2 pages

Import Numpy As NP

The document is a Python script that simulates the motion of a pendulum by calculating and plotting the angular position, velocity, and acceleration based on user-defined initial and final conditions. It uses polynomial equations to solve for motion coefficients and visualizes the results using Matplotlib. The script includes functions for solving motion equations and plotting both angular and linear motion graphs.

Uploaded by

John H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Import Numpy As NP

The document is a Python script that simulates the motion of a pendulum by calculating and plotting the angular position, velocity, and acceleration based on user-defined initial and final conditions. It uses polynomial equations to solve for motion coefficients and visualizes the results using Matplotlib. The script includes functions for solving motion equations and plotting both angular and linear motion graphs.

Uploaded by

John H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import numpy as np

from numpy.linalg import solve


import matplotlib.pyplot as plt

def main():
# Get user inputs for initial and final conditions
theta0r = float(input("Enter initial angular position in degree: "))
theta0 = np.radians(theta0r)
omega0 = float(input("Enter initial angular velocity in radians/s: "))
alpha0 = float(input("Enter initial angular acceleration in radians/s^2: "))
thetafr = float(input("Enter final angular position in degree: "))
thetaf = np.radians(thetafr)
omegaf = float(input("Enter final angular velocity in radians/s: "))
alphaf = float(input("Enter final angular acceleration in radians/s^2: "))
t0 = float(input("Enter initial time: "))
tf = float(input("Enter final time: "))
L = float(input("Enter the length of the arm: "))

# Solve for the polynomial coefficients


coefficients = solve_motion_equations(theta0, omega0, alpha0, thetaf, omegaf,
alphaf, t0, tf)

# Print the equations for angular position, velocity, and acceleration


print("Angular Position Equation: θ(t) = {:.6f} + {:.6f}t + {:.6f}t^2 +
{:.6f}t^3 + {:.6f}t^4 + {:.6f}t^5".format(*coefficients))
print("Angular Velocity Equation: ω(t) = {:.6f} + {:.6f}t + {:.6f}t^2 +
{:.6f}t^3 + {:.6f}t^4".format(coefficients[1], 2*coefficients[2],
3*coefficients[3], 4*coefficients[4], 5*coefficients[5]))
print("Angular Acceleration Equation: α(t) = {:.6f} + {:.6f}t + {:.6f}t^2 +
{:.6f}t^3".format(2*coefficients[2], 6*coefficients[3], 12*coefficients[4],
20*coefficients[5]))

# Plot the graphs for angular position, velocity, and acceleration


plot_motion_graphs(coefficients, t0, tf, L)

def solve_motion_equations(theta0, omega0, alpha0, thetaf, omegaf, alphaf, t0, tf):


# Create the matrix using the boundary conditions
M = np.array([
[1, t0, t0**2, t0**3, t0**4, t0**5],
[0, 1, 2*t0, 3*t0**2, 4*t0**3, 5*t0**4],
[0, 0, 2, 6*t0, 12*t0**2, 20*t0**3],
[1, tf, tf**2, tf**3, tf**4, tf**5],
[0, 1, 2*tf, 3*tf**2, 4*tf**3, 5*tf**4],
[0, 0, 2, 6*tf, 12*tf**2, 20*tf**3]
])

# Create the vector for the boundary conditions


b = np.array([theta0, omega0, alpha0, thetaf, omegaf, alphaf])
# Solve the linear system for the coefficients
return solve(M, b)

def plot_motion_graphs(coefficients, t0, tf, L):


# Generate time values for the plots
t_values = np.linspace(t0, tf, 300)
# Calculate the angular position, velocity, and acceleration values
theta_values = np.polyval(coefficients[::-1], t_values)
omega_values = np.polyval(np.polyder(coefficients[::-1]), t_values)
alpha_values = np.polyval(np.polyder(coefficients[::-1], 2), t_values)
# Calculate the linear velocity and acceleration
vx_values = -L * omega_values * np.sin(theta_values)
vy_values = L * omega_values * np.cos(theta_values)
ax_values = -L * (alpha_values * np.sin(theta_values) + omega_values**2 *
np.cos(theta_values))
ay_values = L * (alpha_values * np.cos(theta_values) - omega_values**2 *
np.sin(theta_values))

# Plot the results for angular motion


fig, axs = plt.subplots(3, 1, figsize=(10, 15))
axs[0].plot(t_values, theta_values, label='Angular Position (θ(t))')
axs[1].plot(t_values, omega_values, label='Angular Velocity (ω(t))',
color='orange')
axs[2].plot(t_values, alpha_values, label='Angular Acceleration (α(t))',
color='red')

for ax in axs:
ax.legend()
ax.grid(True)

axs[0].set_title('Angular Position (θ(t))')


axs[1].set_title('Angular Velocity (ω(t))')
axs[2].set_title('Angular Acceleration (α(t))')

axs[2].set_xlabel('Time (t)')
axs[0].set_ylabel('θ(t)')
axs[1].set_ylabel('ω(t)')
axs[2].set_ylabel('α(t)')

plt.tight_layout()
plt.show()

# Plot the results for linear motion


fig, axs = plt.subplots(2, 1, figsize=(10, 10))
axs[0].plot(t_values, vx_values, label='Linear Velocity x (vx(t))',
color='blue')
axs[0].plot(t_values, vy_values, label='Linear Velocity y (vy(t))',
color='green')
axs[1].plot(t_values, ax_values, label='Linear Acceleration x (ax(t))',
color='purple')
axs[1].plot(t_values, ay_values, label='Linear Acceleration y (ay(t))',
color='brown')

for ax in axs:
ax.legend()
ax.grid(True)

axs[0].set_title('Linear Velocity')
axs[1].set_title('Linear Acceleration')

axs[1].set_xlabel('Time (t)')
axs[0].set_ylabel('Velocity (m/s)')
axs[1].set_ylabel('Acceleration (m/s^2)')

plt.tight_layout()
plt.show()

if __name__ == "__main__":
main()

You might also like