{"id":107,"date":"2018-12-31T09:50:47","date_gmt":"2018-12-31T09:50:47","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=107"},"modified":"2018-12-31T09:50:47","modified_gmt":"2018-12-31T09:50:47","slug":"nonlinear-least-square-fit","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/nonlinear-least-square-fit\/","title":{"rendered":"Nonlinear least square fit"},"content":{"rendered":"<pre>import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport scipy\nos.chdir('\/home\/vg\/Downloads\/projects\/ex19')\nos.getcwd()\nbeta=(0.25,0.75,0.5)\ndef f(x,b0,b1,b2):\n    return b0+b1*np.exp(-b2*x**2)\n\nxdata=np.linspace(0,5,50)\ny=f(xdata,*beta)\nydata=y+0.05*np.random.randn(len(xdata))\ndef g(beta):\n    return ydata-f(xdata,*beta)\n\nbeta_start=(1,1,1)\nbeta_opt,beta_cov=scipy.optimize.leastsq(g,beta_start)\nprint(beta_opt)\nfig,ax=plt.subplots()\nax.scatter(xdata,ydata,label='samples')\nax.plot(xdata,y,'r',lw=2,label='true model')\nax.plot(xdata,f(xdata,*beta_opt),'b',lw=2,label='fitted model')\nax.set_xlim(0,5)\nax.set_xlabel(r\"$x$\",fontsize=18)\nax.set_ylabel(r\"$f(x,\\beta)$\",fontsize=18)\nax.legend()\nax.set_title(\"Nonlinear least square fitting\")\nplt.savefig(\"example19.png\", dpi=100)\nplt.show()\nplt.close()\nbeta_opt,beta_cov=scipy.optimize.curve_fit(f,xdata,ydata)\nprint(beta_opt)\n\n<img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-108 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=548%2C365\" alt=\"example19\" width=\"548\" height=\"365\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example19.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import scipy os.chdir(&#8216;\/home\/vg\/Downloads\/projects\/ex19&#8242;) 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=&#8217;samples&#8217;) ax.plot(xdata,y,&#8217;r&#8217;,lw=2,label=&#8217;true model&#8217;) ax.plot(xdata,f(xdata,*beta_opt),&#8217;b&#8217;,lw=2,label=&#8217;fitted model&#8217;) ax.set_xlim(0,5) ax.set_xlabel(r&#8221;$x$&#8221;,fontsize=18) ax.set_ylabel(r&#8221;$f(x,\\beta)$&#8221;,fontsize=18) ax.legend() ax.set_title(&#8220;Nonlinear least square fitting&#8221;) plt.savefig(&#8220;example19.png&#8221;, dpi=100) plt.show() plt.close() beta_opt,beta_cov=scipy.optimize.curve_fit(f,xdata,ydata) print(beta_opt) &nbsp;<\/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-107","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-1J","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":139,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/multidimensional-spline\/","url_meta":{"origin":107,"position":0},"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":1363,"url":"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\/","url_meta":{"origin":107,"position":1},"title":"#259 Interpolation in python","author":"gantovnik","date":"2022-01-11","format":false,"excerpt":"Output:","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\/01\/ex259.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/01\/ex259.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2099,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/410-2d-random-walk-animation-in-python\/","url_meta":{"origin":107,"position":2},"title":"#410 2D random walk animation in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"","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":2175,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/05\/425-an-animation-of-a-bouncing-ball-using-python\/","url_meta":{"origin":107,"position":3},"title":"#425 An animation of a bouncing ball using python","author":"gantovnik","date":"2024-05-05","format":false,"excerpt":"","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":107,"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":169,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/lorenz-equations\/","url_meta":{"origin":107,"position":5},"title":"Lorenz equations","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate from mpl_toolkits.mplot3d.axes3d import Axes3D os.chdir(r'D:\\projects\\wordpress\\ex36') os.getcwd() def f(xyz, t, rho, sigma, beta): x, y, z = xyz return [sigma*(y-x),x*(rho-z)-y,x*y-beta*z] rho = 28 sigma = 8 beta = 8\/3.0 t = np.linspace(0, 25, 10000) xyz0 = [1.0,\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\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example36.png?fit=1200%2C514&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/107","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=107"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}