{"id":1770,"date":"2023-01-15T18:02:57","date_gmt":"2023-01-16T02:02:57","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1770"},"modified":"2023-01-15T18:02:57","modified_gmt":"2023-01-16T02:02:57","slug":"337-minimization-on-an-2d-quadratic-problem-using-the-gradient-descent-method-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/337-minimization-on-an-2d-quadratic-problem-using-the-gradient-descent-method-in-python\/","title":{"rendered":"#337 Minimization on an 2D quadratic problem using the gradient descent method in python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/337-minimization-on-an-2d-quadratic-problem-using-the-gradient-descent-method-in-python\/ex337\/\" rel=\"attachment wp-att-1771\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex337.png?resize=600%2C400&#038;ssl=1\" alt=\"\" width=\"600\" height=\"400\" class=\"alignnone size-full wp-image-1771\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex337.png 600w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex337-480x320.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 600px, 100vw\" \/><\/a><\/p>\n<p>ex337.py<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\nimport matplotlib.pyplot as plt\r\n\r\ndef get_next_point(a0, b0, gamma):\r\n    # Get the next point $(a_1, b_1)$ starting from the current point $(a_0,b_0)$\r\n    # using exact line search. The close-form of $t_{exact}$ is \r\n    # $\\dfrac{a_0^2+\\gamma^2 b_0^2}{a_0^2+\\gamma^3 b_0^2}$.\r\n    r = gamma\r\n    t = (a0**2 + r**2 * b0**2) \/ (a0**2 + r**3 * b0**2)\r\n    a1 = (1 - t)*a0\r\n    b1 = (1 - t*r)*b0\r\n    return a1, b1\r\n\r\ndef plot_quadratic_convergence():\r\n    # plot the convergence of $x$ during the minimization on a quadratic \r\n    # objective function $f(x_1, x_2)=\\frac{1}{2}(x_1^2+\\gamma x_2^2)$,\r\n    # where $\\gamma=10$, starting from $x_0=(\\gamma, 3)$\r\n    gamma = 10\r\n    a0 = gamma\r\n    b0 = 3\r\n    num_iteration = 100\r\n    x = np.zeros((num_iteration, 1))\r\n    y = np.zeros((num_iteration, 1))\r\n    for iteration in range(num_iteration):\r\n        print(&quot;({}, {})&quot;.format(a0, b0))\r\n        x&#x5B;iteration] = a0\r\n        y&#x5B;iteration] = b0\r\n        a1, b1 = get_next_point(a0, b0, gamma)\r\n        a0, b0 = a1, b1\r\n    fig, ax = plt.subplots()\r\n    ax.set_xlim(&#x5B;-gamma * 1, gamma * 1])\r\n    ax.set_ylim(&#x5B;-1 * 4, 1 * 4])\r\n    ax.plot(x, y, marker=&quot;o&quot;,markerfacecolor='red',\r\n                markeredgecolor='red',\r\n                markeredgewidth=2)\r\n    delta = gamma * 1.0 \/ 100\r\n    x = np.arange(-gamma * 1, gamma * 1, delta)\r\n    y = np.arange(-4.0, 4.0, delta)\r\n    X, Y = np.meshgrid(x, y)\r\n    Z = 0.5 * (X ** 2 + gamma * Y ** 2)\r\n    levels = np.exp(np.arange(-2, 10, 0.25))\r\n    CS = ax.contour(X, Y, Z, levels=levels, vmin=0,vmax=100)\r\n    ax.set_aspect(aspect='2')\r\n    ax.clabel(CS, fontsize=6, inline=True)\r\n    ax.set_xlabel(r'$x_1$')\r\n    ax.set_ylabel(r'$x_2$')\r\n    ax.set_title(r'$f(x)=\\frac{1}{2}\\left(x_1^2+\\gamma x_2^2\\right),\\gamma=10,x^{0}=(\\gamma, 3)$')\r\n    plt.savefig('ex337.png', dpi=100)\r\n    plt.show()        \r\n    \r\nif __name__ == &quot;__main__&quot;:\r\n    plot_quadratic_convergence() \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ex337.py import numpy as np import matplotlib.pyplot as plt def get_next_point(a0, b0, gamma): # Get the next point $(a_1, b_1)$ starting from the current point $(a_0,b_0)$ # using exact line search. The close-form of $t_{exact}$ is # $\\dfrac{a_0^2+\\gamma^2 b_0^2}{a_0^2+\\gamma^3 b_0^2}$. r = gamma t = (a0**2 + r**2 * b0**2) \/ (a0**2 + r**3 * [&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":[9,2],"tags":[25,3],"class_list":["post-1770","post","type-post","status-publish","format-standard","hentry","category-optimization","category-python","tag-optimization","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-sy","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":1770,"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 [code language=\"python\"] import numpy as np import math import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') def my_f(x): #$f(x_1, x_2) = e^{x_1+3x_2-0.1}+e^{x_1-3x_2-0.1}+e^{-x_1-0.1}$ x1 = x[0, 0] x2 = x[1, 0] return math.exp(x1+3*x2-0.1)+math.exp(x1-3*x2-0.1)+math.exp(-x1-0.1) def my_gradient_f(x): #$\\nabla f(x_1, x_2)$ x1 = x[0, 0] x2 = x[1, 0] gradient_1=1*math.exp(x1+3*x2-0.1)+1*math.exp(x1-3*x2-0.1)-math.exp(-x1-0.1) gradient_2=3*math.exp(x1+3*x2-0.1)-3*math.exp(x1-3*x2-0.1) return np.array([[gradient_1], [gradient_2]])\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\/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":148,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/damped-harmonic-oscillator\/","url_meta":{"origin":1770,"position":1},"title":"Damped harmonic oscillator","author":"gantovnik","date":"2019-01-06","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl mpl.rcParams['text.usetex'] = True import sympy from IPython.display import display sympy.init_printing() def apply_ics(sol, ics, x, known_params): free_params = sol.free_symbols - set(known_params) eqs = [(sol.lhs.diff(x, n) - sol.rhs.diff(x, n)).subs(x, 0).subs(ics) for n in range(len(ics))] sol_params = sympy.solve(eqs, free_params)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example31","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example31.png?resize=525%2C300 1.5x"},"classes":[]},{"id":318,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/weighted-and-unweighted-least-squares-fitting\/","url_meta":{"origin":1770,"position":2},"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":495,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/05\/optimization-with-constraints\/","url_meta":{"origin":1770,"position":3},"title":"#72 Optimization with constraints using scipy in python","author":"gantovnik","date":"2020-05-03","format":false,"excerpt":"[code language=\"python\"] import os from scipy.optimize import minimize import matplotlib.pyplot as plt import numpy as np os.chdir(r'D:\\projects\\wordpress\\ex72') os.getcwd() def f(X): x, y = X return (x - 1)2 + (y - 1)2 def func_X_Y_to_XY(f, X, Y): #Wrapper for f(X, Y) -> f([X, Y]) s = np.shape(X) return f(np.vstack([X.ravel(), Y.ravel()])).reshape(*s) x_opt\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"ex72","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2020\/05\/ex72.png?resize=525%2C300 1.5x"},"classes":[]},{"id":104,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","url_meta":{"origin":1770,"position":4},"title":"Unconstrained multivariate optimization","author":"gantovnik","date":"2018-12-31","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np import scipy import sympy os.chdir('\/home\/vg\/Downloads\/projects\/ex18') os.getcwd() x1,x2=sympy.symbols(\"x_1,x_2\") f_sym=(x1-1)**4 + 5*(x2-1)**2 - 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,'numpy') fprime_lmbda=sympy.lambdify((x1,x2),fprime_sym,'numpy') fhess_lmbda=sympy.lambdify((x1,x2),fhess_sym,'numpy') 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)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example18","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/example18.png?resize=525%2C300 1.5x"},"classes":[]},{"id":187,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/coupled-damped-springs\/","url_meta":{"origin":1770,"position":5},"title":"Coupled damped springs","author":"gantovnik","date":"2019-01-09","format":false,"excerpt":"\u00a0 import os import numpy as np import matplotlib.pyplot as plt from scipy import integrate os.chdir(r'D:\\projects\\wordpress\\ex37') os.getcwd() def f(t, y, args): m1, k1, g1, m2, k2, g2 = args return [y[1], - k1\/m1 * y[0] + k2\/m1 * (y[2] - y[0]) - g1\/m1 * y[1], y[3], - k2\/m2 * (y[2]\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"example37","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example37.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1770","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=1770"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1770\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1770"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}