{"id":104,"date":"2018-12-31T09:32:52","date_gmt":"2018-12-31T09:32:52","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=104"},"modified":"2018-12-31T09:32:52","modified_gmt":"2018-12-31T09:32:52","slug":"unconstrained-multivariate-optimization","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","title":{"rendered":"Unconstrained multivariate optimization"},"content":{"rendered":"<pre>import os\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport scipy\nimport sympy\nos.chdir('\/home\/vg\/Downloads\/projects\/ex18')\nos.getcwd()\nx1,x2=sympy.symbols(\"x_1,x_2\")\nf_sym=(x1-1)**4 + 5*(x2-1)**2 - 2*x1*x2\nfprime_sym=[f_sym.diff(x_) for x_ in (x1,x2)]\nsympy.Matrix(fprime_sym)\nfhess_sym=[[f_sym.diff(x1_,x2_) for x1_ in (x1,x2)] for x2_ in (x1,x2)]\nsympy.Matrix(fhess_sym)\nf_lmbda=sympy.lambdify((x1,x2),f_sym,'numpy')\nfprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,'numpy')\nfhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,'numpy')\ndef func_XY_to_xy(f):\n    return lambda X: np.array(f(X[0],X[1]))\n\nf=func_XY_to_xy(f_lmbda)\nfprime=func_XY_to_xy(fprime_lmbda)\nfhess=func_XY_to_xy(fhess_lmbda)\nx_opt=scipy.optimize.fmin_ncg(f,(0,0),fprime=fprime,fhess=fhess)\nprint(x_opt)\nfig,ax=plt.subplots(figsize=(6,4))\nx_=y_=np.linspace(-1,4,100)\nX,Y=np.meshgrid(x_,y_)\nc=ax.contour(X,Y,f_lmbda(X,Y),50)\nax.plot(x_opt[0],x_opt[1],'r*',markersize=15)\nax.set_xlabel(r\"$x_1$\",fontsize=18)\nax.set_ylabel(r\"$x_2$\",fontsize=18)\nplt.colorbar(c,ax=ax)\nplt.savefig(\"example18.png\", dpi=100)\nplt.show()\nplt.close()\nx_opt=scipy.optimize.fmin_bfgs(f,(0,0),fprime=fprime)\nprint(x_opt)\nx_opt=scipy.optimize.fmin_cg(f,(0,0),fprime=fprime)\nprint(x_opt)\nx_opt=scipy.optimize.fmin_bfgs(f,(0,0))\nprint(x_opt)\nx_opt=scipy.optimize.fmin_cg(f,(0,0))\nprint(x_opt)\n\n\n<img data-recalc-dims=\"1\" decoding=\"async\" class=\"  wp-image-105 aligncenter\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=536%2C357\" alt=\"example18\" width=\"536\" height=\"357\" srcset=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?w=600&amp;ssl=1 600w, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=300%2C200&amp;ssl=1 300w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import os import matplotlib.pyplot as plt import numpy as np import scipy import sympy os.chdir(&#8216;\/home\/vg\/Downloads\/projects\/ex18&#8242;) os.getcwd() x1,x2=sympy.symbols(&#8220;x_1,x_2&#8221;) f_sym=(x1-1)**4 + 5*(x2-1)**2 &#8211; 2*x1*x2 fprime_sym=[f_sym.diff(x_) for x_ in (x1,x2)] sympy.Matrix(fprime_sym) fhess_sym=[[f_sym.diff(x1_,x2_) for x1_ in (x1,x2)] for x2_ in (x1,x2)] sympy.Matrix(fhess_sym) f_lmbda=sympy.lambdify((x1,x2),f_sym,&#8217;numpy&#8217;) fprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,&#8217;numpy&#8217;) fhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,&#8217;numpy&#8217;) def func_XY_to_xy(f): return lambda X: np.array(f(X[0],X[1])) f=func_XY_to_xy(f_lmbda) fprime=func_XY_to_xy(fprime_lmbda) fhess=func_XY_to_xy(fhess_lmbda) x_opt=scipy.optimize.fmin_ncg(f,(0,0),fprime=fprime,fhess=fhess) print(x_opt) fig,ax=plt.subplots(figsize=(6,4)) x_=y_=np.linspace(-1,4,100) X,Y=np.meshgrid(x_,y_) [&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_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":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[],"class_list":["post-104","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-1G","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1774,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/338-minimization-using-newtons-method-on-the-non-quadratic-problem-in-python\/","url_meta":{"origin":104,"position":0},"title":"#338 Minimization using Newton\u2019s method on the non-quadratic problem in python","author":"gantovnik","date":"2023-01-16","format":false,"excerpt":"ex338.py","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\/ex338.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex338.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex338.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex338.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex338.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex338.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":1738,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2\/","url_meta":{"origin":104,"position":1},"title":"#329 Golden section method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py ex329.py","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\/ex329.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1742,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2-2-2\/","url_meta":{"origin":104,"position":2},"title":"#330 Gradient method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py grad.py ex330.py","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\/ex330.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":85,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/subplots-in-matplotlib\/","url_meta":{"origin":104,"position":3},"title":"#13 Subplots in matplotlib","author":"gantovnik","date":"2018-12-29","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example13","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example13.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1112,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/194-solving-of-system-of-linear-equations-in-python-gauss-seidel-method\/","url_meta":{"origin":104,"position":4},"title":"#194 Solving of system of linear equations in python (Gauss-Seidel method)","author":"gantovnik","date":"2021-11-19","format":false,"excerpt":"Result:","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":193,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/double-pendulum\/","url_meta":{"origin":104,"position":5},"title":"#39 Double pendulum using python","author":"gantovnik","date":"2019-01-10","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate import sympy os.chdir(r'D:\\projects\\wordpress\\ex39') os.getcwd() t, g, m1, l1, m2, l2 = sympy.symbols(\"t, g, m_1, l_1, m_2, l_2\") theta1, theta2 = sympy.symbols(\"theta_1, theta_2\", cls=sympy.Function) ode1 = sympy.Eq((m1+m2)*l1 * theta1(t).diff(t,t) + m2*l2 * theta2(t).diff(t,t) + m2*l2 * theta2(t).diff(t)**2\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example39","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example39.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/104","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=104"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}