{"id":926,"date":"2021-06-15T20:32:40","date_gmt":"2021-06-16T03:32:40","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=926"},"modified":"2021-06-15T20:32:40","modified_gmt":"2021-06-16T03:32:40","slug":"166-solution-of-a-differential-equation-using-bubnov-galerkin-method-with-sympy-package","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/06\/166-solution-of-a-differential-equation-using-bubnov-galerkin-method-with-sympy-package\/","title":{"rendered":"#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package"},"content":{"rendered":"<p>#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package<\/p>\n<p>The problem and solution in this pdf file:<br \/>\n<a href=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/06\/ex166.pdf\">ex166<\/a><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sympy\r\nfrom matplotlib import pyplot as plt\r\nimport seaborn as sns\r\nimport numpy as np\r\nfrom sympy.utilities.lambdify import lambdify\r\nfrom sympy import simplify\r\nfrom sympy import *\r\nimport os\r\nos.chdir(r'D:\\projects\\wordpress\\ex166')\r\na, b, x = sympy.symbols(&quot;a, b, x&quot;)\r\nC0, C1, C2, C3 = sympy.symbols(&quot;C0, C1, C2, C3&quot;)\r\nu0 = sympy.Function(&quot;u0&quot;)\r\nu1 = sympy.Function(&quot;u1&quot;)\r\nu2 = sympy.Function(&quot;u2&quot;)\r\nu3 = sympy.Function(&quot;u3&quot;)\r\ny = sympy.Function(&quot;y&quot;)\r\nu0=1 - x\r\nu1=x*(1 - x)\r\nu2=x**2*(1 - x)\r\nu3=x**3*(1 - x)\r\n\r\ny = u0 + C1*u1 + C2*u2 + C3*u3\r\ndy = sympy.diff(y, x)\r\nd2y = sympy.diff(dy, x)\r\nR=d2y+x*dy+y-2*x\r\na=0\r\nb=1\r\ni1 = sympy.integrate(u1*R, (x, a, b))\r\ni2 = sympy.integrate(u2*R, (x, a, b))\r\ni3 = sympy.integrate(u3*R, (x, a, b))\r\nsols=sympy.solve(&#x5B;i1, i2, i3], (C1,C2,C3), dict=True)\r\nC1_sol= &#x5B;sol&#x5B;C1] for sol in sols]&#x5B;0]\r\nC2_sol= &#x5B;sol&#x5B;C2] for sol in sols]&#x5B;0]\r\nC3_sol= &#x5B;sol&#x5B;C3] for sol in sols]&#x5B;0]\r\n\r\nC1_sol_float = &quot;{:.4f}&quot;.format(float(C1_sol))\r\nC2_sol_float = &quot;{:.4f}&quot;.format(float(C2_sol))\r\nC3_sol_float = &quot;{:.4f}&quot;.format(float(C3_sol))\r\n\r\nprint(&quot;C1 =&quot;,C1_sol_float)\r\nprint(&quot;C2 =&quot;,C2_sol_float)\r\nprint(&quot;C3 =&quot;,C3_sol_float)\r\n\r\ny = u0 + C1_sol*u1 + C2_sol*u2 + C3_sol*u3\r\ny_eq=nfloat(simplify(y),5)\r\nprint(y_eq)\r\n\r\nfunc = lambdify(x, y,'numpy')\r\nxvals = np.arange(a,b,.01)\r\nyvals = func(xvals)\r\n\r\nsns.set_theme()\r\nplt.rcParams&#x5B;&quot;figure.figsize&quot;] = (8,8)\r\nfig, ax = plt.subplots(1,1,subplot_kw=dict(aspect='equal'))\r\nax.plot(xvals, yvals)\r\nax.set_xlabel('x')\r\nax.set_ylabel('y(x)')\r\n\r\nplt.text(0,0,'y = {}'.format(y_eq))\r\nplt.savefig(&quot;figure_ex166.pdf&quot;)\r\nplt.show()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package The problem and solution in this pdf file: ex166 import sympy from matplotlib import pyplot as plt import seaborn as sns import numpy as np from sympy.utilities.lambdify import lambdify from sympy import simplify from sympy import * import os os.chdir(r&#8217;D:\\projects\\wordpress\\ex166&#8242;) a, b, x [&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-926","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-eW","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":151,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/direction-fields\/","url_meta":{"origin":926,"position":0},"title":"Direction fields","author":"gantovnik","date":"2019-01-07","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex32') 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\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example32","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example32.png?resize=525%2C300 1.5x"},"classes":[]},{"id":157,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/numerical-integration-of-odes-using-scipy\/","url_meta":{"origin":926,"position":1},"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":[]},{"id":143,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/143\/","url_meta":{"origin":926,"position":2},"title":"#30 Newton&#8217;s Law of Cooling","author":"gantovnik","date":"2019-01-06","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['text.usetex'] = True import sympy from IPython.display import display sympy.init_printing() #%matplotlib inline #%config InlineBackend.figure_format='retina' os.chdir(r'D:\\projects\\wordpress\\ex30') os.getcwd() #Symbolic ODE solving with SymPy #Newton's law of cooling t, k, T0, Ta = sympy.symbols(\"t, k, T_0, T_a\") T =\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example30","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=525%2C300 1.5x"},"classes":[]},{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":926,"position":3},"title":"Inexact solutions to ODEs","author":"gantovnik","date":"2019-01-08","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import sympy from IPython.display import display sympy.init_printing() mpl.rcParams['text.usetex'] = True import sympy os.chdir(r'D:\\projects\\wordpress\\ex33') 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\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example33","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example33.png?resize=525%2C300 1.5x"},"classes":[]},{"id":200,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/heat-equation-1d\/","url_meta":{"origin":926,"position":4},"title":"Heat equation (1D)","author":"gantovnik","date":"2019-01-10","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex41') os.getcwd() N = 5 u0 = 1 u1 = 2 dx = 1.0 \/ (N + 1) A = (np.eye(N, k=-1) - 2 * np.eye(N) + np.eye(N, k=1)) \/ dx**2 print(A) d = -5 * np.ones(N) d[0] -= u0\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example41","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example41.png?resize=525%2C300 1.5x"},"classes":[]},{"id":148,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/damped-harmonic-oscillator\/","url_meta":{"origin":926,"position":5},"title":"Damped harmonic oscillator","author":"gantovnik","date":"2019-01-06","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['text.usetex'] = True import sympy from IPython.display import display sympy.init_printing() def apply_ics(sol, ics, x, known_params): free_params = sol.free_symbols - set(known_params) eqs = [(sol.lhs.diff(x, n) - sol.rhs.diff(x, n)).subs(x, 0).subs(ics) for n in range(len(ics))] sol_params = sympy.solve(eqs, free_params)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example31","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/926","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=926"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}