{"id":1261,"date":"2021-12-04T03:33:25","date_gmt":"2021-12-04T11:33:25","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1261"},"modified":"2021-12-04T03:33:25","modified_gmt":"2021-12-04T11:33:25","slug":"210-parametric-curve-in-3d-2-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-2\/","title":{"rendered":"#226 Finite difference method to solve ODE using python"},"content":{"rendered":"<p>Solve the following problem:<br \/>\nd^2y\/dt^2=-4*y + 4*x<\/p>\n<p>Boundary conditions are<br \/>\ny(0) = 0 and y'(\u03c0\/2) = 0.<\/p>\n<p>The exact solution of the problem is<br \/>\ny = x \u2212 sin(2x*)<\/p>\n<p>y(0) = 0,<br \/>\ny(i\u22121) \u2212 2*y(i) + y(i+1) \u2212 h^2*(\u22124*y(i) + 4*x(i)) = 0, i = 1,2,&#8230;,n \u2212 1<br \/>\n2*y(n\u22121) \u2212 2*y(n) \u2212 h^2*(\u22124*y(n) + 4*x(n)) = 0.<\/p>\n<p>y(n+1) = y(n\u22121)<\/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\n\r\ndef get_a_b(n):\r\n    h = (np.pi\/2-0) \/ n\r\n    x = np.linspace(0, np.pi\/2, n+1)\r\n    # Get A\r\n    A = np.zeros((n+1, n+1))\r\n    A&#x5B;0, 0] = 1\r\n    A&#x5B;n, n] = -2+4*h**2\r\n    A&#x5B;n, n-1] = 2\r\n    for i in range(1, n):\r\n        A&#x5B;i, i-1] = 1\r\n        A&#x5B;i, i] = -2+4*h**2\r\n        A&#x5B;i, i+1] = 1\r\n    # Get b\r\n    b = np.zeros(n+1)\r\n    for i in range(1, n+1):\r\n        b&#x5B;i] = 4*h**2*x&#x5B;i]\r\n    return x, A, b\r\n\r\nx = np.pi\/2\r\nv = x - np.sin(2*x)\r\nn_s = &#x5B;]\r\nerrors = &#x5B;]\r\nfor n in range(3, 100, 5):\r\n    x, A, b = get_a_b(n)\r\n    y = np.linalg.solve(A, b)\r\n    n_s.append(n)\r\n    e = v - y&#x5B;-1]\r\n    errors.append(e)\r\nplt.figure(figsize = (10,8))\r\nplt.plot(n_s, errors)\r\nplt.yscale(&quot;log&quot;)\r\nplt.xlabel(&quot;n gird points&quot;)\r\nplt.ylabel(&quot;errors at x = $\\pi\/2$&quot;)\r\n\r\nplt.savefig('ex226.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\/12\/ex226.png?resize=720%2C576&#038;ssl=1\" alt=\"\" width=\"720\" height=\"576\" class=\"alignnone size-full wp-image-1262\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex226.png 720w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/12\/ex226-480x384.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 720px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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,&#8230;,n \u2212 1 2*y(n\u22121) \u2212 2*y(n) \u2212 h^2*(\u22124*y(n) + 4*x(n)) [&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-1261","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-kl","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":58,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/fourier-expansion-of-a-rectangular-wave\/","url_meta":{"origin":1261,"position":0},"title":"#7: Fourier expansion of a rectangular wave","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex7') os.getcwd() plt.figure(figsize=(10,8)) N = 2**8 t = np.linspace(0,1,N) y = np.zeros(N) for n in range(1,32,2): y = y + 4\/(np.pi*n)*np.sin(2*np.pi*n*t*2) plt.plot(t,y) plt.axis([0,1,-1.4,1.4]) plt.grid() plt.xlabel('Time, sec') plt.ylabel('Value') plt.title('Fourier expansion of a rectangular wave') plt.savefig(\"example7.png\", dpi=100) plt.show() plt.close()","rel":"","context":"In &quot;math&quot;","block_context":{"text":"math","link":"https:\/\/gantovnik.com\/bio-tips\/category\/math\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1109,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/193-animation-using-python\/","url_meta":{"origin":1261,"position":1},"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":894,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/160-max-and-min-using-argmax-and-argmax-in-python\/","url_meta":{"origin":1261,"position":2},"title":"#160 Max and min using argmax and argmax in python","author":"gantovnik","date":"2021-02-14","format":false,"excerpt":"#160 Max and min using argmax and argmax in python [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import os os.chdir(r'D:\\projects\\wordpress\\ex160') os.getcwd() def main(): N = 100 L = 1 def f(i, n): x = i * L \/ N lam = 2 * L \/ (n+1) return\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\/02\/example160.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2065,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/408-animated-line-plot-in-python\/","url_meta":{"origin":1261,"position":3},"title":"#408 Animated line plot in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy as np from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation plt.style.use('seaborn-pastel') fig = plt.figure() n=10 ax = plt.axes(xlim=(0, n), ylim=(-2, 2)) line, = ax.plot([], [], c=\"blue\") def init(): line.set_data([], []) return line, def animate(i): n = 10 x = np.linspace(0, n, 1000) y =\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\/01\/ex408-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex408-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":217,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-b\/","url_meta":{"origin":1261,"position":4},"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":3022,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/","url_meta":{"origin":1261,"position":5},"title":"#442 The partial sums of the Fourier series for the square-wave function using python","author":"gantovnik","date":"2024-07-25","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider nmax = 5 pi = np.pi x = np.linspace(-2*pi, 2*pi, 1001) def f(xarray): y = np.zeros_like(xarray) for ind, x in enumerate(xarray): xmod = x%(2*pi) if xmod<pi: y[ind] = 1 if x%pi==0: y[ind]= np.nan return y def Fourier(x, nmax):\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\/07\/ex442_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1261","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=1261"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1261\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}