-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kdw.py
34 lines (30 loc) · 996 Bytes
/
Kdw.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
# import packages
import numpy as np
import matplotlib.pyplot as plt
# Korteweg-deVries Numerical Solution
# initial condition
k = 1*10**(-7) # temporal step
n = 1000 # spatial number
h = 1/n # spatial step
delta = 0.022 # who the fuck knows
t = np.arange(0,.0001,k)
x = np.arange(0,2, h)
#
u = np.zeros([len(x),len(t)]) # generate empty position array
for i in range(0,len(x)):
u[i,0] = np.cos(np.pi*x[i])
for j in range(1,len(t)-1):
for i in range(len(x)):
i = i % 2000
u[i,j+1] = u[i,j-1] - 1/3*(k/h)*(u[ (i+1 )% 2000,j]+u[i,j]+u[i-1,j])*(u[( i+1 )% 2000,j]-u[i-1,j]) \
- (delta**2*(k/h)**3)*(u[ (i+2 ) % 2000,j]-2*u[ ( i+1 ) % 2000,j]+2*u[i-1,j]-u[i-2,j])
# plotting
from pylab import clf, plot, xlim, ylim, show, pause
for i in range(100):
clf() # clear the plot
plt.plot(u[:,i])
plt.draw()
plt.title('Velocity Time Evolution', fontsize = 12)
plt.xlabel('Position (m)', fontsize = 10)
plt.ylabel('Velocity (m/s)', fontsize = 10)
pause(0.1)