Skip to content

Commit 2cc9026

Browse files
authored
Jupyter notebook version added
1 parent 708b634 commit 2cc9026

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

ch06/Program_6.2_eigh.ipynb

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 6.2: Diagonalization of triatomic systems (eigh.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import numpy as np\n",
15+
"from scipy.linalg import eigh # Hermitian eigenvalue solver\n",
16+
"\n",
17+
"k = 1.0\n",
18+
"m1, m2, m3 = 1./4., 2./5., 1./4.\n",
19+
"A = np.array([[k, -k, 0], [-k, 2*k, -k], [0, -k, k]])\n",
20+
"B = np.array([[m1, 0, 0], [0, m2, 0], [0, 0, m3]])\n",
21+
"\n",
22+
"lamb, u = eigh(A, B) # eigenvalues and eigenvectors \n",
23+
"print (np.sqrt(lamb)) # print omega"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {
30+
"collapsed": true
31+
},
32+
"outputs": [],
33+
"source": []
34+
}
35+
],
36+
"metadata": {
37+
"kernelspec": {
38+
"display_name": "Python 3",
39+
"language": "python",
40+
"name": "python3"
41+
},
42+
"language_info": {
43+
"codemirror_mode": {
44+
"name": "ipython",
45+
"version": 3
46+
},
47+
"file_extension": ".py",
48+
"mimetype": "text/x-python",
49+
"name": "python",
50+
"nbconvert_exporter": "python",
51+
"pygments_lexer": "ipython3",
52+
"version": "3.6.1"
53+
}
54+
},
55+
"nbformat": 4,
56+
"nbformat_minor": 2
57+
}

ch06/Program_6.3_stringfdm.ipynb

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 6.3: String under external forces (stringfdm.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"from scipy.linalg import solve # SciPy linear eqn solver\n",
15+
"import numpy as np, matplotlib.pyplot as plt\n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"N, u0, uN= 20, 0., 0. # number of intervals, boundary values\n",
19+
"x = np.linspace(0., 1., N+1) # grid\n",
20+
"h, T, f = x[1]-x[0], 1.0, -1.0 # bin size, tension, load\n",
21+
"\n",
22+
"A = np.diag([-2.]*(N-1)) # diagonal \n",
23+
"A += np.diag([1.]*(N-2),1) + np.diag([1.]*(N-2),-1) # off diagonals\n",
24+
" \n",
25+
"B = np.array([-h*h*f/T]*(N-1)) # B matrix\n",
26+
"B[0], B[-1] = B[0]-u0, B[-1]-uN # boundary values, just in case\n",
27+
"\n",
28+
"u = solve(A, B) # solve \n",
29+
"u = np.insert(u, [0, N-1], [u0, uN]) # insert BV at 1st and last pos \n",
30+
"\n",
31+
"plt.plot(x, u, label='$f=-1$'), plt.legend(loc='center') # legend \n",
32+
"plt.xlabel('$x$'), plt.ylabel('$u$'), plt.show()"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": []
43+
}
44+
],
45+
"metadata": {
46+
"kernelspec": {
47+
"display_name": "Python 3",
48+
"language": "python",
49+
"name": "python3"
50+
},
51+
"language_info": {
52+
"codemirror_mode": {
53+
"name": "ipython",
54+
"version": 3
55+
},
56+
"file_extension": ".py",
57+
"mimetype": "text/x-python",
58+
"name": "python",
59+
"nbconvert_exporter": "python",
60+
"pygments_lexer": "ipython3",
61+
"version": "3.6.1"
62+
}
63+
},
64+
"nbformat": 4,
65+
"nbformat_minor": 2
66+
}

ch06/Program_6.6_stringfun.ipynb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 6.6: Standing waves on a string (stringfun.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"from scipy.linalg import eigh # Hermitian eigenvalue solver\n",
15+
"import numpy as np, matplotlib.pyplot as plt\n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"N = 20 # number of intervals\n",
19+
"h, s = 1./N, 5 # bin size, skip\n",
20+
"\n",
21+
"A = np.diag([-2.]*(N-1)) # diagonal\n",
22+
"A += np.diag([1.]*(N-2),1) + np.diag([1.]*(N-2),-1) # off diagonals\n",
23+
"\n",
24+
"A = -A/(h*h) # solve $-h^{-2} A X = k^2 X$\n",
25+
"lamb, u = eigh(A) # so eigenvalues lamb = k^2\n",
26+
"\n",
27+
"x, sty = np.linspace(0.,1., s*N + 1), ['o','^','s','v']\n",
28+
"fig, f, pi = plt.figure(), np.sin, np.pi # some aliases\n",
29+
"print (2*pi/np.sqrt(lamb[:len(sty)])) # wavelength \n",
30+
"\n",
31+
"for i in range(1, len(sty)+1):\n",
32+
" ui = np.insert(u[:,i-1], [0, N-1], [0., 0.]) # insert BV\n",
33+
" plt.plot(x, f(i*pi*x)*ui[1]/f(i*pi*x[s])) # normalize soln\n",
34+
" plt.plot(x[::s], ui, sty[i-1], label=repr(i))\n",
35+
" \n",
36+
"plt.xlabel('$x$'), plt.ylabel('$u$')\n",
37+
"plt.legend(loc='lower right'), plt.show()"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {
44+
"collapsed": true
45+
},
46+
"outputs": [],
47+
"source": []
48+
}
49+
],
50+
"metadata": {
51+
"kernelspec": {
52+
"display_name": "Python 3",
53+
"language": "python",
54+
"name": "python3"
55+
},
56+
"language_info": {
57+
"codemirror_mode": {
58+
"name": "ipython",
59+
"version": 3
60+
},
61+
"file_extension": ".py",
62+
"mimetype": "text/x-python",
63+
"name": "python",
64+
"nbconvert_exporter": "python",
65+
"pygments_lexer": "ipython3",
66+
"version": "3.6.1"
67+
}
68+
},
69+
"nbformat": 4,
70+
"nbformat_minor": 2
71+
}

0 commit comments

Comments
 (0)