{"id":1112,"date":"2021-11-19T02:00:05","date_gmt":"2021-11-19T10:00:05","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1112"},"modified":"2021-11-19T02:00:05","modified_gmt":"2021-11-19T10:00:05","slug":"194-solving-of-system-of-linear-equations-in-python-gauss-seidel-method","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2021\/11\/194-solving-of-system-of-linear-equations-in-python-gauss-seidel-method\/","title":{"rendered":"#194 Solving of system of linear equations in python (Gauss-Seidel method)"},"content":{"rendered":"<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport numpy as np\r\n# solve system of linear equation using \r\n# Gauss-Seidel method using\r\n# a predefined threshold eps=0.01\r\n#  8*x1+3*x2-3*x3=14\r\n# -2*x1-8*x2+5*x3=5\r\n#  3*x1+5*x2+10*x3=-8\r\n# first check if the coefficient matrix is \r\n# diagonally dominant or not.\r\na = &#x5B;&#x5B;8, 3, -3], &#x5B;-2, -8, 5], &#x5B;3, 5, 10]]\r\n# Find diagonal coefficients\r\ndiag = np.diag(np.abs(a))\r\n# Find row sum without diagonal\r\noff_diag = np.sum(np.abs(a), axis=1) - diag\r\nif np.all(diag &gt; off_diag):\r\n    print(&quot;matrix is diagonally dominant&quot;)\r\nelse:\r\n    print(&quot;NOT diagonally dominant&quot;)\r\n\r\nx1 = 0.0\r\nx2 = 0.0\r\nx3 = 0.0\r\nepsilon = 0.01\r\nconverged = False\r\nx_old = np.array(&#x5B;x1, x2, x3])\r\nprint(&quot;Iteration results&quot;)\r\nprint(&quot;k,   x1,      x2,     x3 &quot;)\r\nfor k in range(1, 50):\r\n    x1 = (14-3*x2+3*x3)\/8\r\n    x2 = (5+2*x1-5*x3)\/(-8)\r\n    x3 = (-8-3*x1-5*x2)\/(-5)\r\n    x = np.array(&#x5B;x1, x2, x3])\r\n    # check if it is smaller than threshold\r\n    dx = np.sqrt(np.dot(x-x_old, x-x_old))\r\n    print(&quot;%d, %.4f, %.4f, %.4f&quot;%(k, x1, x2, x3))\r\n    if dx &lt; epsilon:\r\n        converged = True\r\n        print(&quot;Converged!&quot;)\r\n        break\r\n    # assign the latest x value to the old value\r\n    x_old = x\r\n    if not converged:\r\n        print(&quot;Not converged, increase the # of iterations&quot;)\r\n<\/pre>\n<p>Result:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nmatrix is diagonally dominant\r\nIteration results\r\nk,   x1,      x2,     x3 \r\n1, 1.7500, -1.0625, 1.5875\r\nNot converged, increase the # of iterations\r\n2, 2.7437, -0.3188, 2.9275\r\nNot converged, increase the # of iterations\r\n3, 2.9673, 0.4629, 3.8433\r\nNot converged, increase the # of iterations\r\n4, 3.0177, 1.0226, 4.4332\r\nNot converged, increase the # of iterations\r\n5, 3.0290, 1.3885, 4.8059\r\nNot converged, increase the # of iterations\r\n6, 3.0315, 1.6208, 5.0397\r\nNot converged, increase the # of iterations\r\n7, 3.0321, 1.7668, 5.1861\r\nNot converged, increase the # of iterations\r\n8, 3.0322, 1.8582, 5.2776\r\nNot converged, increase the # of iterations\r\n9, 3.0322, 1.9154, 5.3348\r\nNot converged, increase the # of iterations\r\n10, 3.0323, 1.9512, 5.3705\r\nNot converged, increase the # of iterations\r\n11, 3.0323, 1.9735, 5.3929\r\nNot converged, increase the # of iterations\r\n12, 3.0323, 1.9875, 5.4068\r\nNot converged, increase the # of iterations\r\n13, 3.0323, 1.9962, 5.4156\r\nNot converged, increase the # of iterations\r\n14, 3.0323, 2.0017, 5.4210\r\nConverged!\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np # solve system of linear equation using # Gauss-Seidel method using # a predefined threshold eps=0.01 # 8*x1+3*x2-3*x3=14 # -2*x1-8*x2+5*x3=5 # 3*x1+5*x2+10*x3=-8 # first check if the coefficient matrix is # diagonally dominant or not. a = &#x5B;&#x5B;8, 3, -3], &#x5B;-2, -8, 5], &#x5B;3, 5, 10]] # Find diagonal coefficients diag [&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_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-1112","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-hW","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1984,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/12\/398-find-points-of-intersection-of-two-circles\/","url_meta":{"origin":1112,"position":0},"title":"#398 Find points of intersection of two circles","author":"gantovnik","date":"2023-12-26","format":false,"excerpt":"- how to find an equation of the common chord of two intersected circles. [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import math def get_intersections(x0, y0, r0, x1, y1, r1): # circle 1: (x0, y0), radius r0 # circle 2: (x1, y1), radius r1 d=math.sqrt((x1-x0)**2 + (y1-y0)**2)\u2026","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\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/12\/ex398.png?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"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":1112,"position":1},"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":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":1112,"position":2},"title":"#329 Golden section method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py [code language=\"python\"] import math as mt def golden(f,a,b,tol=1.0e-10): # Golden section method for determining x # that minimizes the scalar function f(x). # The minimum must be bracketed in (a,b). c1 = (mt.sqrt(5.0)-1.0)\/2.0 c2 = 1.0 - c1 nIt = int(mt.ceil(mt.log(tol\/abs(a-b))\/mt.log(c1))) # first step x1 = c1*a + c2*b\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\/ex329.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":104,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/unconstrained-multivariate-optimization\/","url_meta":{"origin":1112,"position":3},"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":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":1112,"position":4},"title":"#330 Gradient method using python","author":"gantovnik","date":"2023-01-03","format":false,"excerpt":"golden.py [code language=\"python\"] import math as mt def golden(f,a,b,tol=1.0e-10): # Golden section method for determining x # that minimizes the scalar function f(x). # The minimum must be bracketed in (a,b). c1 = (mt.sqrt(5.0)-1.0)\/2.0 c2 = 1.0 - c1 nIt = int(mt.ceil(mt.log(tol\/abs(a-b))\/mt.log(c1))) # first step x1 = c1*a + c2*b\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\/ex330.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1695,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-6\/","url_meta":{"origin":1112,"position":5},"title":"#321 unittest &#8211; unit testing framework in python","author":"gantovnik","date":"2022-11-30","format":false,"excerpt":"calculator.py [code language=\"python\"] #A simple calculator class Calculator: #empty constructor def __init__(self): pass #add method - given two numbers, return the addition def add(self, x1, x2): return x1 + x2 #multiply method - given two numbers, return the #multiplication of the two def multiply(self, x1, x2): return x1 * x2\u2026","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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1112","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=1112"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1112\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}