Show More
@@ -0,0 +1,475 b'' | |||
|
1 | { | |
|
2 | "metadata": { | |
|
3 | "name": "" | |
|
4 | }, | |
|
5 | "nbformat": 3, | |
|
6 | "nbformat_minor": 0, | |
|
7 | "worksheets": [ | |
|
8 | { | |
|
9 | "cells": [ | |
|
10 | { | |
|
11 | "cell_type": "heading", | |
|
12 | "level": 1, | |
|
13 | "metadata": {}, | |
|
14 | "source": [ | |
|
15 | "Interact Demos" | |
|
16 | ] | |
|
17 | }, | |
|
18 | { | |
|
19 | "cell_type": "markdown", | |
|
20 | "metadata": {}, | |
|
21 | "source": [ | |
|
22 | "This Notebook shows basic demonstrations of IPython `interact` module. This provides a high-level interface for creating user interface controls to use in exploring code and data interactively." | |
|
23 | ] | |
|
24 | }, | |
|
25 | { | |
|
26 | "cell_type": "code", | |
|
27 | "collapsed": false, | |
|
28 | "input": [ | |
|
29 | "%pylab inline" | |
|
30 | ], | |
|
31 | "language": "python", | |
|
32 | "metadata": {}, | |
|
33 | "outputs": [] | |
|
34 | }, | |
|
35 | { | |
|
36 | "cell_type": "code", | |
|
37 | "collapsed": false, | |
|
38 | "input": [ | |
|
39 | "from IPython.html.widgets.interact import interact, interactive\n", | |
|
40 | "from IPython.html import widgets\n", | |
|
41 | "from IPython.display import clear_output, display, HTML" | |
|
42 | ], | |
|
43 | "language": "python", | |
|
44 | "metadata": {}, | |
|
45 | "outputs": [] | |
|
46 | }, | |
|
47 | { | |
|
48 | "cell_type": "heading", | |
|
49 | "level": 2, | |
|
50 | "metadata": {}, | |
|
51 | "source": [ | |
|
52 | "Basic interact" | |
|
53 | ] | |
|
54 | }, | |
|
55 | { | |
|
56 | "cell_type": "markdown", | |
|
57 | "metadata": {}, | |
|
58 | "source": [ | |
|
59 | "Here is a simple function that displays its arguments as an HTML table:" | |
|
60 | ] | |
|
61 | }, | |
|
62 | { | |
|
63 | "cell_type": "code", | |
|
64 | "collapsed": false, | |
|
65 | "input": [ | |
|
66 | "def show_args(**kwargs):\n", | |
|
67 | " s = '<h3>Arguments:</h3><table>\\n'\n", | |
|
68 | " for k,v in kwargs.items():\n", | |
|
69 | " s += '<tr><td>{0}</td><td>{1}</td></tr>\\n'.format(k,v)\n", | |
|
70 | " s += '</table>'\n", | |
|
71 | " display(HTML(s))" | |
|
72 | ], | |
|
73 | "language": "python", | |
|
74 | "metadata": {}, | |
|
75 | "outputs": [] | |
|
76 | }, | |
|
77 | { | |
|
78 | "cell_type": "code", | |
|
79 | "collapsed": false, | |
|
80 | "input": [ | |
|
81 | "show_args(a=10, b='Hi There', c=True)" | |
|
82 | ], | |
|
83 | "language": "python", | |
|
84 | "metadata": {}, | |
|
85 | "outputs": [] | |
|
86 | }, | |
|
87 | { | |
|
88 | "cell_type": "markdown", | |
|
89 | "metadata": {}, | |
|
90 | "source": [ | |
|
91 | "Let's use this function to explore how `interact` works." | |
|
92 | ] | |
|
93 | }, | |
|
94 | { | |
|
95 | "cell_type": "code", | |
|
96 | "collapsed": false, | |
|
97 | "input": [ | |
|
98 | "interact(show_args,\n", | |
|
99 | " Temp=(0,10),\n", | |
|
100 | " Current=(0.,10.,0.01),\n", | |
|
101 | " z=(True,False),\n", | |
|
102 | " Text=u'Type here!',\n", | |
|
103 | " Algorithm=['This','That','Other'],\n", | |
|
104 | " a=widgets.FloatRangeWidget(min=-10.0, max=10.0, step=0.1, value=5.0)\n", | |
|
105 | " )" | |
|
106 | ], | |
|
107 | "language": "python", | |
|
108 | "metadata": {}, | |
|
109 | "outputs": [] | |
|
110 | }, | |
|
111 | { | |
|
112 | "cell_type": "markdown", | |
|
113 | "metadata": {}, | |
|
114 | "source": [ | |
|
115 | "The keyword arguments to `interact` can be any `Widget` instance that has a `value` and `description` attribute, or one of the shorthand notations shown above." | |
|
116 | ] | |
|
117 | }, | |
|
118 | { | |
|
119 | "cell_type": "heading", | |
|
120 | "level": 2, | |
|
121 | "metadata": {}, | |
|
122 | "source": [ | |
|
123 | "Factoring polynomials" | |
|
124 | ] | |
|
125 | }, | |
|
126 | { | |
|
127 | "cell_type": "markdown", | |
|
128 | "metadata": {}, | |
|
129 | "source": [ | |
|
130 | "Here is an example that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials." | |
|
131 | ] | |
|
132 | }, | |
|
133 | { | |
|
134 | "cell_type": "code", | |
|
135 | "collapsed": false, | |
|
136 | "input": [ | |
|
137 | "from sympy import Symbol, Eq, factor, init_printing\n", | |
|
138 | "init_printing(use_latex=True)" | |
|
139 | ], | |
|
140 | "language": "python", | |
|
141 | "metadata": {}, | |
|
142 | "outputs": [] | |
|
143 | }, | |
|
144 | { | |
|
145 | "cell_type": "code", | |
|
146 | "collapsed": false, | |
|
147 | "input": [ | |
|
148 | "x = Symbol('x')" | |
|
149 | ], | |
|
150 | "language": "python", | |
|
151 | "metadata": {}, | |
|
152 | "outputs": [] | |
|
153 | }, | |
|
154 | { | |
|
155 | "cell_type": "code", | |
|
156 | "collapsed": false, | |
|
157 | "input": [ | |
|
158 | "def factorit(n):\n", | |
|
159 | " display(Eq(x**n-1, factor(x**n-1)))" | |
|
160 | ], | |
|
161 | "language": "python", | |
|
162 | "metadata": {}, | |
|
163 | "outputs": [] | |
|
164 | }, | |
|
165 | { | |
|
166 | "cell_type": "markdown", | |
|
167 | "metadata": {}, | |
|
168 | "source": [ | |
|
169 | "Notice how the output of the `factorit` function is properly formatted LaTeX." | |
|
170 | ] | |
|
171 | }, | |
|
172 | { | |
|
173 | "cell_type": "code", | |
|
174 | "collapsed": false, | |
|
175 | "input": [ | |
|
176 | "interact(factorit, n=(2,40))" | |
|
177 | ], | |
|
178 | "language": "python", | |
|
179 | "metadata": {}, | |
|
180 | "outputs": [] | |
|
181 | }, | |
|
182 | { | |
|
183 | "cell_type": "heading", | |
|
184 | "level": 2, | |
|
185 | "metadata": {}, | |
|
186 | "source": [ | |
|
187 | "A simple image browser" | |
|
188 | ] | |
|
189 | }, | |
|
190 | { | |
|
191 | "cell_type": "markdown", | |
|
192 | "metadata": {}, | |
|
193 | "source": [ | |
|
194 | "This example shows how to browse through a set of images with a slider." | |
|
195 | ] | |
|
196 | }, | |
|
197 | { | |
|
198 | "cell_type": "code", | |
|
199 | "collapsed": false, | |
|
200 | "input": [ | |
|
201 | "from sklearn import datasets" | |
|
202 | ], | |
|
203 | "language": "python", | |
|
204 | "metadata": {}, | |
|
205 | "outputs": [] | |
|
206 | }, | |
|
207 | { | |
|
208 | "cell_type": "markdown", | |
|
209 | "metadata": {}, | |
|
210 | "source": [ | |
|
211 | "We will use the digits dataset from [scikit-learn](http://scikit-learn.org/stable/)." | |
|
212 | ] | |
|
213 | }, | |
|
214 | { | |
|
215 | "cell_type": "code", | |
|
216 | "collapsed": false, | |
|
217 | "input": [ | |
|
218 | "digits = datasets.load_digits()" | |
|
219 | ], | |
|
220 | "language": "python", | |
|
221 | "metadata": {}, | |
|
222 | "outputs": [] | |
|
223 | }, | |
|
224 | { | |
|
225 | "cell_type": "code", | |
|
226 | "collapsed": false, | |
|
227 | "input": [ | |
|
228 | "def browse_images(digits):\n", | |
|
229 | " n = len(digits.images)\n", | |
|
230 | " def view_image(i):\n", | |
|
231 | " imshow(digits.images[i], cmap=cm.gray_r, interpolation='nearest')\n", | |
|
232 | " title('Training: %s' % digits.target[i])\n", | |
|
233 | " show()\n", | |
|
234 | " interact(view_image, i=(0,n-1))" | |
|
235 | ], | |
|
236 | "language": "python", | |
|
237 | "metadata": {}, | |
|
238 | "outputs": [] | |
|
239 | }, | |
|
240 | { | |
|
241 | "cell_type": "code", | |
|
242 | "collapsed": false, | |
|
243 | "input": [ | |
|
244 | "browse_images(digits)" | |
|
245 | ], | |
|
246 | "language": "python", | |
|
247 | "metadata": {}, | |
|
248 | "outputs": [] | |
|
249 | }, | |
|
250 | { | |
|
251 | "cell_type": "heading", | |
|
252 | "level": 2, | |
|
253 | "metadata": {}, | |
|
254 | "source": [ | |
|
255 | "Explore random graphs" | |
|
256 | ] | |
|
257 | }, | |
|
258 | { | |
|
259 | "cell_type": "markdown", | |
|
260 | "metadata": {}, | |
|
261 | "source": [ | |
|
262 | "In this example, we build a simple UI for exploring random graphs with [NetworkX](http://networkx.github.io/)." | |
|
263 | ] | |
|
264 | }, | |
|
265 | { | |
|
266 | "cell_type": "code", | |
|
267 | "collapsed": false, | |
|
268 | "input": [ | |
|
269 | "import networkx as nx" | |
|
270 | ], | |
|
271 | "language": "python", | |
|
272 | "metadata": {}, | |
|
273 | "outputs": [] | |
|
274 | }, | |
|
275 | { | |
|
276 | "cell_type": "code", | |
|
277 | "collapsed": false, | |
|
278 | "input": [ | |
|
279 | "def plot_random_graph(n, p, generator):\n", | |
|
280 | " g = generator(n,p)\n", | |
|
281 | " nx.draw(g)\n", | |
|
282 | " show()" | |
|
283 | ], | |
|
284 | "language": "python", | |
|
285 | "metadata": {}, | |
|
286 | "outputs": [] | |
|
287 | }, | |
|
288 | { | |
|
289 | "cell_type": "code", | |
|
290 | "collapsed": false, | |
|
291 | "input": [ | |
|
292 | "interact(plot_random_graph, n=(2,30), p=(0.0, 1.0, 0.001),\n", | |
|
293 | " generator={'gnp': nx.gnp_random_graph,\n", | |
|
294 | " 'erdos_renyi': nx.erdos_renyi_graph,\n", | |
|
295 | " 'binomial': nx.binomial_graph})" | |
|
296 | ], | |
|
297 | "language": "python", | |
|
298 | "metadata": {}, | |
|
299 | "outputs": [] | |
|
300 | }, | |
|
301 | { | |
|
302 | "cell_type": "heading", | |
|
303 | "level": 2, | |
|
304 | "metadata": {}, | |
|
305 | "source": [ | |
|
306 | "Image manipulation" | |
|
307 | ] | |
|
308 | }, | |
|
309 | { | |
|
310 | "cell_type": "markdown", | |
|
311 | "metadata": {}, | |
|
312 | "source": [ | |
|
313 | "This example builds a simple UI for performing basic image manipulation with [scikit-image](http://scikit-image.org/)." | |
|
314 | ] | |
|
315 | }, | |
|
316 | { | |
|
317 | "cell_type": "code", | |
|
318 | "collapsed": false, | |
|
319 | "input": [ | |
|
320 | "import skimage\n", | |
|
321 | "from skimage import data, filter, io" | |
|
322 | ], | |
|
323 | "language": "python", | |
|
324 | "metadata": {}, | |
|
325 | "outputs": [] | |
|
326 | }, | |
|
327 | { | |
|
328 | "cell_type": "code", | |
|
329 | "collapsed": false, | |
|
330 | "input": [ | |
|
331 | "i = data.coffee()" | |
|
332 | ], | |
|
333 | "language": "python", | |
|
334 | "metadata": {}, | |
|
335 | "outputs": [] | |
|
336 | }, | |
|
337 | { | |
|
338 | "cell_type": "code", | |
|
339 | "collapsed": false, | |
|
340 | "input": [ | |
|
341 | "io.Image(i)" | |
|
342 | ], | |
|
343 | "language": "python", | |
|
344 | "metadata": {}, | |
|
345 | "outputs": [] | |
|
346 | }, | |
|
347 | { | |
|
348 | "cell_type": "code", | |
|
349 | "collapsed": false, | |
|
350 | "input": [ | |
|
351 | "def edit_image(image):\n", | |
|
352 | " def apply_filter(sigma, r, g, b):\n", | |
|
353 | " new_image = filter.gaussian_filter(image, sigma=sigma)\n", | |
|
354 | " new_image[:,:,0] = r*new_image[:,:,0]\n", | |
|
355 | " new_image[:,:,1] = g*new_image[:,:,1]\n", | |
|
356 | " new_image[:,:,2] = b*new_image[:,:,2]\n", | |
|
357 | " new_image = io.Image(new_image)\n", | |
|
358 | " display(new_image)\n", | |
|
359 | " return new_image\n", | |
|
360 | " lims = (0.0,1.0,0.01)\n", | |
|
361 | " return interactive(apply_filter, sigma=(0.1,10.0,0.01), r=lims, g=lims, b=lims)" | |
|
362 | ], | |
|
363 | "language": "python", | |
|
364 | "metadata": {}, | |
|
365 | "outputs": [] | |
|
366 | }, | |
|
367 | { | |
|
368 | "cell_type": "code", | |
|
369 | "collapsed": false, | |
|
370 | "input": [ | |
|
371 | "w = edit_image(i)" | |
|
372 | ], | |
|
373 | "language": "python", | |
|
374 | "metadata": {}, | |
|
375 | "outputs": [] | |
|
376 | }, | |
|
377 | { | |
|
378 | "cell_type": "code", | |
|
379 | "collapsed": false, | |
|
380 | "input": [ | |
|
381 | "display(w)" | |
|
382 | ], | |
|
383 | "language": "python", | |
|
384 | "metadata": {}, | |
|
385 | "outputs": [] | |
|
386 | }, | |
|
387 | { | |
|
388 | "cell_type": "code", | |
|
389 | "collapsed": false, | |
|
390 | "input": [ | |
|
391 | "w.arguments" | |
|
392 | ], | |
|
393 | "language": "python", | |
|
394 | "metadata": {}, | |
|
395 | "outputs": [] | |
|
396 | }, | |
|
397 | { | |
|
398 | "cell_type": "code", | |
|
399 | "collapsed": false, | |
|
400 | "input": [ | |
|
401 | "w.result" | |
|
402 | ], | |
|
403 | "language": "python", | |
|
404 | "metadata": {}, | |
|
405 | "outputs": [] | |
|
406 | }, | |
|
407 | { | |
|
408 | "cell_type": "heading", | |
|
409 | "level": 2, | |
|
410 | "metadata": {}, | |
|
411 | "source": [ | |
|
412 | "Playing with audio" | |
|
413 | ] | |
|
414 | }, | |
|
415 | { | |
|
416 | "cell_type": "markdown", | |
|
417 | "metadata": {}, | |
|
418 | "source": [ | |
|
419 | "This example uses the `Audio` object and Matplotlib to explore the phenomenon of beat frequencies." | |
|
420 | ] | |
|
421 | }, | |
|
422 | { | |
|
423 | "cell_type": "code", | |
|
424 | "collapsed": false, | |
|
425 | "input": [ | |
|
426 | "from IPython.display import Audio\n", | |
|
427 | "import numpy as np" | |
|
428 | ], | |
|
429 | "language": "python", | |
|
430 | "metadata": {}, | |
|
431 | "outputs": [] | |
|
432 | }, | |
|
433 | { | |
|
434 | "cell_type": "code", | |
|
435 | "collapsed": false, | |
|
436 | "input": [ | |
|
437 | "def beat_freq(f1=220.0, f2=224.0):\n", | |
|
438 | " max_time = 3\n", | |
|
439 | " rate = 8000.0\n", | |
|
440 | " times = np.linspace(0,max_time,rate*max_time)\n", | |
|
441 | " signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)\n", | |
|
442 | " print f1, f2, abs(f1-f2)\n", | |
|
443 | " display(Audio(data=signal, rate=rate))\n", | |
|
444 | " return signal" | |
|
445 | ], | |
|
446 | "language": "python", | |
|
447 | "metadata": {}, | |
|
448 | "outputs": [] | |
|
449 | }, | |
|
450 | { | |
|
451 | "cell_type": "code", | |
|
452 | "collapsed": false, | |
|
453 | "input": [ | |
|
454 | "v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))\n", | |
|
455 | "display(v)" | |
|
456 | ], | |
|
457 | "language": "python", | |
|
458 | "metadata": {}, | |
|
459 | "outputs": [] | |
|
460 | }, | |
|
461 | { | |
|
462 | "cell_type": "code", | |
|
463 | "collapsed": false, | |
|
464 | "input": [ | |
|
465 | "plot(v.result[0:6000])" | |
|
466 | ], | |
|
467 | "language": "python", | |
|
468 | "metadata": {}, | |
|
469 | "outputs": [] | |
|
470 | } | |
|
471 | ], | |
|
472 | "metadata": {} | |
|
473 | } | |
|
474 | ] | |
|
475 | } No newline at end of file |
@@ -0,0 +1,269 b'' | |||
|
1 | { | |
|
2 | "metadata": { | |
|
3 | "name": "" | |
|
4 | }, | |
|
5 | "nbformat": 3, | |
|
6 | "nbformat_minor": 0, | |
|
7 | "worksheets": [ | |
|
8 | { | |
|
9 | "cells": [ | |
|
10 | { | |
|
11 | "cell_type": "heading", | |
|
12 | "level": 1, | |
|
13 | "metadata": {}, | |
|
14 | "source": [ | |
|
15 | "Exploring the Lorenz System of Differential Equations" | |
|
16 | ] | |
|
17 | }, | |
|
18 | { | |
|
19 | "cell_type": "markdown", | |
|
20 | "metadata": {}, | |
|
21 | "source": [ | |
|
22 | "In this Notebook we explore the Lorenz system of differential equations:\n", | |
|
23 | "\n", | |
|
24 | "$$\n", | |
|
25 | "\\begin{aligned}\n", | |
|
26 | "\\dot{x} & = \\sigma(y-x) \\\\\n", | |
|
27 | "\\dot{y} & = \\rho x - y - xz \\\\\n", | |
|
28 | "\\dot{z} & = -\\beta z + xy\n", | |
|
29 | "\\end{aligned}\n", | |
|
30 | "$$\n", | |
|
31 | "\n", | |
|
32 | "This is one of the classic systems in non-linear differential equations. It exhibits a range of different behaviors as the parameters ($\\sigma$, $\\beta$, $\\rho$) are varied." | |
|
33 | ] | |
|
34 | }, | |
|
35 | { | |
|
36 | "cell_type": "heading", | |
|
37 | "level": 2, | |
|
38 | "metadata": {}, | |
|
39 | "source": [ | |
|
40 | "Imports" | |
|
41 | ] | |
|
42 | }, | |
|
43 | { | |
|
44 | "cell_type": "markdown", | |
|
45 | "metadata": {}, | |
|
46 | "source": [ | |
|
47 | "First, we import the needed things from IPython, NumPy, Matplotlib and SciPy." | |
|
48 | ] | |
|
49 | }, | |
|
50 | { | |
|
51 | "cell_type": "code", | |
|
52 | "collapsed": false, | |
|
53 | "input": [ | |
|
54 | "%pylab inline" | |
|
55 | ], | |
|
56 | "language": "python", | |
|
57 | "metadata": {}, | |
|
58 | "outputs": [] | |
|
59 | }, | |
|
60 | { | |
|
61 | "cell_type": "code", | |
|
62 | "collapsed": false, | |
|
63 | "input": [ | |
|
64 | "from IPython.html.widgets.interact import interact, interactive\n", | |
|
65 | "from IPython.display import clear_output, display, HTML" | |
|
66 | ], | |
|
67 | "language": "python", | |
|
68 | "metadata": {}, | |
|
69 | "outputs": [] | |
|
70 | }, | |
|
71 | { | |
|
72 | "cell_type": "code", | |
|
73 | "collapsed": false, | |
|
74 | "input": [ | |
|
75 | "import numpy as np\n", | |
|
76 | "from scipy import integrate\n", | |
|
77 | "\n", | |
|
78 | "from matplotlib import pyplot as plt\n", | |
|
79 | "from mpl_toolkits.mplot3d import Axes3D\n", | |
|
80 | "from matplotlib.colors import cnames\n", | |
|
81 | "from matplotlib import animation" | |
|
82 | ], | |
|
83 | "language": "python", | |
|
84 | "metadata": {}, | |
|
85 | "outputs": [] | |
|
86 | }, | |
|
87 | { | |
|
88 | "cell_type": "heading", | |
|
89 | "level": 2, | |
|
90 | "metadata": {}, | |
|
91 | "source": [ | |
|
92 | "Computing the trajectories and plotting the result" | |
|
93 | ] | |
|
94 | }, | |
|
95 | { | |
|
96 | "cell_type": "markdown", | |
|
97 | "metadata": {}, | |
|
98 | "source": [ | |
|
99 | "We define a function that can integrate the differential equations numerically and then plot the solutions. This function has arguments that control the parameters of the differential equation ($\\sigma$, $\\beta$, $\\rho$), the numerical integration (`N`, `max_time`) and the visualization (`angle`)." | |
|
100 | ] | |
|
101 | }, | |
|
102 | { | |
|
103 | "cell_type": "code", | |
|
104 | "collapsed": false, | |
|
105 | "input": [ | |
|
106 | "def solve_lorenz(N=10, angle=0.0, max_time=4.0, sigma=10.0, beta=8./3, rho=28.0):\n", | |
|
107 | "\n", | |
|
108 | " fig = plt.figure()\n", | |
|
109 | " ax = fig.add_axes([0, 0, 1, 1], projection='3d')\n", | |
|
110 | " ax.axis('off')\n", | |
|
111 | "\n", | |
|
112 | " # prepare the axes limits\n", | |
|
113 | " ax.set_xlim((-25, 25))\n", | |
|
114 | " ax.set_ylim((-35, 35))\n", | |
|
115 | " ax.set_zlim((5, 55))\n", | |
|
116 | " \n", | |
|
117 | " def lorenz_deriv((x, y, z), t0, sigma=sigma, beta=beta, rho=rho):\n", | |
|
118 | " \"\"\"Compute the time-derivative of a Lorentz system.\"\"\"\n", | |
|
119 | " return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]\n", | |
|
120 | "\n", | |
|
121 | " # Choose random starting points, uniformly distributed from -15 to 15\n", | |
|
122 | " np.random.seed(1)\n", | |
|
123 | " x0 = -15 + 30 * np.random.random((N, 3))\n", | |
|
124 | "\n", | |
|
125 | " # Solve for the trajectories\n", | |
|
126 | " t = np.linspace(0, max_time, int(250*max_time))\n", | |
|
127 | " x_t = np.asarray([integrate.odeint(lorenz_deriv, x0i, t)\n", | |
|
128 | " for x0i in x0])\n", | |
|
129 | " \n", | |
|
130 | " # choose a different color for each trajectory\n", | |
|
131 | " colors = plt.cm.jet(np.linspace(0, 1, N))\n", | |
|
132 | "\n", | |
|
133 | " for i in range(N):\n", | |
|
134 | " x, y, z = x_t[i,:,:].T\n", | |
|
135 | " lines = ax.plot(x, y, z, '-', c=colors[i])\n", | |
|
136 | " setp(lines, linewidth=2)\n", | |
|
137 | "\n", | |
|
138 | " ax.view_init(30, angle)\n", | |
|
139 | " show()\n", | |
|
140 | "\n", | |
|
141 | " return t, x_t" | |
|
142 | ], | |
|
143 | "language": "python", | |
|
144 | "metadata": {}, | |
|
145 | "outputs": [] | |
|
146 | }, | |
|
147 | { | |
|
148 | "cell_type": "markdown", | |
|
149 | "metadata": {}, | |
|
150 | "source": [ | |
|
151 | "Let's call the function once to view the solutions. For this set of parameters, we see the trajectories swirling around two points, called attractors. " | |
|
152 | ] | |
|
153 | }, | |
|
154 | { | |
|
155 | "cell_type": "code", | |
|
156 | "collapsed": false, | |
|
157 | "input": [ | |
|
158 | "t, x_t = solve_lorenz(angle=0, N=10)" | |
|
159 | ], | |
|
160 | "language": "python", | |
|
161 | "metadata": {}, | |
|
162 | "outputs": [] | |
|
163 | }, | |
|
164 | { | |
|
165 | "cell_type": "markdown", | |
|
166 | "metadata": {}, | |
|
167 | "source": [ | |
|
168 | "Using IPython's `interactive` function, we can explore how the trajectories behave as we change the various parameters." | |
|
169 | ] | |
|
170 | }, | |
|
171 | { | |
|
172 | "cell_type": "code", | |
|
173 | "collapsed": false, | |
|
174 | "input": [ | |
|
175 | "w = interactive(solve_lorenz, angle=(0.,360.), N=(0,50), sigma=(0.0,50.0), rho=(0.0,50.0))\n", | |
|
176 | "display(w)" | |
|
177 | ], | |
|
178 | "language": "python", | |
|
179 | "metadata": {}, | |
|
180 | "outputs": [] | |
|
181 | }, | |
|
182 | { | |
|
183 | "cell_type": "markdown", | |
|
184 | "metadata": {}, | |
|
185 | "source": [ | |
|
186 | "The object returned by `interactive` is a `Widget` object and it has attributes that contain the current result and arguments:" | |
|
187 | ] | |
|
188 | }, | |
|
189 | { | |
|
190 | "cell_type": "code", | |
|
191 | "collapsed": false, | |
|
192 | "input": [ | |
|
193 | "t, x_t = w.result" | |
|
194 | ], | |
|
195 | "language": "python", | |
|
196 | "metadata": {}, | |
|
197 | "outputs": [] | |
|
198 | }, | |
|
199 | { | |
|
200 | "cell_type": "code", | |
|
201 | "collapsed": false, | |
|
202 | "input": [ | |
|
203 | "w.arguments" | |
|
204 | ], | |
|
205 | "language": "python", | |
|
206 | "metadata": {}, | |
|
207 | "outputs": [] | |
|
208 | }, | |
|
209 | { | |
|
210 | "cell_type": "markdown", | |
|
211 | "metadata": {}, | |
|
212 | "source": [ | |
|
213 | "After interacting with the system, we can take the result and perform further computations. In this case, we compute the average positions in $x$, $y$ and $z$." | |
|
214 | ] | |
|
215 | }, | |
|
216 | { | |
|
217 | "cell_type": "code", | |
|
218 | "collapsed": false, | |
|
219 | "input": [ | |
|
220 | "xyz_avg = x_t.mean(axis=1)" | |
|
221 | ], | |
|
222 | "language": "python", | |
|
223 | "metadata": {}, | |
|
224 | "outputs": [] | |
|
225 | }, | |
|
226 | { | |
|
227 | "cell_type": "code", | |
|
228 | "collapsed": false, | |
|
229 | "input": [ | |
|
230 | "xyz_avg.shape" | |
|
231 | ], | |
|
232 | "language": "python", | |
|
233 | "metadata": {}, | |
|
234 | "outputs": [] | |
|
235 | }, | |
|
236 | { | |
|
237 | "cell_type": "markdown", | |
|
238 | "metadata": {}, | |
|
239 | "source": [ | |
|
240 | "Creating histograms of the average positions (across different trajectories) show that on average the trajectories swirl about the attractors." | |
|
241 | ] | |
|
242 | }, | |
|
243 | { | |
|
244 | "cell_type": "code", | |
|
245 | "collapsed": false, | |
|
246 | "input": [ | |
|
247 | "hist(xyz_avg[:,0])\n", | |
|
248 | "title('Average $x(t)$')" | |
|
249 | ], | |
|
250 | "language": "python", | |
|
251 | "metadata": {}, | |
|
252 | "outputs": [] | |
|
253 | }, | |
|
254 | { | |
|
255 | "cell_type": "code", | |
|
256 | "collapsed": false, | |
|
257 | "input": [ | |
|
258 | "hist(xyz_avg[:,1])\n", | |
|
259 | "title('Average $y(t)$')" | |
|
260 | ], | |
|
261 | "language": "python", | |
|
262 | "metadata": {}, | |
|
263 | "outputs": [] | |
|
264 | } | |
|
265 | ], | |
|
266 | "metadata": {} | |
|
267 | } | |
|
268 | ] | |
|
269 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now