Vibration Simulation using Python
Distance from Equilibrium over Time Interval |
The vibration simulation was made using Python. First, specify vibration parameters which will be used on vibration position formula, consisted of Amplitude and angular velocity. Then, create a number of time data. For example, the simulation use 50 points data which is started from 0 and end at 49. Calculate position for each time data and plot them using python matplotlib.
Vibration Position Equation
- y is movement position
- A is vibration amplitude
- w is angular velocity
- t is time
Amplitude and angular velocity is constant, as vibration property, while time changes of a certain interval. We can say that vibration position is a function of time.
Vibration Position Simulation using Python
Full python script of vibration simulation can be accessed at:
https://github.com/WahyuNoorIchwan/physics/blob/main/vibration_model.py
Vibration Simulation Script Explanation
The python script will be elaborated by the following explanation.
2 import matplotlib.pyplot as plt
3
4 # Parameters
5 A = 10
6 w = 0.25
7 n = 50
8
9 # Model Matrix - t and y
10 model = np.zeros((n, 2))
11 model[:, 0] = range(50)
12
13 # Calculate y
14 for i in range(n):
15 model[i, 1] = A*np.sin(w*model[i, 0])
16
17 # Plot Vibration Model
18 fig, ax = plt.subplots()
19 fig.set_size_inches(8, 4)
20
21 ax.scatter(model[:, 0], model[:, 1])
22 ax.set_title("Vibration Simulation")
23 ax.set_xlabel("Time")
24 ax.set_ylabel("Deviation")
25
26 plt.show()
- First, import numpy as np and matplotlib.pyplot as plt. These modules will handle mathematical operation and data plot.
- Define vibrations constants at line 5 to 7. Where A is amplitude, w is angular velocity, and n is number of sampling data.
- Create an zeros matrix using np.zeros, the dimension is [n, 2]. First column is filled with time sampling, and second column is calculated vibration position.
- Fill first column using range(n). The function will create range of data started from 0 and end at n-1. So, first column will be filled with 0, 1, 2, 3, to 49.
- Calculate position using for loop at line 14 and 15. The loop using i as index. Fill model[i, 1] using vibration position equation.
- After the loop, model matrix will has 50 pair of time and position.
- Plot model using matplotlib scatter plot.
- Create plot canvas and axis (fig, ax) at line 18 and 19. The data will be plotted at axis ax.
- Create scatter plot at line 21, using ax.scatter(x, y). Use time (model[:, 0]) as x, and position (model[:, 1]) as y.
- Add title, x axis label, and y axis label into ax.
- Show the graph using plt.show.
Posting Komentar untuk "Vibration Simulation using Python"