{"id":5,"date":"2016-12-08T07:56:08","date_gmt":"2016-12-08T07:56:08","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=5"},"modified":"2024-01-08T10:26:20","modified_gmt":"2024-01-08T18:26:20","slug":"example-1-interpolation","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2016\/12\/example-1-interpolation\/","title":{"rendered":"Example 1: Interpolation"},"content":{"rendered":"<h5 id=\"subject_4\">Example 1: Interpolation<\/h5>\n<p><code><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignnone size-medium wp-image-6\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1-300x200.png?resize=300%2C200\" alt=\"example1\" width=\"300\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1.png?resize=300%2C200&amp;ssl=1 300w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1.png?w=600&amp;ssl=1 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#Example 1: Interpolation\r\nimport numpy as np\r\nfrom scipy import interpolate\r\nimport matplotlib.pyplot as plt\r\nimport os\r\nos.chdir('C:\\\\Anaconda\\\\mycodes\\\\aerospace')\r\nos.getcwd()\r\n#create the data points and add noise as follows\r\nx = np.linspace(-18,18,36)\r\nnoise = 0.1 * np.random.random(len(x))\r\nsignal = np.sinc(x) + noise\r\n#create a linear interpolation function, and then apply it to an input array\r\n#with five times as many data points\r\ninterpolated = interpolate.interp1d(x,signal)\r\nx2 = np.linspace(-18,18,180)\r\ny = interpolated(x2)\r\n#cubic interpolation\r\ncubic = interpolate.interp1d(x,signal,kind=&quot;cubic&quot;)\r\ny2 = cubic(x2)\r\n#plot the results\r\nplt.plot(x,signal,'o',label=&quot;data&quot;)\r\nplt.plot(x2,y,'-',label='linear')\r\nplt.plot(x2,y,'--',label='cubic')\r\nplt.legend()\r\nplt.savefig(&quot;example1.png&quot;, dpi=300)\r\nplt.show()\r\nplt.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example 1: Interpolation #Example 1: Interpolation import numpy as np from scipy import interpolate import matplotlib.pyplot as plt import os os.chdir(&#8216;C:\\\\Anaconda\\\\mycodes\\\\aerospace&#8217;) os.getcwd() #create the data points and add noise as follows x = np.linspace(-18,18,36) noise = 0.1 * np.random.random(len(x)) signal = np.sinc(x) + noise #create a linear interpolation function, and then apply it to an [&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":[69,2],"tags":[4,70,3],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-matplotlib","category-python","tag-interpolation","tag-matplotlib","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-5","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":260,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/interpolation\/","url_meta":{"origin":5,"position":0},"title":"Interpolation","author":"gantovnik","date":"2019-01-15","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d os.chdir(r'D:\\data\\scripts\\wordpress\\ex50') os.getcwd() A,nu,k=10,4,2 def f(x,A,nu,k): return A*np.exp(-k*x)*np.cos(2*np.pi*nu*x) xmax,nx=0.5,8 x=np.linspace(0,xmax,nx) y=f(x,A,nu,k) f_nearest=interp1d(x,y,kind='nearest') f_linear =interp1d(x,y) f_cubic =interp1d(x,y,kind='cubic') x2=np.linspace(0,xmax,100) plt.plot(x,y,'o',label='datapoints') plt.plot(x2,f(x2,A,nu,k),label='exact') plt.plot(x2,f_nearest(x2),label='nearest') plt.plot(x2, f_linear(x2), label='linear') plt.plot(x2, f_cubic(x2), label='cubic') plt.legend() plt.show() plt.tight_layout() plt.savefig(\"example50.png\", dpi=100) plt.show() plt.close() [\/code]","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":62,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/piecewise-linear-interpolation\/","url_meta":{"origin":5,"position":1},"title":"Piecewise linear interpolation","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\/ex8') 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,'b',label='ideal') plt.plot(xp,yp,'or',label='interpolation points') plt.plot(xp,yp,'--r',label='piecewise linear function') plt.plot(xi,yi,'sg',label='interpolated values') plt.legend(loc='best') plt.grid() plt.axis('scaled') plt.axis([0,1.1,0,1.1]) plt.title('Piecewise linear interpolation') plt.savefig(\"example8.png\", dpi=100) plt.show() plt.close()","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example8","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example8.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1123,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/199-cubic-spline-interpolation-using-scipy\/","url_meta":{"origin":5,"position":2},"title":"#199 Cubic spline interpolation using scipy","author":"gantovnik","date":"2021-11-20","format":false,"excerpt":"[code language=\"python\"] # Cubic spline interpolation from scipy.interpolate import CubicSpline import numpy as np import matplotlib.pyplot as plt plt.style.use(\"seaborn-poster\") x = [0, 1, 2] y = [1, 3, 2] # use bc_type = \"natural\" adds the constraints f = CubicSpline(x, y, bc_type=\"natural\") x_new = np.linspace(0, 2, 100) y_new = f(x_new)\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\/ex199-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex199-1.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":73,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/spline-interpolation\/","url_meta":{"origin":5,"position":3},"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":1746,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":5,"position":4},"title":"#331 Interpolation with Newton&#8217;s polynomial using python","author":"gantovnik","date":"2023-01-04","format":false,"excerpt":"interpolation.py [code language=\"python\"] def interpolation(c,x,x0): # Evaluate Newton's polynomial at x0. # Degree of polynomial n = len(x) - 1 y0 = c[n] for k in range(1,n+1): y0 = c[n-k] + (x0 - x[n-k]) * y0 return y0 def coef(x,y): # Computes the coefficients of Newton's polynomial. # Number of\u2026","rel":"","context":"In &quot;interpolation&quot;","block_context":{"text":"interpolation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/interpolation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex331.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1116,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/198-linear-interpolation\/","url_meta":{"origin":5,"position":5},"title":"#198 Linear interpolation","author":"gantovnik","date":"2021-11-20","format":false,"excerpt":"[code language=\"python\"] # Linear interpolation from scipy.interpolate import interp1d import matplotlib.pyplot as plt plt.style.use(\"seaborn-poster\") x = [0, 1, 2] y = [1, 3, 2] f = interp1d(x, y) y_hat = f(1.5) print(y_hat) my_dpi=100 plt.figure(figsize = (795\/my_dpi,447\/my_dpi),dpi=my_dpi) plt.plot(x, y, \"-ob\") plt.plot(1.5, y_hat, \"ro\") plt.title(\"Linear Interpolation at x = 1.5\") plt.xlabel(\"x\") plt.ylabel(\"y\")\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\/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\/5","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=5"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"predecessor-version":[{"id":2031,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/5\/revisions\/2031"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}