{"id":1363,"date":"2022-01-11T19:02:48","date_gmt":"2022-01-12T03:02:48","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1363"},"modified":"2022-01-11T19:16:30","modified_gmt":"2022-01-12T03:16:30","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2022\/01\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3\/","title":{"rendered":"#259 Interpolation in python"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nfrom numpy import array , poly1d , polyfit\r\nfrom scipy.interpolate import interp1d\r\nimport matplotlib.pyplot as plt\r\n# Data points\r\nxdata = array (&#x5B;2,6,12,18,22,33,40,45,50,57])\r\nydata = array (&#x5B;4.5 ,37 ,120 , 100 ,83.9 ,65 ,64 ,66 ,70 ,71])\r\n\r\nfig,ax = plt.subplots()\r\nnew_x=np.linspace(xdata&#x5B;0],xdata&#x5B;-1],num=100)\r\n#linear \r\nlin = interp1d(xdata , ydata , kind='linear')\r\nnew_y=lin(new_x)\r\nax.plot(new_x,new_y,'--',c='green')\r\n# pure polynomial\r\ncoefficients=polyfit(xdata,ydata,len(xdata)-1)\r\npoly=poly1d(coefficients)\r\nnew_y=poly(new_x)\r\nax.plot(new_x,new_y,c='red')\r\n# quadratic spline\r\nqspl=interp1d(xdata , ydata , kind='quadratic')\r\nnew_y=qspl(new_x)\r\nax.plot(new_x,new_y,'--',c='blue')\r\n# cubic spline\r\ncspl=interp1d(xdata , ydata , kind='cubic')\r\nnew_y=cspl(new_x)\r\nax.plot(new_x,new_y,c='m')\r\n# initial points\r\nax.scatter(xdata,ydata,color='black')\r\n#legend\r\nax.legend (('linear','polinomial','quadratic spline','cubic spline','data'))\r\nax.set(xlabel='x',ylabel='y')\r\nplt.title('Interpolation')\r\nplt.savefig('ex259.png', dpi=90)\r\nplt.show()\r\n#value to interpolate\r\nxv=30.0\r\n#linear \r\nyv_lin = interp1d(xdata , ydata , kind='linear')(xv)\r\n# Pure polynomial\r\nyv_pol = poly1d(polyfit(xdata , ydata , len(xdata)-1))(xv)\r\n# Quadratic spline\r\nyv_qspl = interp1d(xdata , ydata , kind='quadratic')(xv)\r\n# Cubic spline\r\nyv_cspl = interp1d(xdata , ydata , kind='cubic')(xv)\r\nprint(&quot;y_lin =&quot;,yv_lin ,&quot;\\ny_pol =&quot;,yv_pol ,&quot;\\ny_qspl =&quot;,yv_qspl ,\r\n      &quot;\\ny_cspl =&quot;,yv_cspl)\r\n<\/pre>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=576%2C432&#038;ssl=1\" alt=\"\" width=\"576\" height=\"432\" class=\"alignnone size-full wp-image-1364\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png 576w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 576px, 100vw\" \/><\/p>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ny_lin = 70.15454545454546 \r\ny_pol = 68.68763643841513 \r\ny_qspl = 67.5966557972689 \r\ny_cspl = 67.74754052923349\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np from numpy import array , poly1d , polyfit from scipy.interpolate import interp1d import matplotlib.pyplot as plt # Data points xdata = array (&#x5B;2,6,12,18,22,33,40,45,50,57]) ydata = array (&#x5B;4.5 ,37 ,120 , 100 ,83.9 ,65 ,64 ,66 ,70 ,71]) fig,ax = plt.subplots() new_x=np.linspace(xdata&#x5B;0],xdata&#x5B;-1],num=100) #linear lin = interp1d(xdata , ydata , kind=&#8217;linear&#8217;) new_y=lin(new_x) ax.plot(new_x,new_y,&#8217;&#8211;&#8216;,c=&#8217;green&#8217;) [&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-1363","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-lZ","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":107,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/nonlinear-least-square-fit\/","url_meta":{"origin":1363,"position":0},"title":"Nonlinear least square fit","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir('\/home\/vg\/Downloads\/projects\/ex19') os.getcwd() beta=(0.25,0.75,0.5) def f(x,b0,b1,b2): return b0+b1*np.exp(-b2*x**2) xdata=np.linspace(0,5,50) y=f(xdata,*beta) ydata=y+0.05*np.random.randn(len(xdata)) def g(beta): return ydata-f(xdata,*beta) beta_start=(1,1,1) beta_opt,beta_cov=scipy.optimize.leastsq(g,beta_start) print(beta_opt) fig,ax=plt.subplots() ax.scatter(xdata,ydata,label='samples') ax.plot(xdata,y,'r',lw=2,label='true model') ax.plot(xdata,f(xdata,*beta_opt),'b',lw=2,label='fitted model') ax.set_xlim(0,5) ax.set_xlabel(r\"$x$\",fontsize=18) ax.set_ylabel(r\"$f(x,\\beta)$\",fontsize=18) ax.legend() ax.set_title(\"Nonlinear least square fitting\") plt.savefig(\"example19.png\", dpi=100) plt.show() plt.close() beta_opt,beta_cov=scipy.optimize.curve_fit(f,xdata,ydata) print(beta_opt) \u00a0","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example19","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2099,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/410-2d-random-walk-animation-in-python\/","url_meta":{"origin":1363,"position":1},"title":"#410 2D random walk animation in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"[code language=\"python\"] import numpy import random import matplotlib.pyplot as plt from matplotlib import animation # define some plot variables fig, ax = plt.subplots(figsize=(8,8)) bound = 25 ax.set_xlim(-bound,bound) ax.set_ylim(-bound,bound) # define a numpy array to hold the locations visited on the random walk locations = numpy.zeros((1,2)) # 1 row, 2 columns\u2026","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\/2024\/01\/ex410.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex410.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":139,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/multidimensional-spline\/","url_meta":{"origin":1363,"position":2},"title":"Multidimensional Spline","author":"gantovnik","date":"2019-01-04","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\\ex29') os.getcwd() np.random.seed(115925231) x = y = np.linspace(-1, 1, 100) X, Y = np.meshgrid(x, y) def f(x, y): return np.exp(-x**2 - y**2) * np.cos(4*x) * np.sin(6*y) Z = f(X, Y) N = 500 xdata = np.random.uniform(-1,\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example29","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example29.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":1363,"position":3},"title":"#425 An animation of a bouncing ball using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"[code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import matplotlib. Animation as animation # Acceleration due to gravity, m.s-2. g = 9.81 # The maximum x-range of ball's trajectory to plot. XMAX = 5 # The coefficient of restitution for bounces (-v_up\/v_down). cor = 0.65 # The time\u2026","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\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/05\/ex425-1.gif?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":130,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-2\/","url_meta":{"origin":1363,"position":4},"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":133,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/spline-interpolation-3\/","url_meta":{"origin":1363,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1363","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=1363"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1363\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}