{"id":124,"date":"2019-01-03T15:57:15","date_gmt":"2019-01-03T23:57:15","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=124"},"modified":"2019-01-03T15:57:15","modified_gmt":"2019-01-03T23:57:15","slug":"polynomial-fit","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/polynomial-fit\/","title":{"rendered":"Polynomial fit"},"content":{"rendered":"<pre>import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom numpy import polynomial as P\nos.chdir(r'D:\\data\\scripts\\web1\\ex24')\nos.getcwd()\nx = np.array([1, 2, 3, 4])\ny = np.array([1, 3, 5, 4])\nf1 = P.Polynomial.fit(x, y, 1)\nf2 = P.Polynomial.fit(x, y, 2)\nf3 = P.Polynomial.fit(x, y, 3)\nxx = np.linspace(x.min(), x.max(), 100)\nfig, ax = plt.subplots(1, 1, figsize=(8, 4))\nax.plot(xx, f1(xx), 'r', lw=2, label='1st order')\nax.plot(xx, f2(xx), 'g', lw=2, label='2nd order')\nax.plot(xx, f3(xx), 'b', lw=2, label='3rd order')\nax.scatter(x, y, label='data points')\nax.legend(loc=4)\nax.set_xticks(x)\nax.set_ylabel(r\"$y$\", fontsize=18)\nax.set_xlabel(r\"$x$\", fontsize=18)\nax.set_title(\"Polynomial fit\")\nfig.tight_layout()\nplt.savefig(\"example24.png\", dpi=100)\nplt.show()\nplt.close()\n\n<img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-125 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=606%2C303\" alt=\"example24\" width=\"606\" height=\"303\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?w=800&amp;ssl=1 800w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example24.png?resize=768%2C384&amp;ssl=1 768w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r&#8217;D:\\data\\scripts\\web1\\ex24&#8242;) os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) f1 = P.Polynomial.fit(x, y, 1) f2 = P.Polynomial.fit(x, y, 2) f3 = P.Polynomial.fit(x, y, 3) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(1, 1, [&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-124","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":null,"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-20","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":121,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/polynomial-interpolation\/","url_meta":{"origin":124,"position":0},"title":"Polynomial interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P from scipy import linalg os.chdir(r'D:\\data\\scripts\\web1\\ex23') os.getcwd() x = np.array([1, 2, 3, 4]) y = np.array([1, 3, 5, 4]) deg = len(x) - 1 A = P.polynomial.polyvander(x, deg) c = linalg.solve(A, y) f1 = P.Polynomial(c)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example23","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example23.png?resize=525%2C300 1.5x"},"classes":[]},{"id":127,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/runge-problem\/","url_meta":{"origin":124,"position":1},"title":"Runge problem","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from numpy import polynomial as P os.chdir(r'D:\\data\\scripts\\web1\\ex25') os.getcwd() # In the mathematical field of numerical analysis, Runge's phenomenon # is a problem of oscillation at the edges of an interval that occurs # when using polynomial interpolation with polynomials of\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example25","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example25.png?resize=525%2C300 1.5x"},"classes":[]},{"id":130,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-2\/","url_meta":{"origin":124,"position":2},"title":"Spline interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex26') os.getcwd() def runge(x): return 1\/(1 + 25 * x**2) x = np.linspace(-1, 1, 11) y = runge(x) f = interpolate.interp1d(x, y, kind=3) xx = np.linspace(-1, 1, 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(xx, runge(xx), 'k',\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example26","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example26.png?resize=525%2C300 1.5x"},"classes":[]},{"id":88,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-least-square-fit\/","url_meta":{"origin":124,"position":3},"title":"#14 Linear least square fit using python","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example13","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":133,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-3\/","url_meta":{"origin":124,"position":4},"title":"#27 Spline interpolation","author":"gantovnik","date":"2019-01-03","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy import interpolate os.chdir(r'D:\\data\\scripts\\web1\\ex27') os.getcwd() x = np.array([0, 1, 2, 3, 4, 5, 6, 7]) y = np.array([3, 4, 3.5, 2, 1, 1.5, 1.25, 0.9]) xx = np.linspace(x.min(), x.max(), 100) fig, ax = plt.subplots(figsize=(8, 4)) ax.scatter(x, y) for n\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example27","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example27.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":124,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/124","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=124"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}