{"id":1153,"date":"2021-11-23T14:07:50","date_gmt":"2021-11-23T22:07:50","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1153"},"modified":"2021-11-23T14:59:50","modified_gmt":"2021-11-23T22:59:50","slug":"201-polyfit-and-polyval-2-2","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/201-polyfit-and-polyval-2-2\/","title":{"rendered":"#203 Find minimum of Rosenbrock function using scipy.optimize"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\nimport scipy.optimize as so\r\nrosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2\r\nX,Y = np.meshgrid(np.linspace(-.5,1.5,100), np.linspace(-1.5,3.,100))\r\nZ = rosenbrockfunction(X,Y)\r\nplt.figure(figsize=(4,3), dpi=72)\r\ncs=plt.contour(X,Y,Z,np.logspace(-0.5,3.5,20,base=10),cmap='gray',linewidths=1.0)\r\nrosen=lambda x: rosenbrockfunction(x&#x5B;0],x&#x5B;1])\r\nsolution, iterates = so.fmin_powell(rosen,x0=np.array(&#x5B;0,-0.7]),retall=True)\r\nx,y=zip(*iterates)\r\nplt.plot(x,y,'.',c=&quot;red&quot;) # plot black bullets\r\nplt.plot(x,y,':',c=&quot;red&quot;,linewidth=1.5) # plot black dotted lines\r\nplt.clabel(cs)\r\nplt.savefig('ex203.png')\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\/2021\/11\/ex203.png?resize=288%2C216&#038;ssl=1\" alt=\"\" width=\"288\" height=\"216\" class=\"alignnone size-full wp-image-1173\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex203.png?w=288&amp;ssl=1 288w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/11\/ex203.png?resize=150%2C113&amp;ssl=1 150w\" sizes=\"(max-width: 288px) 100vw, 288px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt import scipy.optimize as so rosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2 X,Y = np.meshgrid(np.linspace(-.5,1.5,100), np.linspace(-1.5,3.,100)) Z = rosenbrockfunction(X,Y) plt.figure(figsize=(4,3), dpi=72) cs=plt.contour(X,Y,Z,np.logspace(-0.5,3.5,20,base=10),cmap=&#8217;gray&#8217;,linewidths=1.0) rosen=lambda x: rosenbrockfunction(x&#x5B;0],x&#x5B;1]) solution, iterates = so.fmin_powell(rosen,x0=np.array(&#x5B;0,-0.7]),retall=True) x,y=zip(*iterates) plt.plot(x,y,&#8217;.&#8217;,c=&quot;red&quot;) # plot black bullets plt.plot(x,y,&#8217;:&#8217;,c=&quot;red&quot;,linewidth=1.5) # plot black dotted lines plt.clabel(cs) plt.savefig(&#8216;ex203.png&#8217;) plt.show()<\/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-1153","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-iB","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":370,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/roots-of-equation\/","url_meta":{"origin":1153,"position":0},"title":"#52 Roots of equation using python","author":"gantovnik","date":"2019-01-29","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import brentq os.chdir(r'D:\\projects\\wordpress\\ex52') 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='k') xstar=brentq(f,-0.7,-0.5) print(xstar,f(xstar)) plt.plot(xstar,f(xstar), 'ro') plt.text(-1.0, -0.5, 'x=%6.4f and f(x)=%6.4f' % (xstar, f(xstar))) plt.tight_layout() plt.savefig(\"example52.png\", dpi=300) 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":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1718,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","url_meta":{"origin":1153,"position":1},"title":"#325 Finding the intersection points between two functions using python","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"[code language=\"python\"] from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as plt # Defining function to simplify intersection solution def findIntersection(func1, func2, x0): return fsolve(lambda x : func1(x) - func2(x), x0) # Defining functions that will intersect funky = lambda x : np.cos(x \/ 5) * np.sin(x \/\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\/12\/ex325.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":1153,"position":2},"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":2865,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/439-relative-accelerometer-displacement-during-sine-vibration\/","url_meta":{"origin":1153,"position":3},"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":[]},{"id":318,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/weighted-and-unweighted-least-squares-fitting\/","url_meta":{"origin":1153,"position":4},"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":5,"url":"https:\/\/gantovnik.com\/bio-tips\/2016\/12\/example-1-interpolation\/","url_meta":{"origin":1153,"position":5},"title":"Example 1: Interpolation","author":"gantovnik","date":"2016-12-08","format":false,"excerpt":"Example 1: Interpolation [code language=\"python\"] #Example 1: Interpolation import numpy as np from scipy import interpolate import matplotlib.pyplot as plt import os os.chdir('C:\\\\Anaconda\\\\mycodes\\\\aerospace') os.getcwd() #create the data points and add noise as follows x = np.linspace(-18,18,36) noise = 0.1 * np.random.random(len(x)) signal = np.sinc(x) + noise #create a linear interpolation\u2026","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example1","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1153","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=1153"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1153\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}