-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab08_Q2.py
58 lines (50 loc) · 1.81 KB
/
Lab08_Q2.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
54
55
56
57
58
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import seaborn as sns
sns.set()
# Defining Constants
h = 10e-6 # time step
L = 1 # Length of wire in metres
v = 100 # velocity in m/s
d = 0.1 # distance of hammer
C = 1 # 1 m/s
sigma = 0.3 # sigma in metres
N = 100 # grid spacings
a = L/N
dt= 0.003 # time step
dt= dt= 0.01
iterations = int(dt/h)
# initialize arrays
displacement = np.zeros([iterations,N+1],float)
velocity = np.zeros([iterations,N+1],float)
x = np.linspace(0,L,N+1)
velocity[0,:] = C * x*(L-x)/L**2*np.exp( -1*(x-d)**2/(2*sigma**2)) # initial velocity
for i in range(iterations-1): # time iteration
for j in range(1,len(x)-1): # space iterations
displacement[i+1,j] = displacement[i,j] + h*velocity[i,j] # equations given in textbooks
velocity[i+1,j] = velocity[i,j] + \
h*(v**2)/(a**2)*(displacement[i,j+1] + displacement[i,j-1] - 2*displacement[i,j]) # equations given in textbooks
#plotting functions
from pylab import clf, plot, xlim, ylim, show, pause
for i in range((iterations//10)-1):
clf() # clear the plot
plt.plot(x,velocity[i*10,:])#,s = 3)
ylim([-.2, .2]) # set the x boundaries constant
plt.draw()
plt.title('Velocity Time Evolution', fontsize = 12)
plt.xlabel('Position (m)', fontsize = 10)
plt.ylabel('Velocity (m/s)', fontsize = 10)
pause(0.01)
#pause to allow a smooth animation
from pylab import clf, plot, xlim, ylim, show, pause
for i in range((iterations//10)-1):
clf() # clear the plot
plt.plot(x,displacement[i*10,:])#,s = 3)
ylim([-.0015, .0015]) # set the x boundaries constant
plt.title('Displcement Time Evolution',fontsize = 12)
plt.xlabel('Position (m)', fontsize = 10)
plt.ylabel('displacement (m)', fontsize = 10)
plt.draw()
pause(0.01)
#pause to allow a smooth animation