Skip to content

Commit fe7e1cc

Browse files
committed
add scipy learning
1 parent 43d489b commit fe7e1cc

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed

scipy/001_special.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
# FileName: 001_special.py
4+
#
5+
# Description:
6+
#
7+
# Version: 1.0
8+
# Created: 2018-06-13 09:13:12
9+
# Last Modified: 2018-06-13 09:26:10
10+
# Revision: none
11+
# Compiler: gcc
12+
#
13+
# Author: zt ()
14+
# Organization:
15+
16+
import matplotlib.pyplot as plt
17+
18+
from numpy import *
19+
from scipy.special import jn, yn, jn_zeros, yn_zeros
20+
21+
n = 0
22+
x = 0.0
23+
24+
print("J_%d(%f)=%f" % (n, x, jn(n, x)))
25+
26+
x = 1.0
27+
print("Y_%d(%f)=%f" % (n, x, yn(n, x)))
28+
29+
x = linspace(0, 10, 100)
30+
31+
fig, ax = plt.subplots()
32+
33+
for n in xrange(4):
34+
ax.plot(x, jn(n, x), label=r"$J_%d(x)$" % n)
35+
ax.legend()
36+
37+
plt.show()
38+
39+
n = 0
40+
m = 4
41+
jn_zeros(n, m)

scipy/002_integrate.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
# FileName: 002_integrate.py
4+
#
5+
# Description:
6+
#
7+
# Version: 1.0
8+
# Created: 2018-06-13 09:26:33
9+
# Last Modified: 2018-06-13 10:22:12
10+
# Revision: none
11+
# Compiler: gcc
12+
#
13+
# Author: zt ()
14+
# Organization:
15+
16+
from numpy import *
17+
from scipy.integrate import quad, dblquad, tplquad
18+
from scipy.special import jn, yn, jn_zeros, yn_zeros
19+
20+
21+
def f(x):
22+
return x
23+
24+
25+
x_lower = 0
26+
x_upper = 1
27+
28+
val, abserr = quad(f, x_lower, x_upper)
29+
30+
print("integral value=", val, ",absolute error=", abserr)
31+
32+
33+
def integrand(x, n):
34+
return jn(n, x)
35+
36+
37+
x_lower = 0
38+
x_upper = 10
39+
40+
val, abserr = quad(integrand, x_lower, x_upper, args=(3, ))
41+
print("integral value=", val, ",absolute error=", abserr)
42+
43+
val, abserr = quad(lambda x: exp(-x**2), -Inf, Inf)
44+
print("integral value=", val, ",absolute error=", abserr)
45+
46+
analytical = sqrt(pi)
47+
print("analytical=", analytical)
48+
49+
50+
def integrand(x, y):
51+
return exp(-x**2 - y**2)
52+
53+
54+
x_lower = 0
55+
x_upper = 10
56+
y_lower = 0
57+
y_upper = 10
58+
59+
val, abserr = dblquad(integrand, x_lower, x_upper, lambda x: y_lower,
60+
lambda x: y_upper)
61+
print("integral value=", val, ",absolute error=", abserr)

scipy/003_02_ode.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
# FileName: 003_02_ode.py
4+
#
5+
# Description:
6+
#
7+
# Version: 1.0
8+
# Created: 2018-06-13 10:22:44
9+
# Last Modified: 2018-06-13 10:30:22
10+
# Revision: none
11+
# Compiler: gcc
12+
#
13+
# Author: zt ()
14+
# Organization:
15+
16+
import matplotlib.pyplot as plt
17+
from numpy import *
18+
from scipy.integrate import quad, dblquad, tplquad
19+
from scipy.integrate import odeint, ode
20+
from scipy.special import jn, yn, jn_zeros, yn_zeros
21+
22+
23+
def dy(y, t, zeta, w0):
24+
x, p = y[0], y[1]
25+
dx = p
26+
dp = -2 * zeta * w0 * p - w0**2 * x
27+
return [dx, dp]
28+
29+
30+
y0 = [1.0, 0.0]
31+
t = linspace(0, 10, 1000)
32+
w0 = 2 * pi * 1.0
33+
34+
y1 = odeint(dy, y0, t, args=(0.0, w0))
35+
y2 = odeint(dy, y0, t, args=(0.2, w0))
36+
y3 = odeint(dy, y0, t, args=(1.0, w0))
37+
y4 = odeint(dy, y0, t, args=(5.0, w0))
38+
39+
fig, ax = plt.subplots()
40+
ax.plot(t, y1[:, 0], 'k', label="undamped", linewidth=0.25)
41+
ax.plot(t, y2[:, 0], 'r', label="under damped")
42+
ax.plot(t, y3[:, 0], 'g', label=r"critical damping")
43+
ax.plot(t, y4[:, 0], 'b', label="over undamped")
44+
ax.legend()
45+
plt.show()

scipy/004_fftpack.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
#-*- coding:utf-8 -*-
3+
# FileName: 004_fftpack.py
4+
#
5+
# Description:
6+
#
7+
# Version: 1.0
8+
# Created: 2018-06-13 10:31:32
9+
# Last Modified: 2018-06-13 10:44:29
10+
# Revision: none
11+
# Compiler: gcc
12+
#
13+
# Author: zt ()
14+
# Organization:
15+
16+
import matplotlib.pyplot as plt
17+
from numpy import *
18+
from scipy.integrate import quad, dblquad, tplquad
19+
from scipy.integrate import odeint, ode
20+
from scipy.special import jn, yn, jn_zeros, yn_zeros
21+
22+
from numpy.fft import fftfreq
23+
from scipy.fftpack import *
24+
25+
26+
def dy(y, t, zeta, w0):
27+
x, p = y[0], y[1]
28+
dx = p
29+
dp = -2 * zeta * w0 * p - w0**2 * x
30+
return [dx, dp]
31+
32+
33+
y0 = [1.0, 0.0]
34+
t = linspace(0, 10, 1000)
35+
w0 = 2 * pi * 1.0
36+
37+
y1 = odeint(dy, y0, t, args=(0.0, w0))
38+
y2 = odeint(dy, y0, t, args=(0.2, w0))
39+
y3 = odeint(dy, y0, t, args=(1.0, w0))
40+
y4 = odeint(dy, y0, t, args=(5.0, w0))
41+
42+
# fig, ax = plt.subplots()
43+
# ax.plot(t, y1[:, 0], 'k', label="undamped", linewidth=0.25)
44+
# ax.plot(t, y2[:, 0], 'r', label="under damped")
45+
# ax.plot(t, y3[:, 0], 'g', label=r"critical damping")
46+
# ax.plot(t, y4[:, 0], 'b', label="over undamped")
47+
# ax.legend()
48+
# plt.show()
49+
50+
N = len(t)
51+
dt = t[1] - t[0]
52+
53+
# print(y2)
54+
F = fft(y2[:, 0])
55+
W = fftfreq(N, dt)
56+
57+
fig, ax = plt.subplots(figsize=(9, 3))
58+
ax.plot(W, abs(F))
59+
60+
indices = where(W > 0)
61+
W_pos = W[indices]
62+
F_pos = F[indices]
63+
64+
fig, ax = plt.subplots(figsize=(9, 3))
65+
ax.plot(W_pos, abs(F_pos))
66+
ax.set_xlim(0, 5)
67+
plt.show()

0 commit comments

Comments
 (0)