{"id":143,"date":"2019-01-06T00:45:59","date_gmt":"2019-01-06T08:45:59","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=143"},"modified":"2024-12-13T11:15:10","modified_gmt":"2024-12-13T19:15:10","slug":"143","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/143\/","title":{"rendered":"#30 Newton&#8217;s Law of Cooling"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\" wp-image-144 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=610%2C305\" alt=\"example30\" width=\"610\" height=\"305\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example30.png?resize=768%2C384&amp;ssl=1 768w\" sizes=\"(max-width: 610px) 100vw, 610px\" \/><\/p>\n<pre class=\"lang:python decode:true \">import os\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\nmpl.rcParams['text.usetex'] = True\nimport sympy\nfrom IPython.display import display\nsympy.init_printing()\n#%matplotlib inline\n#%config InlineBackend.figure_format='retina'\nos.chdir(r'D:\\projects\\wordpress\\ex30')\nos.getcwd()\n#Symbolic ODE solving with SymPy\n#Newton's law of cooling\nt, k, T0, Ta = sympy.symbols(\"t, k, T_0, T_a\")\nT = sympy.Function(\"T\")\node = T(t).diff(t) + k*(T(t) - Ta)\node_sol = sympy.dsolve(ode)\ndisplay(ode_sol.lhs)\ndisplay(ode_sol.rhs)\nics = {T(0): T0}\ndisplay(ics)\nC_eq = sympy.Eq(ode_sol.lhs.subs(t, 0).subs(ics), ode_sol.rhs.subs(t, 0))\ndisplay(C_eq)\nC_sol = sympy.solve(C_eq)\ndisplay(C_sol)\ndisplay(ode_sol.subs(C_sol[0]))&lt;\/pre&gt;\ndef apply_ics(sol, ics, x, known_params):\n# Apply the initial conditions (ics), given as a dictionary on\n# the form ics = {y(0): y0: y(x).diff(x).subs(x, 0): yp0, ...}\n# to the solution of the ODE with indepdendent variable x.\n# The undetermined integration constants C1, C2, ... are extracted\n# from the free symbols of the ODE solution, excluding symbols in\n# the known_params list.\nfree_params = sol.free_symbols - set(known_params)\neqs = [(sol.lhs.diff(x, n) - sol.rhs.diff(x, n)).subs(x, 0).subs(ics)\nfor n in range(len(ics))]\nsol_params = sympy.solve(eqs, free_params)\nreturn sol.subs(sol_params)\n\ndisplay(ode_sol)\napply_ics(ode_sol, ics, t, [k, Ta])\node_sol = apply_ics(ode_sol, ics, t, [k, Ta]).simplify()\ndisplay(ode_sol)\ny_x = sympy.lambdify((t, k), ode_sol.rhs.subs({T0: 5, Ta: 1}), 'numpy')\nfig, ax = plt.subplots(figsize=(8, 4))\nx = np.linspace(0, 4, 100)\nfor k in [1, 2, 3]:\nax.plot(x, y_x(x, k), label=r\"$k=%d$\" % k)\n\nax.set_title(r\"$%s$\" % sympy.latex(ode_sol), fontsize=18)\nax.set_xlabel(r\"$x$\", fontsize=18)\nax.set_ylabel(r\"$y$\", fontsize=18)\nax.legend()\nfig.tight_layout()\nplt.savefig(\"example30.png\", dpi=100)\nplt.show()\nplt.close()<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams[&#8216;text.usetex&#8217;] = True import sympy from IPython.display import display sympy.init_printing() #%matplotlib inline #%config InlineBackend.figure_format=&#8217;retina&#8217; os.chdir(r&#8217;D:\\projects\\wordpress\\ex30&#8242;) os.getcwd() #Symbolic ODE solving with SymPy #Newton&#8217;s law of cooling t, k, T0, Ta = sympy.symbols(&#8220;t, k, T_0, T_a&#8221;) T = sympy.Function(&#8220;T&#8221;) ode = T(t).diff(t) + [&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-143","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\/s8bH0k-143","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":148,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/damped-harmonic-oscillator\/","url_meta":{"origin":143,"position":0},"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":[]},{"id":154,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/inexact-solutions-to-odes\/","url_meta":{"origin":143,"position":1},"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":2140,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/417-polar-plot-using-sympy-plotting-functions-in-python\/","url_meta":{"origin":143,"position":2},"title":"#417 Polar plot using SymPy plotting functions in python","author":"gantovnik","date":"2024-02-21","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex417.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex417.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex417.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex417.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2135,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/02\/416-plot-using-sympy-in-python\/","url_meta":{"origin":143,"position":3},"title":"#416 Plot using sympy and plot_implicit in python","author":"gantovnik","date":"2024-02-21","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex416.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex416.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex416.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/02\/ex416.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1793,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/02\/341-ridgeline-plot-using-plotly\/","url_meta":{"origin":143,"position":4},"title":"#341 Solving ODE using python","author":"gantovnik","date":"2023-02-16","format":false,"excerpt":"","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/02\/ex341.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1875,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/06\/354-animation-in-python\/","url_meta":{"origin":143,"position":5},"title":"#354 Animation in python","author":"gantovnik","date":"2023-06-30","format":false,"excerpt":"","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\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/06\/ex354.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/143","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=143"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":6682,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/143\/revisions\/6682"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}