{"id":1254,"date":"2021-12-04T03:06:06","date_gmt":"2021-12-04T11:06:06","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1254"},"modified":"2021-12-04T03:26:19","modified_gmt":"2021-12-04T11:26:19","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2","status":"publish","type":"post","link":"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-2\/","title":{"rendered":"#225 Finite difference method to solve ODE using python"},"content":{"rendered":"<p>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.<br \/>\nFind the velocity at launch v0=y'(0)? Ignore the drag of the air resistance.<br \/>\nBoundary conditions are y(0) = 0 and y(5) = 50.<br \/>\nd^2y\/dt^2=-g<br \/>\nThe result should be y'(0)=34.5 m\/s.<\/p>\n<p>d^2y\/dt^2=(y(i-1) &#8211; 2*y(i) + y(i+1))\/(h^2)<\/p>\n<p>Since the time interval is [0,5] and we have n = 100, h = (5-0)\/n, using the finite difference approximated derivatives, we obtain<br \/>\ny(0) = 0<br \/>\ny(i\u22121) \u2212 2y(i) + y(i+1) = \u2212g*h^2, (i = 1,2,&#8230;,n \u2212 1),<br \/>\ny(n) = 50<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nplt.style.use(&quot;seaborn-poster&quot;)\r\nn = 100\r\nh = (5-0) \/ n\r\n# Get A\r\nA = np.zeros((n+1, n+1))\r\nA&#x5B;0, 0] = 1\r\nA&#x5B;n, n] = 1\r\n\r\nfor i in range(1, n):\r\n    A&#x5B;i, i-1] = 1\r\n    A&#x5B;i, i] = -2\r\n    A&#x5B;i, i+1] = 1\r\nprint(A)\r\n\r\n# Get b\r\nb = np.zeros(n+1)\r\nb&#x5B;1:-1] = -9.8*h**2\r\nb&#x5B;-1] = 50\r\nprint(b)\r\n\r\n# solve the linear equations\r\ny = np.linalg.solve(A, b)\r\nt = np.linspace(0, 5, n+1)\r\nplt.figure(figsize=(10,8))\r\nplt.plot(t, y)\r\nplt.plot(5, 50, &quot;ro&quot;)\r\nplt.xlabel(&quot;time (s)&quot;)\r\nplt.ylabel(&quot;altitude (m)&quot;)\r\nplt.savefig('ex225.png', dpi=72)\r\nplt.show()\r\ny_n_1 = -9.8*h**2 + 2*y&#x5B;0] - y&#x5B;1]\r\nv0=(y&#x5B;1] - y_n_1) \/ (2*h)\r\nprint(v0)\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\/12\/ex225.png?resize=720%2C576&#038;ssl=1\" alt=\"\" width=\"720\" height=\"576\" class=\"alignnone size-full wp-image-1255\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex225.png 720w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex225-480x384.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 720px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 conditions are y(0) = 0 [&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-1254","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-ke","jetpack_likes_enabled":true,"jetpack-related-posts":[{"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":1254,"position":0},"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":1261,"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-2-2\/","url_meta":{"origin":1254,"position":1},"title":"#226 Finite difference method to solve ODE using python","author":"gantovnik","date":"2021-12-04","format":false,"excerpt":"Solve the following problem: d^2y\/dt^2=-4*y + 4*x Boundary conditions are y(0) = 0 and y'(\u03c0\/2) = 0. The exact solution of the problem is y = x \u2212 sin(2x*) y(0) = 0, y(i\u22121) \u2212 2*y(i) + y(i+1) \u2212 h^2*(\u22124*y(i) + 4*x(i)) = 0, i = 1,2,...,n \u2212 1 2*y(n\u22121) \u2212\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\/ex226.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex226.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex226.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex226.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":1254,"position":2},"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":1254,"position":3},"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":1254,"position":4},"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":1102,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/192-4th-order-runge-kutta-method\/","url_meta":{"origin":1254,"position":5},"title":"#192 4th order Runge-Kutta method","author":"gantovnik","date":"2021-11-15","format":false,"excerpt":"\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\/example192_2-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/example192_2-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/example192_2-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/example192_2-1.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/example192_2-1.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/example192_2-1.png?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1254","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=1254"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1254\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}