Jungletronics
Published in

Jungletronics

Linear Equations — Solve by GAUSS SEIDEL

How To Use Python to Solve Linear System #PySeries#Episode 34

To solve a system of linear equations we going to use this methods:

GAUSS SEIDEL

In numerical linear algebra, the Gauss–Seidel method, also known as the Liebmann method or the method of successive displacement, is an iterative method used to solve a system of linear equations. (FROM https://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method)

In this task, we going to use GAUSS SEIDEL to solve the problem below.

WELCOME!

THE PROBLEM EQUATIONS SYSTEMS

A production engineer from an electronics industry analyzes an industrial process in which he considers the assembly time (t), the number of employees (x) and the number of components (n) used in the final product, and reached this system of equations.
Fig 1. Isolating the varibles

The system equation to solve in this methods: GAUSS SEIDEL

The engineer wants to reach an ideal condition for all goals to be achieved, with an estimated accuracy of 0.0000000001.

HOW TO SOLVE THIS PROBLEM USING GAUSS SEIDEL?

LET’S GET IT ON !!!

Click to goto colab’s notebook solution BELOW :)

01#Step — # Gauss seidel method initialization (importing libs):

from numpy import matrix, linalg
import numpy as np

02#Step —Defining equations:

print('Equations for iteration:')
print('t = (39 — x — n) / 2')
print('x = (13-(0.3 * t) — (0.25 * n))/ 2)')
print('n = (45 -t — x) / 2')
Equations for iteration:
t = (39 - x - n) / 2
x = (13-(0.3 * t) - (0.25 * n))/ 2)
n = (45 -t - x) / 2

03#Step —Initializing the variables:

t = 0.0
x = 0.0
n = 0.0
k = 0
matrix_X0 = np.array([[t],[x],[n]])
print('Inicial parameter: matrix_X0')
print(matrix_X0)
Inicial parameter: matrix_X0
[[0.]
[0.]
[0.]]

04#Step —The Solution: While method:

err_t = 1.0
err_x = 1.0
err_n = 1.0
while(err_t >= 0.0000000001 and err_x >= 0.0000000001 and err_n >= 0.0000000001):
k1 = k + 1
t1 = (39-x-n)/2
x1 = (13-(0.3*t1)-(0.25*n))/2
n1 = (45-t1-x1)/2

# updating the errors' values
err_t = abs((t1-t))
err_x = abs((x1-x))
err_n = abs((n1-n))

# updating variable's values
k=k1
t=t1
x=x1
n=n1

# each iteration resuts:
print('k=', k)
print("t={0} --> (err_t={1:.11f})".format(t,err_t))
print("x={0} --> (err_x={1:.11f})".format(x,err_x))
print("n={0} --> (err_n={1:.11f})".format(n,err_n))

print('_________________________________________________\n')




else:

print("The last iteration results: ", k)
matrix_S = np.array(([t],[x],[n]))
print('Solution: matrix_S ')
print(matrix_S)
k= 1
t=19.5 --> (err_t=19.50000000000)
x=3.575 --> (err_x=3.57500000000)
n=10.9625 --> (err_n=10.96250000000)
_________________________________________________

k= 2
t=12.23125 --> (err_t=7.26875000000)
x=3.295000000000001 --> (err_x=0.28000000000)
n=14.736874999999998 --> (err_n=3.77437500000)
_________________________________________________

k= 3
t=10.4840625 --> (err_t=1.74718750000)
x=3.0852812500000004 --> (err_x=0.20971875000)
n=15.715328125 --> (err_n=0.97845312500)
_________________________________________________

k= 4
t=10.0996953125 --> (err_t=0.38436718750)
x=3.0206296875000005 --> (err_x=0.06465156250)
n=15.939837499999998 --> (err_n=0.22450937500)
_________________________________________________

k= 5
t=10.019766406250003 --> (err_t=0.07992890625)
x=3.0045553515625 --> (err_x=0.01607433594)
n=15.98783912109375 --> (err_n=0.04800162109)
_________________________________________________

k= 6
t=10.003802763671874 --> (err_t=0.01596364258)
x=3.0009496953125003 --> (err_x=0.00360565625)
n=15.997623770507815 --> (err_n=0.00978464941)
_________________________________________________

k= 7
t=10.000713267089843 --> (err_t=0.00308949658)
x=3.000190038623047 --> (err_x=0.00075965669)
n=15.999548347143556 --> (err_n=0.00192457664)
_________________________________________________

k= 8
t=10.0001308071167 --> (err_t=0.00058245997)
x=3.0000368355395506 --> (err_x=0.00015320308)
n=15.999916178671876 --> (err_n=0.00036783153)
_________________________________________________

k= 9
t=10.000023492894286 --> (err_t=0.00010731422)
x=3.000006953731873 --> (err_x=0.00002988181)
n=15.99998477668692 --> (err_n=0.00006859802)
_________________________________________________

k= 10
t=10.000004134790604 --> (err_t=0.00001935810)
x=3.0000012826955444 --> (err_x=0.00000567104)
n=15.999997291256928 --> (err_n=0.00001251457)
_________________________________________________

k= 11
t=10.000000713023764 --> (err_t=0.00000342177)
x=3.0000002316393197 --> (err_x=0.00000105106)
n=15.999999527668457 --> (err_n=0.00000223641)
_________________________________________________

k= 12
t=10.000000120346112 --> (err_t=0.00000059268)
x=3.0000000409895264 --> (err_x=0.00000019065)
n=15.99999991933218 --> (err_n=0.00000039166)
_________________________________________________

k= 13
t=10.000000019839147 --> (err_t=0.00000010051)
x=3.0000000071076056 --> (err_x=0.00000003388)
n=15.999999986526623 --> (err_n=0.00000006719)
_________________________________________________

k= 14
t=10.000000003182883 --> (err_t=0.00000001666)
x=3.0000000012067396 --> (err_x=0.00000000590)
n=15.999999997805189 --> (err_n=0.00000001128)
_________________________________________________

k= 15
t=10.000000000494033 --> (err_t=0.00000000269)
x=3.000000000200246 --> (err_x=0.00000000101)
n=15.99999999965286 --> (err_n=0.00000000185)
_________________________________________________

k= 16
t=10.000000000073447 --> (err_t=0.00000000042)
x=3.0000000000323754 --> (err_x=0.00000000017)
n=15.999999999947088 --> (err_n=0.00000000029)
_________________________________________________

k= 17
t=10.000000000010271 --> (err_t=0.00000000006)
x=3.000000000005074 --> (err_x=0.00000000003)
n=15.999999999992326 --> (err_n=0.00000000005)
_________________________________________________

The last iteration results: 17
Solution: matrix_S
[[10.]
[ 3.]
[16.]]

That’s it:

print(“That’s it! Thanks for reading the post!”)That's it! Thanks for reading the post!

👉Colab notebook: link

Credits & References

Based on: Uninter Course Computer Engeneer Graduation — Date Aug/2021

Thanks to:

Fernanda Fonseca— Doctoral Student in Education, Master in Science and Mathematics Education and Degree in Physics and Philosophy — UNINTER — Brazil — PR

Related Posts:

32#PySeries#Episode — Linear Equations — GAUSS JORDAN — How To Use Python to Solve Linear System

33#PySeries#Episode — Linear Equations — CREMER RULE — How To Use Python to Solve Linear System

34#PySeries#Episode — Linear Equations — Linear Equations — GAUSS SEIDEL — How To Use Python to Solve Linear System (this one:)

Fig 1. The Secret of Gauss-siedel method: Iterations! (image from here)

Jungletronics

J of Jungle + 3 Plats Arduino/RPi/Pic = J3

Following

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
J3

J3

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!