{"id":1242,"date":"2021-11-29T07:58:12","date_gmt":"2021-11-29T15:58:12","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1242"},"modified":"2021-11-29T07:58:12","modified_gmt":"2021-11-29T15:58:12","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2","status":"publish","type":"post","link":"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\/","title":{"rendered":"#223 Initial value problem in python"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nclass IV_Problem:\r\n    &quot;&quot;&quot;\r\n    Initial value problem (IVP) class\r\n    &quot;&quot;&quot;\r\n    def __init__(self, rhs, y0, interval, name='IVP'):\r\n        &quot;&quot;&quot;\r\n        rhs 'right hand side' function of the ordinary differential\r\n        equation f(t,y)\r\n        y0 array with initial values\r\n        interval start and end value of the interval of independent\r\n        variables often initial and end time\r\n        name descriptive name of the problem\r\n        &quot;&quot;&quot;\r\n        self.rhs = rhs\r\n        self.y0 = y0\r\n        self.t0, self.tend = interval\r\n        self.name = name\r\n\r\ndef rhs(t,y):\r\n    g = 9.81\r\n    l = 1.\r\n    yprime = np.array(&#x5B;y&#x5B;1], g \/ l * np.sin(y&#x5B;0])])\r\n    return yprime\r\n\r\npendulum = IV_Problem(rhs,np.array(&#x5B;np.pi \/ 2, 0.]),&#x5B;0., 20.],'mathem. pendulum')\r\n\r\nclass IVPsolver:\r\n    &quot;&quot;&quot;\r\n    IVP solver class for explicit one-step discretization methods\r\n    with constant step size\r\n    &quot;&quot;&quot;\r\n    def __init__(self, problem, discretization, stepsize):\r\n        self.problem = problem\r\n        self.discretization = discretization\r\n        self.stepsize = stepsize\r\n\r\n    def one_stepper(self):\r\n        yield self.problem.t0, self.problem.y0\r\n        ys = self.problem.y0\r\n        ts = self.problem.t0\r\n        while ts &lt;= self.problem.tend:\r\n            ts, ys = self.discretization(self.problem.rhs, ts, ys,\r\n                                         self.stepsize)\r\n            yield ts, ys\r\n\r\n    def solve(self):\r\n        return list(self.one_stepper())\r\n\r\ndef expliciteuler(rhs, ts, ys, h):\r\n    return ts + h, ys + h * rhs(ts, ys)\r\n\r\ndef rungekutta4(rhs, ts, ys, h):\r\n    k1 = h * rhs(ts, ys)\r\n    k2 = h * rhs(ts + h\/2., ys + k1\/2.)\r\n    k3 = h * rhs(ts + h\/2., ys + k2\/2.)\r\n    k4 = h * rhs(ts + h, ys + k3)\r\n    return ts + h, ys + (k1 + 2*k2 + 2*k3 + k4)\/6.\r\n\r\npendulum_Euler = IVPsolver(pendulum, expliciteuler, 0.001)\r\npendulum_RK4 = IVPsolver(pendulum, rungekutta4, 0.001)\r\nsol_Euler = pendulum_Euler.solve()\r\nsol_RK4 = pendulum_RK4.solve()\r\ntEuler, yEuler = zip(*sol_Euler)\r\ntRK4, yRK4 = zip(*sol_RK4)\r\nplt.subplot(1,2,1)\r\nplt.plot(tEuler,yEuler)\r\nplt.title('Pendulum result with Explicit Euler')\r\nplt.xlabel('Time')\r\nplt.ylabel('Angle and angular velocity')\r\nplt.subplot(1,2,2)\r\nplt.plot(tRK4,abs(np.array(yRK4)-np.array(yEuler)))\r\nplt.title('Difference between methods RK4 and Euler')\r\nplt.xlabel('Time')\r\nplt.ylabel('Angle and angular velocity')\r\nplt.savefig('ex223.png', dpi=72)\r\nplt.show()\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex223.png?resize=460%2C345&#038;ssl=1\" alt=\"\" width=\"460\" height=\"345\" class=\"alignnone size-full wp-image-1243\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex223.png?w=460&amp;ssl=1 460w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex223.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex223.png?resize=150%2C113&amp;ssl=1 150w\" sizes=\"(max-width: 460px) 100vw, 460px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt class IV_Problem: &quot;&quot;&quot; Initial value problem (IVP) class &quot;&quot;&quot; def __init__(self, rhs, y0, interval, name=&#8217;IVP&#8217;): &quot;&quot;&quot; rhs &#8216;right hand side&#8217; function of the ordinary differential equation f(t,y) y0 array with initial values interval start and end value of the interval of independent variables often initial and end [&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":"","_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":[2],"tags":[],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-k2","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":1242,"position":0},"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":1249,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/12\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1242,"position":1},"title":"#224 Shooting method to solve system of ODEs using python","author":"gantovnik","date":"2021-12-04","format":false,"excerpt":"Let y(t) be the altitude of the missile at time t. The gravity g = 9.8 m\/s^2. We want to have the missile at 50 m off the ground after t = 5 s after launch. Find the velocity at launch v0=y'(0)? Ignore the drag of the air resistance. Boundary\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\/12\/ex224.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex224.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex224.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex224.png?resize=700%2C400&ssl=1 2x"},"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":1242,"position":2},"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":2208,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/06\/430-damped-harmonic-oscillator-using-python\/","url_meta":{"origin":1242,"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":1204,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/210-parametric-curve-in-3d-2-2\/","url_meta":{"origin":1242,"position":4},"title":"#212 The double pendulum simulation using python","author":"gantovnik","date":"2021-11-27","format":false,"excerpt":"[code language=\"python\"] from numpy import sin, cos import numpy as np import matplotlib.pyplot as plt import scipy.integrate as integrate import matplotlib.animation as animation from collections import deque G = 9.8 # acceleration due to gravity, in m\/s^2 L1 = 1.0 # length of pendulum 1 in m L2 = 1.0\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\/2021\/11\/ex212.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":1242,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1242","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=1242"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1242\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}