{"id":1786,"date":"2023-01-17T18:15:21","date_gmt":"2023-01-18T02:15:21","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1786"},"modified":"2023-01-17T18:15:21","modified_gmt":"2023-01-18T02:15:21","slug":"340-lorenz-attractors-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/","title":{"rendered":"#340 Lorenz attractors using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/ex340_a\/\" rel=\"attachment wp-att-1787\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_a.png?resize=1000%2C500&#038;ssl=1\" alt=\"\" width=\"1000\" height=\"500\" class=\"alignnone size-full wp-image-1787\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_a.png 1000w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_a-980x490.png 980w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_a-480x240.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1000px, 100vw\" \/><\/a><\/p>\n<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/ex340_b\/\" rel=\"attachment wp-att-1788\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=1000%2C500&#038;ssl=1\" alt=\"\" width=\"1000\" height=\"500\" class=\"alignnone size-full wp-image-1788\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png 1000w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b-980x490.png 980w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b-480x240.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1000px, 100vw\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nfrom numpy import linspace\r\nfrom scipy.integrate import odeint\r\nos.chdir(r'D:\\projects\\wordpress\\ex340')\r\nos.getcwd()\r\nsigma=10.0\r\nb=8\/3.0\r\nr=28.0\r\nf = lambda x,t: &#x5B;sigma*(x&#x5B;1]-x&#x5B;0]), r*x&#x5B;0]-x&#x5B;1]-x&#x5B;0]*x&#x5B;2], x&#x5B;0]*x&#x5B;1]-b*x&#x5B;2]]\r\nt=linspace(0,20,2000)\r\ny0=&#x5B;5.0,5.0,5.0]\r\nsolution=odeint(f,y0,t)\r\nX=solution&#x5B;:,0]; Y=solution&#x5B;:,1]; Z=solution&#x5B;:,2]\r\nplt.subplot(projection='3d')\r\nplt.plot(X,Y,Z)\r\nplt.xlabel('x')\r\nplt.ylabel('y')\r\nplt.savefig(&quot;ex340_a.png&quot;, dpi=100)\r\nplt.show()\r\nplt.rcParams&#x5B;'figure.figsize'] = (10.0, 5.0)\r\nplt.subplot(121) \r\nplt.plot(t,Z)\r\nplt.xlabel('t')\r\nplt.ylabel('Z')\r\nplt.subplot(122)\r\nplt.plot(Y,Z)\r\nplt.xlabel('Y')\r\nplt.ylabel('Z')\r\nplt.savefig(&quot;ex340_b.png&quot;, dpi=100)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt from numpy import linspace from scipy.integrate import odeint os.chdir(r&#8217;D:\\projects\\wordpress\\ex340&#8242;) os.getcwd() sigma=10.0 b=8\/3.0 r=28.0 f = lambda x,t: &#x5B;sigma*(x&#x5B;1]-x&#x5B;0]), r*x&#x5B;0]-x&#x5B;1]-x&#x5B;0]*x&#x5B;2], x&#x5B;0]*x&#x5B;1]-b*x&#x5B;2]] t=linspace(0,20,2000) y0=&#x5B;5.0,5.0,5.0] solution=odeint(f,y0,t) X=solution&#x5B;:,0]; Y=solution&#x5B;:,1]; Z=solution&#x5B;:,2] plt.subplot(projection=&#8217;3d&#8217;) plt.plot(X,Y,Z) plt.xlabel(&#8216;x&#8217;) plt.ylabel(&#8216;y&#8217;) plt.savefig(&quot;ex340_a.png&quot;, dpi=100) plt.show() plt.rcParams&#x5B;&#8217;figure.figsize&#8217;] = (10.0, 5.0) plt.subplot(121) plt.plot(t,Z) plt.xlabel(&#8216;t&#8217;) plt.ylabel(&#8216;Z&#8217;) plt.subplot(122) plt.plot(Y,Z) plt.xlabel(&#8216;Y&#8217;) plt.ylabel(&#8216;Z&#8217;) plt.savefig(&quot;ex340_b.png&quot;, dpi=100) plt.show() plt.close()<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[76,3],"class_list":["post-1786","post","type-post","status-publish","format-standard","hentry","category-python","tag-lorenz-attractor","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-sO","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":169,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/lorenz-equations\/","url_meta":{"origin":1786,"position":0},"title":"Lorenz equations","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate from mpl_toolkits.mplot3d.axes3d import Axes3D os.chdir(r'D:\\projects\\wordpress\\ex36') os.getcwd() def f(xyz, t, rho, sigma, beta): x, y, z = xyz return [sigma*(y-x),x*(rho-z)-y,x*y-beta*z] rho = 28 sigma = 8 beta = 8\/3.0 t = np.linspace(0, 25, 10000) xyz0 = [1.0,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":1746,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1786,"position":1},"title":"#331 Interpolation with Newton&#8217;s polynomial using python","author":"gantovnik","date":"2023-01-04","format":false,"excerpt":"interpolation.py [code language=\"python\"] def interpolation(c,x,x0): # Evaluate Newton's polynomial at x0. # Degree of polynomial n = len(x) - 1 y0 = c[n] for k in range(1,n+1): y0 = c[n-k] + (x0 - x[n-k]) * y0 return y0 def coef(x,y): # Computes the coefficients of Newton's polynomial. # Number of\u2026","rel":"","context":"In &quot;interpolation&quot;","block_context":{"text":"interpolation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/interpolation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex331.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":1786,"position":2},"title":"#425 An animation of a bouncing ball using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as animation # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball's trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2208,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/06\/430-damped-harmonic-oscillator-using-python\/","url_meta":{"origin":1786,"position":3},"title":"#430 Damped harmonic oscillator using python","author":"gantovnik","date":"2024-06-03","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from scipy.integrate import odeint def dho(y, t, beta, omega): x, v = y dydt = [v, -2*beta*v - omega*x] return dydt y0 = [1,0] t = np.linspace(0,10,1000) sol0 = odeint(dho, y0, t, args = (0,1)) beta=0.2 sol0_2 = odeint(dho, y0,\u2026","rel":"","context":"In &quot;differential equations&quot;","block_context":{"text":"differential equations","link":"https:\/\/gantovnik.com\/bio-tips\/category\/differential-equations\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/06\/ex430.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/06\/ex430.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/06\/ex430.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":1786,"position":4},"title":"#193 Animation using python","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"[code language=\"python\"] # create an animation import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as manimation n = 1000 x = np.linspace(0, 6*np.pi, n) y = np.sin(x) # Define the meta data for the movie FFMpegWriter = manimation.writers[\"ffmpeg\"] metadata = dict(title=\"Movie Test\", artist=\"Matplotlib\", comment=\"a red circle following\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex193.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":157,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/numerical-integration-of-odes-using-scipy\/","url_meta":{"origin":1786,"position":5},"title":"Numerical integration of ODEs using SciPy","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate import sympy os.chdir(r'D:\\projects\\wordpress\\ex35') os.getcwd() def plot_direction_field(x, y_x, f_xy, x_lim=(-5, 5), y_lim=(-5, 5), ax=None): f_np = sympy.lambdify((x, y_x), f_xy, 'numpy') x_vec = np.linspace(x_lim[0], x_lim[1], 20) y_vec = np.linspace(y_lim[0], y_lim[1], 20) if ax is None: _, ax =\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example35","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example35.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1786"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1786\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}