{"id":1793,"date":"2023-02-16T03:21:26","date_gmt":"2023-02-16T11:21:26","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1793"},"modified":"2023-03-09T19:53:53","modified_gmt":"2023-03-10T03:53:53","slug":"341-ridgeline-plot-using-plotly","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/02\/341-ridgeline-plot-using-plotly\/","title":{"rendered":"#341 Solving ODE using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2023\/02\/341-ridgeline-plot-using-plotly\/ex341\/\" rel=\"attachment wp-att-1803\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341.png?resize=600%2C400&#038;ssl=1\" alt=\"\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-1803\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341.png 600w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341-480x320.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 600px, 100vw\" \/><\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport scipy.integrate as si\r\nos.chdir(r'D:\\projects\\wordpress\\ex341')\r\nos.getcwd()\r\n\r\ndef model1(y,t):\r\n    k = 0.1\r\n    dydt = -k*y\r\n    return dydt\r\n\r\ndef model2(y,t):\r\n    k = 0.5\r\n    dydt = -k*y\r\n    return dydt\r\n\r\ninits = &#x5B;5]\r\nts = np.linspace (0, 20)\r\nys1 = si.odeint(model1 , inits , ts)\r\nplt.plot(ts,ys1)\r\nys2 = si.odeint(model2 , inits , ts)\r\nplt.plot(ts,ys2)\r\n\r\nplt.legend(&#x5B;'k=0.1','k=0.5'])\r\n\r\nplt.savefig(&quot;ex341.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 import numpy as np import scipy.integrate as si os.chdir(r&#8217;D:\\projects\\wordpress\\ex341&#8242;) os.getcwd() def model1(y,t): k = 0.1 dydt = -k*y return dydt def model2(y,t): k = 0.5 dydt = -k*y return dydt inits = &#x5B;5] ts = np.linspace (0, 20) ys1 = si.odeint(model1 , inits , ts) plt.plot(ts,ys1) ys2 = si.odeint(model2 [&hellip;]<\/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":"off","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_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":""},"categories":[77,2],"tags":[78,79,3],"class_list":["post-1793","post","type-post","status-publish","format-standard","hentry","category-numerical","category-python","tag-numerical","tag-ode","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-sV","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":1793,"position":0},"title":"#44 2nd-order Runge-Kutta type A using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex44') os.getcwd() #2nd-order Runge-Kutta methods with A=1\/2 (type A) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2A(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x\u2026","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example44","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":217,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-b\/","url_meta":{"origin":1793,"position":1},"title":"#45  2nd-order Runge-Kutta type B using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex45') os.getcwd() #2nd-order Runge-Kutta methods with A=0 (type B) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2B(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x = x_range[0]\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":"example45","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=525%2C300 1.5x"},"classes":[]},{"id":220,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-c\/","url_meta":{"origin":1793,"position":2},"title":"#46 2nd-order Runge-Kutta type C using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex46') os.getcwd() #2nd-order Runge-Kutta methods with A=1\/3 (type C) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2C(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x = x_range[0]\u2026","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example46","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1242,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1793,"position":3},"title":"#223 Initial value problem in python","author":"gantovnik","date":"2021-11-29","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt class IV_Problem: \"\"\" Initial value problem (IVP) class \"\"\" def __init__(self, rhs, y0, interval, name='IVP'): \"\"\" rhs 'right hand side' function of the ordinary differential equation f(t,y) y0 array with initial values interval start and end value of the interval\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\/ex223.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":449,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/08\/smoothing-the-noise-in-real-world-data\/","url_meta":{"origin":1793,"position":4},"title":"#65 Smoothing the Noise in Real-world Data","author":"gantovnik","date":"2019-08-22","format":false,"excerpt":"#Smoothing the noise in real-world data #This window rolls over the data and is used to compute the average over that window. import matplotlib.pyplot as plt import numpy as np import os os.chdir(r'D:\\projects\\wordpress\\ex65') def moving_average(interval, window_size): #Compute convoluted window for given size window = np.ones(int(window_size)) \/ float(window_size) return np.convolve(interval, window,\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\/08\/ex65.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=1400%2C800&ssl=1 4x"},"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":1793,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1793","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=1793"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1793\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}