{"id":62,"date":"2018-12-24T11:37:06","date_gmt":"2018-12-24T11:37:06","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=62"},"modified":"2018-12-24T11:37:06","modified_gmt":"2018-12-24T11:37:06","slug":"piecewise-linear-interpolation","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/piecewise-linear-interpolation\/","title":{"rendered":"Piecewise linear interpolation"},"content":{"rendered":"<pre>import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nos.chdir('\/home\/vg\/Downloads\/projects\/ex8')\nos.getcwd()\nplt.figure(figsize=(10,8)) \nx = np.linspace(0,1,500)\ny = np.sqrt(1-x**2)\nxp = np.linspace(0,1,6)\nyp = np.sqrt(1-xp**2)\nxi = np.arange(0.1,1.0,0.2)\nyi = np.interp(xi,xp,yp)\nplt.plot(x,y,'b',label='ideal')\nplt.plot(xp,yp,'or',label='interpolation points')\nplt.plot(xp,yp,'--r',label='piecewise linear function')\nplt.plot(xi,yi,'sg',label='interpolated values')\nplt.legend(loc='best')\nplt.grid()\nplt.axis('scaled')\nplt.axis([0,1.1,0,1.1])\nplt.title('Piecewise linear interpolation')\nplt.savefig(\"example8.png\", dpi=100)\nplt.show()\nplt.close()\n\n<img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-63 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=680%2C544\" alt=\"example8\" width=\"680\" height=\"544\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?w=1000&amp;ssl=1 1000w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=300%2C240&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=768%2C614&amp;ssl=1 768w\" sizes=\"(max-width: 680px) 100vw, 680px\" \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np os.chdir(&#8216;\/home\/vg\/Downloads\/projects\/ex8&#8242;) os.getcwd() plt.figure(figsize=(10,8)) x = np.linspace(0,1,500) y = np.sqrt(1-x**2) xp = np.linspace(0,1,6) yp = np.sqrt(1-xp**2) xi = np.arange(0.1,1.0,0.2) yi = np.interp(xi,xp,yp) plt.plot(x,y,&#8217;b&#8217;,label=&#8217;ideal&#8217;) plt.plot(xp,yp,&#8217;or&#8217;,label=&#8217;interpolation points&#8217;) plt.plot(xp,yp,&#8217;&#8211;r&#8217;,label=&#8217;piecewise linear function&#8217;) plt.plot(xi,yi,&#8217;sg&#8217;,label=&#8217;interpolated values&#8217;) plt.legend(loc=&#8217;best&#8217;) plt.grid() plt.axis(&#8216;scaled&#8217;) plt.axis([0,1.1,0,1.1]) plt.title(&#8216;Piecewise linear interpolation&#8217;) plt.savefig(&#8220;example8.png&#8221;, dpi=100) plt.show() plt.close()<\/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-62","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-10","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":73,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/spline-interpolation\/","url_meta":{"origin":62,"position":0},"title":"Spline interpolation","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np from scipy.interpolate import spline os.chdir('\/home\/vg\/Downloads\/projects\/ex11') os.getcwd() plt.figure(figsize=(10,8)) xp = np.linspace(0,1,6) yp = np.sqrt(1-xp**2) xi = np.linspace(0,1,100) yi = np.interp(xi,xp,yp) ys = spline(xp,yp,xi) plt.plot(xp,yp,'o',label='given points',lw=2) plt.plot(xi,yi,'--',label='piecewise linear',lw=2) plt.plot(xi,ys,'-',label='spline',lw=2) plt.legend(loc='best') plt.grid() plt.xlabel('x') plt.ylabel('y') plt.title(r'Spline interpolation of $y=\\sqrt{1-x^2}$') plt.axis('scaled') plt.axis([0,1.2,0,1.2]) plt.savefig(\"example11.png\", dpi=100) plt.show()\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example11","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example11.png?resize=525%2C300 1.5x"},"classes":[]},{"id":5,"url":"https:\/\/gantovnik.com\/bio-tips\/2016\/12\/example-1-interpolation\/","url_meta":{"origin":62,"position":1},"title":"Example 1: Interpolation","author":"gantovnik","date":"2016-12-08","format":false,"excerpt":"Example 1: Interpolation","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example1","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":260,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/interpolation\/","url_meta":{"origin":62,"position":2},"title":"Interpolation","author":"gantovnik","date":"2019-01-15","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example50","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example50-1.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":62,"position":3},"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":1142,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/201-polyfit-and-polyval\/","url_meta":{"origin":62,"position":4},"title":"#201 Polyfit and polyval","author":"gantovnik","date":"2021-11-22","format":false,"excerpt":"","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\/ex201.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex201.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex201.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex201.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1116,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/198-linear-interpolation\/","url_meta":{"origin":62,"position":5},"title":"#198 Linear interpolation","author":"gantovnik","date":"2021-11-20","format":false,"excerpt":"","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\/ex198.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/62","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=62"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}