{"id":370,"date":"2019-01-29T16:17:36","date_gmt":"2019-01-30T00:17:36","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=370"},"modified":"2024-07-11T02:56:22","modified_gmt":"2024-07-11T09:56:22","slug":"roots-of-equation","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/roots-of-equation\/","title":{"rendered":"#52 Roots of equation using python"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=600%2C400&#038;ssl=1\" alt=\"example62\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-371\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.optimize import brentq\nos.chdir(r&amp;#039;D:\\projects\\wordpress\\ex52&amp;#039;)\nos.getcwd()\nf=lambda x: 0.2+x * np.cos(3\/x)\nx=np.linspace(-1,1,1000)\nplt.plot(x,f(x))\nplt.axhline(0,color=&amp;#039;k&amp;#039;)\nxstar=brentq(f,-0.7,-0.5)\nprint(xstar,f(xstar))\nplt.plot(xstar,f(xstar), &amp;#039;ro&amp;#039;)&lt;br \/&gt;\nplt.text(-1.0, -0.5, &amp;#039;x=%6.4f and f(x)=%6.4f&amp;#039; % (xstar, f(xstar)))\nplt.tight_layout()\nplt.savefig(&amp;quot;example52.png&amp;quot;, dpi=300)\nplt.show()\nplt.close()\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import brentq os.chdir(r&amp;#039;D:\\projects\\wordpress\\ex52&amp;#039;) os.getcwd() f=lambda x: 0.2+x * np.cos(3\/x) x=np.linspace(-1,1,1000) plt.plot(x,f(x)) plt.axhline(0,color=&amp;#039;k&amp;#039;) xstar=brentq(f,-0.7,-0.5) print(xstar,f(xstar)) plt.plot(xstar,f(xstar), &amp;#039;ro&amp;#039;)&lt;br \/&gt; plt.text(-1.0, -0.5, &amp;#039;x=%6.4f and f(x)=%6.4f&amp;#039; % (xstar, f(xstar))) plt.tight_layout() plt.savefig(&amp;quot;example52.png&amp;quot;, dpi=300) 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":[3],"class_list":["post-370","post","type-post","status-publish","format-standard","hentry","category-python","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-5Y","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":370,"position":0},"title":"#335 Solution of nonlinear equation by Brent&#8217;s method in python","author":"gantovnik","date":"2023-01-06","format":false,"excerpt":"Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method, and inverse quadratic interpolation. ex335.py [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as optimize def f(x): return 2*x - 1 + 2*np.cos(np.pi*x) x = np.linspace(0.0,2.0,201) y=f(x) plt.plot(x,y,label='$2x - 1 + 2\\\\cos(\\\\pi\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex335.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":318,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/weighted-and-unweighted-least-squares-fitting\/","url_meta":{"origin":370,"position":1},"title":"Weighted and unweighted least squares fitting","author":"gantovnik","date":"2019-01-22","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit os.chdir(r'D:\\data\\scripts\\wordpress\\ex55') os.getcwd() x0,A,gamma=12,3,5 n=200 x=np.linspace(1,20,n) yexact=A*gamma**2\/(gamma**2+(x-x0)**2) #Learning Scientific Programming with Python # Add some noise with a sigma of 0.5 apart from a particularly noisy region # near x0 where sigma is 3 sigma=np.ones(n)*0.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\/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":3022,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/","url_meta":{"origin":370,"position":2},"title":"#442 The partial sums of the Fourier series for the square-wave function using python","author":"gantovnik","date":"2024-07-25","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider nmax = 5 pi = np.pi x = np.linspace(-2*pi, 2*pi, 1001) def f(xarray): y = np.zeros_like(xarray) for ind, x in enumerate(xarray): xmod = x%(2*pi) if xmod<pi: y[ind] = 1 if x%pi==0: y[ind]= np.nan return y def Fourier(x, nmax):\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\/07\/ex442_3.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1786,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/","url_meta":{"origin":370,"position":3},"title":"#340 Lorenz attractors using python","author":"gantovnik","date":"2023-01-17","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt from numpy import linspace from scipy.integrate import odeint os.chdir(r'D:\\projects\\wordpress\\ex340') os.getcwd() sigma=10.0 b=8\/3.0 r=28.0 f = lambda x,t: [sigma*(x[1]-x[0]), r*x[0]-x[1]-x[0]*x[2], x[0]*x[1]-b*x[2]] t=linspace(0,20,2000) y0=[5.0,5.0,5.0] solution=odeint(f,y0,t) X=solution[:,0]; Y=solution[:,1]; Z=solution[:,2] plt.subplot(projection='3d') plt.plot(X,Y,Z) plt.xlabel('x') plt.ylabel('y') plt.savefig(\"ex340_a.png\", dpi=100) plt.show() plt.rcParams['figure.figsize'] = (10.0, 5.0) plt.subplot(121) plt.plot(t,Z) plt.xlabel('t') plt.ylabel('Z') plt.subplot(122)\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\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":894,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/160-max-and-min-using-argmax-and-argmax-in-python\/","url_meta":{"origin":370,"position":4},"title":"#160 Max and min using argmax and argmax in python","author":"gantovnik","date":"2021-02-14","format":false,"excerpt":"#160 Max and min using argmax and argmax in python [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import os os.chdir(r'D:\\projects\\wordpress\\ex160') os.getcwd() def main(): N = 100 L = 1 def f(i, n): x = i * L \/ N lam = 2 * L \/ (n+1) return\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\/02\/example160.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example160.png?resize=525%2C300 1.5x"},"classes":[]},{"id":260,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/interpolation\/","url_meta":{"origin":370,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/370","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=370"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":2254,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/370\/revisions\/2254"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}