{"id":308,"date":"2019-01-22T16:49:00","date_gmt":"2019-01-23T00:49:00","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=308"},"modified":"2024-12-17T09:38:46","modified_gmt":"2024-12-17T17:38:46","slug":"elementor-308","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/elementor-308\/","title":{"rendered":"# 54 Nonlinear Least Squares Fitting"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\" size-full wp-image-333 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=600%2C400&#038;ssl=1\" alt=\"example54\" width=\"600\" height=\"400\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<pre class=\"lang:python decode:true\">\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nos.chdir(r'D:\\\\data\\scripts\\wordpress\\ex54')\nos.getcwd()\n\nA,freq,tau=10,4,0.5\n\ndef f(t,A,freq,tau):\n  return A*np.exp(-t\/tau)*np.cos(2*np.pi*freq*t)\n\ntmax,dt=1,0.01\nt=np.arange(0,tmax,dt)\nyexact=f(t,A,freq,tau)\ny=yexact + np.random.randn(len(yexact))*2\nplt.plot(t,yexact)\nplt.scatter(t,y,marker='o',facecolors='blue', edgecolors='black')\n\ndef residuals(p,y,t):\n  A,freq,tau=p\n  return y-f(t,A,freq,tau)\n\nfrom scipy.optimize import leastsq\np0=5,5,1\nplsq=leastsq(residuals,p0,args=(y,t))\nplsq[0][1]\npfit = (plsq[0][0],plsq[0][1],plsq[0][2])\n\nplt.plot(t,y,'o',c='k',label='Data')\nplt.plot(t,yexact,c='green',label='Exact')\nplt.plot(t,f(t,plsq[0][0],plsq[0][1],plsq[0][2]),c='red',label='Fit')\nplt.legend()\n\nplt.tight_layout()\nplt.savefig(\"ex54.png\", dpi=100)\nplt.show()\nplt.close()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import numpy as np import matplotlib.pyplot as plt os.chdir(r&#8217;D:\\\\data\\scripts\\wordpress\\ex54&#8242;) os.getcwd() A,freq,tau=10,4,0.5 def f(t,A,freq,tau): return A*np.exp(-t\/tau)*np.cos(2*np.pi*freq*t) tmax,dt=1,0.01 t=np.arange(0,tmax,dt) yexact=f(t,A,freq,tau) y=yexact + np.random.randn(len(yexact))*2 plt.plot(t,yexact) plt.scatter(t,y,marker=&#8217;o&#8217;,facecolors=&#8217;blue&#8217;, edgecolors=&#8217;black&#8217;) def residuals(p,y,t): A,freq,tau=p return y-f(t,A,freq,tau) from scipy.optimize import leastsq p0=5,5,1 plsq=leastsq(residuals,p0,args=(y,t)) plsq[0][1] pfit = (plsq[0][0],plsq[0][1],plsq[0][2]) plt.plot(t,y,&#8217;o&#8217;,c=&#8217;k&#8217;,label=&#8217;Data&#8217;) plt.plot(t,yexact,c=&#8217;green&#8217;,label=&#8217;Exact&#8217;) plt.plot(t,f(t,plsq[0][0],plsq[0][1],plsq[0][2]),c=&#8217;red&#8217;,label=&#8217;Fit&#8217;) plt.legend() plt.tight_layout() plt.savefig(&#8220;ex54.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":[318],"class_list":["post-308","post","type-post","status-publish","format-standard","hentry","category-python","tag-fitting"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-4Y","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2053,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/406-nonlinear-least-squares-fitting-using-leastsq-from-scipy-optimize-in-python\/","url_meta":{"origin":308,"position":0},"title":"#406 Nonlinear least squares fitting using leastsq from scipy.optimize in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":318,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/weighted-and-unweighted-least-squares-fitting\/","url_meta":{"origin":308,"position":1},"title":"Weighted and unweighted least squares fitting","author":"gantovnik","date":"2019-01-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\/2019\/01\/example55.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example55.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example55.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":220,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-c\/","url_meta":{"origin":308,"position":2},"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":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":308,"position":3},"title":"#44 2nd-order Runge-Kutta type A using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"\u00a0","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":217,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-b\/","url_meta":{"origin":308,"position":4},"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":2865,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/439-relative-accelerometer-displacement-during-sine-vibration\/","url_meta":{"origin":308,"position":5},"title":"#439 Relative Accelerometer Displacement During Sine Vibration","author":"gantovnik","date":"2024-07-20","format":false,"excerpt":"\"If you want to find the secrets of the universe, think in terms of energy, frequency, and vibration.\" (Nikola Tesla) import math import numpy as np import matplotlib.pyplot as plt from scipy.optimize import fmin pi=math.pi f=6.0 # f, Hz T=1\/f #T, s omega=2.0*pi*f # Hz g=386.100 # in\/s^2 def func_a(angle,a0,phase):\u2026","rel":"","context":"In &quot;engineering&quot;","block_context":{"text":"engineering","link":"https:\/\/gantovnik.com\/bio-tips\/category\/engineering\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex439_abstract.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex439_abstract.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex439_abstract.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex439_abstract.png?resize=700%2C400&ssl=1 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/308","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=308"}],"version-history":[{"count":4,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"predecessor-version":[{"id":6770,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/308\/revisions\/6770"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}