-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
72 lines (52 loc) · 1.28 KB
/
test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from math import sin
import numpy as np
import finitedifferences
from matplotlib import pyplot as plt
import time
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits import mplot3d
def solver(A,f,u,h,k):
u[:,0]=f[:]
for i in range(1,u.shape[1]):
u[:,i]=u[:, i-1]+k/h*A @ u[:,i-1]
return u
def RK4(A,f,u,h,k):
u[:,0]=f[:]
for i in range(1,u.shape[1]):
k1 = k/h*A @ u[:,i-1]
k2 = k/h*A @ (u[:,i-1] + 0.5*k1)
k3 = k/h*A @ (u[:,i-1] + 0.5*k2)
k4 = k/h*A @ (u[:,i-1] + k3)
u[:,i] = u[:,i-1] + 1/6*(k1 + 2*k2 + 2*k3 + k4)
return u
def uExc(u,T,k,x):
uExc = u.copy()
for i in range(1,u.shape[1]):
t=k*i
uExc[:,i]=np.sin(2*3.1415*(x + t))
return uExc
T=4
m=12
n = 200
u = np.zeros([m,n])
h=1/m
k = T/n
A = finitedifferences.centralDifference(m)
x = np.linspace(0,1,m)
f = np.sin(2*3.1415*x)
print(u.shape[1])
u = RK4(A,f,u,h,k)
X,Y=np.meshgrid(np.linspace(0,1,m),np.linspace(0,T,n))
print(X.shape)
print(Y.shape)
print(u.shape)
print(k/h)
print(A)
uE = uExc(u,T,k,x)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, u.T,cmap='viridis', edgecolor='none')
ax.set_xlabel('X axis')
ax.set_ylabel('t axis')
plt.show()