Skip to content

Commit 708b634

Browse files
authored
Jupyter notebook version added
1 parent 3ab2221 commit 708b634

7 files changed

+501
-0
lines changed

ch05/Program_5.1_logisticmap.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 5.1: Logistic map (logisticmap.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"from __future__ import print_function # use print() as function\n",
15+
"import matplotlib.pyplot as plt # get matplotlib plot functions\n",
16+
"import sys\n",
17+
"%matplotlib notebook\n",
18+
"\n",
19+
"if (sys.version_info[0] < 3):\n",
20+
" x, r = input('enter x0, r: ')\n",
21+
"else:\n",
22+
" x, r = eval(input('enter x0, r: '))\n",
23+
"n, xn = 40, []\n",
24+
"for i in range(n):\n",
25+
" xn.append(x) # new line every 4 steps\n",
26+
" print('%8f' %x, end='\\n' if len(xn)%4==0 else ' ') # inline if \n",
27+
" x = 4*r*x*(1.0-x) # next iteration, logistic map\n",
28+
" \n",
29+
"plt.figure()\n",
30+
"plt.plot(range(n), xn, '--o') # plot dashed line with 'o' symbol\n",
31+
"plt.xlabel('$n$'), plt.ylabel('$x_n$')\n",
32+
"plt.ylim(0,1), plt.text(33, .1, 'r='+ repr(r)) # add text \n",
33+
"plt.show()"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"collapsed": true
41+
},
42+
"outputs": [],
43+
"source": []
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 3",
49+
"language": "python",
50+
"name": "python3"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 3
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython3",
62+
"version": "3.6.1"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 2
67+
}

ch05/Program_5.2_perioddbl.ipynb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 5.2: Period doubling (perioddbl.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import matplotlib.pyplot as plt # get matplotlib plot functions\n",
15+
"%matplotlib notebook\n",
16+
"\n",
17+
"ntrans, nsteps = 1000, 200 # num. of transients and to keep\n",
18+
"r, rend, dr, x, xa = 0.7, 1.0, 0.001, 0.5, [0.0]*nsteps\n",
19+
"plt.figure()\n",
20+
"while r <= rend:\n",
21+
" for i in range(ntrans): x = 4*r*x*(1-x) # discard transients\n",
22+
" for i in range(nsteps): xa[i], x = x, 4*r*x*(1-x) # keep rest\n",
23+
" plt.plot([r]*nsteps, xa, 'b,') # blue pixel markers\n",
24+
" r = r + dr\n",
25+
"plt.xlabel('r'), plt.ylabel('x'), plt.show()"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": true
33+
},
34+
"outputs": [],
35+
"source": []
36+
}
37+
],
38+
"metadata": {
39+
"kernelspec": {
40+
"display_name": "Python 3",
41+
"language": "python",
42+
"name": "python3"
43+
},
44+
"language_info": {
45+
"codemirror_mode": {
46+
"name": "ipython",
47+
"version": 3
48+
},
49+
"file_extension": ".py",
50+
"mimetype": "text/x-python",
51+
"name": "python",
52+
"nbconvert_exporter": "python",
53+
"pygments_lexer": "ipython3",
54+
"version": "3.6.1"
55+
}
56+
},
57+
"nbformat": 4,
58+
"nbformat_minor": 2
59+
}

ch05/Program_5.3_logisdiff.ipynb

Lines changed: 71 additions & 0 deletions
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 5.3: Initial value dependence of logistic map (logisdiff.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import matplotlib.pyplot as plt # get matplotlib plot functions\n",
15+
"import sys\n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"if (sys.version_info[0] < 3):\n",
19+
" x1, r = input('enter x1, r; eg .4, .7 : ')\n",
20+
"else:\n",
21+
" x1, r = eval(input('enter x1, r; eg .4, .7 : '))\n",
22+
"x2 = x1 + 0.01 # initial difference\n",
23+
"n, xn1, xn2, diff = 20, [], [], [] # buffers\n",
24+
"for i in range(n):\n",
25+
" xn1.append(x1), xn2.append(x2), diff.append(abs(x1-x2))\n",
26+
" x1, x2 = 4*r*x1*(1.0-x1), 4*r*x2*(1.0-x2) # parallel iterates\n",
27+
" \n",
28+
"plt.figure() # plot the series\n",
29+
"plt.plot(range(n), xn1, 's-', range(n), xn2,'o-') # squares & circles\n",
30+
"plt.xlabel('$n$'), plt.ylabel('$x_n$')\n",
31+
"plt.text(3, .01+min(xn1),'$r=$' + repr(r))\n",
32+
"\n",
33+
"plt.figure() # plot the difference\n",
34+
"plt.plot(range(n), diff)\n",
35+
"plt.semilogy() # semilog scale\n",
36+
"plt.xlabel('$n$'), plt.ylabel('$\\Delta x$')\n",
37+
"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+
}

ch05/Program_5.4_lyapunov.ipynb

Lines changed: 71 additions & 0 deletions
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 5.4: Lyapunov exponent of logistic map (lyapunov.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import matplotlib.pyplot as plt # get matplotlib plot functions\n",
15+
"import math as ma # get math functions \n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"def lyapunov(r): # compute Lyapunov exponent\n",
19+
" sum, x, ntrans, nsteps = 0.0, 0.5, 2000, 2000 # try diff num.\n",
20+
" for i in range(ntrans): # let transients pass\n",
21+
" x = 4*r*x*(1-x) \n",
22+
" for i in range(nsteps): # sum up next n steps\n",
23+
" x = 4*r*x*(1-x) \n",
24+
" dfdx = 4.0*r*(1.0-x-x) # \n",
25+
" sum += ma.log(abs(dfdx))\n",
26+
" return sum/float(nsteps) # lambda \n",
27+
" \n",
28+
"ra, lyap, r, dr = [], [], 0.7, 0.0001\n",
29+
"while r < 1.0: \n",
30+
" r = r + dr\n",
31+
" ra.append(r), lyap.append(lyapunov(r))\n",
32+
" \n",
33+
"plt.figure()\n",
34+
"plt.plot(ra, lyap, ',') # ','=pixels\n",
35+
"plt.axhline(0.), plt.ylim(-2, 1) # draw horiz. line, set y limits\n",
36+
"plt.xlabel('$r$'), plt.ylabel('$\\lambda$')\n",
37+
"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+
}

ch05/Program_5.5_nonlindro.ipynb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"#\n",
10+
"# Program 5.5: Nonlindro: Nonlinear driven oscillator (nonlindro.ipynb)\n",
11+
"# J Wang, Computational modeling and visualization with Python\n",
12+
"#\n",
13+
"\n",
14+
"import matplotlib.pyplot as plt # get matplotlib plot functions\n",
15+
"import ode, math as ma # get ODE, math functions \n",
16+
"%matplotlib notebook\n",
17+
"\n",
18+
"def pendulum(y, t): # y = [theta, omega], omega_0 = 1\n",
19+
" return [y[1], -ma.sin(y[0]) - b*y[1] + fd*ma.cos(omega_d*t)]\n",
20+
"\n",
21+
"def solution(n_periods): # find solutions for n_periods\n",
22+
" bins = 40 # number of points per period \n",
23+
" t, y, h = 0.0, [1.0, 0.0], 2*pi/(omega_d*bins) # init values \n",
24+
" ta, theta, omega = [], [], []\n",
25+
" for i in range(n_periods*bins):\n",
26+
" ta.append(t), theta.append(y[0]), omega.append(y[1])\n",
27+
" t, y = t+h, ode.RK4n(pendulum, y, t, h)\n",
28+
" return ta, theta, omega\n",
29+
"\n",
30+
"b, omega_d = 0.5, 0.6 # damping coeff., driving frequency\n",
31+
"subnum, pi = 1, ma.pi # subplot number, pi\n",
32+
"plt.figure()\n",
33+
"for fd in [0.7, 1.1]: \n",
34+
" ax1 = plt.subplot(2, 2, subnum) # 2x2 subplots \n",
35+
" ax2 = plt.subplot(2, 2, subnum+2)\n",
36+
" ta, theta, omega = solution(n_periods = 5)\n",
37+
" ax1.plot(ta, theta), ax2.plot(ta, omega)\n",
38+
" if (subnum == 1): # subplot specific label\n",
39+
" ax1.set_ylabel('$\\\\theta$ (rad)')\n",
40+
" ax2.set_ylabel('$\\\\omega$ (rad/s)')\n",
41+
" subnum, fdtxt = subnum + 1, '$F_d=$'+repr(fd)\n",
42+
" ax1.text(17, max(theta), fdtxt), ax2.text(17, max(omega), fdtxt)\n",
43+
" plt.xlabel('t (s)')\n",
44+
"plt.show()"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {
51+
"collapsed": true
52+
},
53+
"outputs": [],
54+
"source": []
55+
}
56+
],
57+
"metadata": {
58+
"kernelspec": {
59+
"display_name": "Python 3",
60+
"language": "python",
61+
"name": "python3"
62+
},
63+
"language_info": {
64+
"codemirror_mode": {
65+
"name": "ipython",
66+
"version": 3
67+
},
68+
"file_extension": ".py",
69+
"mimetype": "text/x-python",
70+
"name": "python",
71+
"nbconvert_exporter": "python",
72+
"pygments_lexer": "ipython3",
73+
"version": "3.6.1"
74+
}
75+
},
76+
"nbformat": 4,
77+
"nbformat_minor": 2
78+
}

0 commit comments

Comments
 (0)