-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumerical_integration.py
71 lines (51 loc) · 1.28 KB
/
numerical_integration.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
# In[2]:
def euler_rate(rate,time,x0):
time = np.array(time); f = np.array(rate)
dt = time[1]-time[0]
x = x0*np.ones(np.size(time))
for i,t in enumerate(time[0:-1]):
dx = f[i]*dt
x[i+1] = x[i]+dx
return x
# In[3]:
def eulerA(A,x0,y0,T,dt):
time = np.arange(0,T,dt)
x = np.ones(np.size(time))*x0
y = np.ones(np.size(time))*y0
#nth variable
for i,t in enumerate(time[0:-1]):
v = np.array([[x[i]], [y[i]]])
dv = np.dot(A,v)*dt
v = v+dv
x[i+1] = v[0]
y[i+1] = v[1]
#nth variable
return x,y,time #,n
# In[4]:
def eulerFG(f,g,x0,y0,T,dt):
time = np.arange(0,T,dt)
x = np.ones(np.size(time))*x0
y = np.ones(np.size(time))*y0
#nth variable
for i,t in enumerate(time[0:-1]):
dx = f(x[i],y[i])*dt
dy = g(x[i],y[i])*dt
x[i+1] = x[i]+dx
y[i+1] = y[i]+dy
#nth variable
return x,y,time #,n
# In[5]:
def vectorize(f,g,ax_max,sp):
#creating the meshgrid
x = np.arange(0, ax_max, sp)
y = np.arange(0, ax_max, sp)
X, Y = np.meshgrid(x, y, sparse = True)
U = f(X,Y)
V = g(X,Y)
C = np.sqrt((U**2)+(V**2))
U = U/C; V = V/C
return U,V,X,Y,C