Lompat ke konten Lompat ke sidebar Lompat ke footer

Vibration Simulation using Python

Vibration is a motion which occur around an equilibrium state. The motion started from farthest point from and equilibrium, which is called amplitude. Then,  it start to move into equilibrium, and going to pass it. After reaching amplitude, it stop and begin to move back into equilibrium, up to initial position.

The movement will be repeated in a certain time interval. The position from equilibrium is changed over the time. For example, when time is 0, position is 0 or in equilibrium, when time is 10 the position is 5.0 from equilibrium, etc. The position forms sinusoidal pattern over the time, we can see the pattern from vibration simulation from the following image.
plot_simpangan_getaran_matplotlib_python
Distance from Equilibrium over Time Interval
 
The simulated vibration movement has amplitude of  10 and started from 0 when time is 0. First, the distance is increasing toward amplitude value. After reaching the amplitude, the distance starts to decrease up to minus amplitude.

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

 Vibration movement position at certain time is formulated as:
 
$ y = A sin(wt) $
Where:
  • 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

Let's use previous equation to make vibration simulation using Python. As stated before, first define vibration constants/parameters, Amplitude, angular velocity, and number of time sampling. Then, create n sampling of time, started from 0 with 1 interval.

Calculate position from each time, so in the end there n samplings of time and position. Plot the samplings using matplotlib scatter plot. Time is plotted on x axis and position in y axis.

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.

1   import numpy as np
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"