{"id":1407,"date":"2022-02-22T01:14:39","date_gmt":"2022-02-22T09:14:39","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1407"},"modified":"2022-02-22T01:14:39","modified_gmt":"2022-02-22T09:14:39","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2022\/02\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-2\/","title":{"rendered":"#269 Fitting noisy data with a linear equation using python"},"content":{"rendered":"<p>#269 Fitting noisy data with a linear equation using python<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom scipy.optimize import curve_fit\r\n\r\ndef func(x,a,b):\r\n    return a*x+b\r\n\r\nx=np.linspace(0,10,100)\r\ny=func(x,1,2)\r\n\r\n# Adding noise to the data\r\nyn=y+0.9*np.random.normal(size=len(x))\r\n\r\npopt,pcov=curve_fit(func,x,yn)\r\nfig,ax = plt.subplots()\r\nax.plot(x,y,'b-',label='data')\r\nax.plot(x,yn,'.',c='red',label='data with noise')\r\nax.plot(x, func(x, *popt),'g--',label='fit: a=%5.3f, b=%5.3f' % tuple(popt))\r\nplt.xlabel('x')\r\nplt.xlabel('y')\r\nplt.legend()\r\nplt.savefig('ex269.png', dpi=90)\r\nplt.show()\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\/02\/ex269.png?resize=576%2C432&#038;ssl=1\" alt=\"\" width=\"576\" height=\"432\" class=\"alignnone size-full wp-image-1408\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex269.png 576w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex269-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 576px, 100vw\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#269 Fitting noisy data with a linear equation using python import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit def func(x,a,b): return a*x+b x=np.linspace(0,10,100) y=func(x,1,2) # Adding noise to the data yn=y+0.9*np.random.normal(size=len(x)) popt,pcov=curve_fit(func,x,yn) fig,ax = plt.subplots() ax.plot(x,y,&#8217;b-&#8216;,label=&#8217;data&#8217;) ax.plot(x,yn,&#8217;.&#8217;,c=&#8217;red&#8217;,label=&#8217;data with noise&#8217;) ax.plot(x, func(x, *popt),&#8217;g&#8211;&#8216;,label=&#8217;fit: a=%5.3f, b=%5.3f&#8217; % tuple(popt)) plt.xlabel(&#8216;x&#8217;) plt.xlabel(&#8216;y&#8217;) plt.legend() plt.savefig(&#8216;ex269.png&#8217;, dpi=90) [&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-1407","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-mH","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1410,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/02\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":1407,"position":0},"title":"#270 Fitting noisy data with a Gaussian equation using python","author":"gantovnik","date":"2022-02-22","format":false,"excerpt":"#270 Fitting noisy data with a Gaussian equation using python [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit # Creating a function to model and create data def func(x, a, b, c): return a*np.exp(-(x-b)**2\/(2*c**2)) x = np.linspace(0, 10, 100) y = func(x, 1, 5,\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\/2022\/02\/ex270.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex270.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/02\/ex270.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":88,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/linear-least-square-fit\/","url_meta":{"origin":1407,"position":1},"title":"#14 Linear least square fit using python","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt import numpy as np import scipy.linalg as la os.chdir('\/home\/vg\/Downloads\/projects\/ex14') os.getcwd() #define model parameters x=np.linspace(-1,1,200) a,b,c=1,2,3 y_exact=a+b*x+c*x**2 #simulate noisy data m=200 X=1-2*np.random.rand(m) Y=a+b*X+c*X**2+np.random.randn(m) #linear least square fit A=np.vstack([X**0,X**1,X**2]) sol,r,rank,sv=la.lstsq(A.T,Y) y_fit=sol[0]+sol[1]*x+sol[2]*x**2 fig,ax=plt.subplots(figsize=(12,4)) ax.plot(X,Y,'go',alpha=0.5,label='simulated data') ax.plot(x,y_exact,'k',lw=2,label='true value $y=1+2x+3x^2$') ax.plot(x,y_fit,'b',lw=2,label='least square fit') #1st order polynomial A=np.vstack([X**n\u2026","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":926,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/06\/166-solution-of-a-differential-equation-using-bubnov-galerkin-method-with-sympy-package\/","url_meta":{"origin":1407,"position":2},"title":"#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package","author":"gantovnik","date":"2021-06-15","format":false,"excerpt":"#166 Solution of a differential equation using Bubnov-Galerkin method with Sympy package The problem and solution in this pdf file: ex166 [code language=\"python\"] import sympy from matplotlib import pyplot as plt import seaborn as sns import numpy as np from sympy.utilities.lambdify import lambdify from sympy import simplify from sympy import\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":217,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-b\/","url_meta":{"origin":1407,"position":3},"title":"#45  2nd-order Runge-Kutta type B using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex45') os.getcwd() #2nd-order Runge-Kutta methods with A=0 (type B) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2B(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x = x_range[0]\u2026","rel":"","context":"In &quot;differential equations&quot;","block_context":{"text":"differential equations","link":"https:\/\/gantovnik.com\/bio-tips\/category\/differential-equations\/"},"img":{"alt_text":"example45","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example45.png?resize=525%2C300 1.5x"},"classes":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":1407,"position":4},"title":"#44 2nd-order Runge-Kutta type A using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex44') os.getcwd() #2nd-order Runge-Kutta methods with A=1\/2 (type A) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2A(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x\u2026","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example44","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":220,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-c\/","url_meta":{"origin":1407,"position":5},"title":"#46 2nd-order Runge-Kutta type C using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\projects\\wordpress\\ex46') os.getcwd() #2nd-order Runge-Kutta methods with A=1\/3 (type C) # dy\/dx=exp(-2x)-2y # y(0)=0.1, interval x=[0,2], step size = h=0.2 def feval(funcName, *args): return eval(funcName)(*args) def RK2C(func, yinit, x_range, h): m = len(yinit) n = int((x_range[-1] - x_range[0])\/h) x = x_range[0]\u2026","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example46","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example46.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1407","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=1407"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1407\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}