# Python Program for Successive Over-Relaxation

In numerical analysis, specially in linear algebra, successive over-relaxation (SOR) method is a variant of the Gauss–Seidel method for solving a system of linear equations. This variant converges faster than Gauss-Seidel method.

#### This Python program implements successive over-relaxation (SOR) method to solve system of linear equations.

This program solves system of linear equation having 3 unknowns. You can modify this program to solve higher number of unknowns.

Following system of linear equations are used:

``` 3x - y + z = -1
-x + 3y - z = 7
x - y + 3z = -7
```

## Python Source Code

``````
# successive over-relaxation (SOR)

# Defining equations to be solved
# in diagonally dominant form
f1 = lambda x,y,z: (-1+y-z)/3
f2 = lambda x,y,z: (7+x+z)/3
f3 = lambda x,y,z: (-7-x+y)/3

# Initial setup
x0 = 0
y0 = 0
z0 = 0
count = 1

e = float(input('Enter tolerable error: '))

w = float(input("Enter relaxation factor: "))

# Implementation of successive over-relaxation
print('\nCount\tx\ty\tz\n')

condition = True

while condition:
x1 = (1-w) * x0 + w * f1(x0,y0,z0)
y1 = (1-w) * y0 + w * f2(x1,y0,z0)
z1 = (1-w) * z0 + w * f3(x1,y1,z0)
print('%d\t%0.4f\t%0.4f\t%0.4f\n' %(count, x1,y1,z1))
e1 = abs(x0-x1);
e2 = abs(y0-y1);
e3 = abs(z0-z1);

count += 1
x0 = x1
y0 = y1
z0 = z1

condition = e1>e and e2>e and e3>e

print('\nSolution: x = %0.3f, y = %0.3f and z = %0.3f\n'% (x1,y1,z1))
``````

## Output

```Enter tolerable error: 0.0001
Enter relaxation factor: 1.25

Count	x	y	z

1	-0.4167	2.7431	-1.6001

2	1.4972	2.1880	-2.2288

3	1.0494	1.8782	-2.0141

4	0.9428	2.0007	-1.9723

5	1.0031	2.0126	-2.0029

6	1.0057	1.9980	-2.0025

7	0.9988	1.9990	-1.9993

8	0.9996	2.0004	-1.9998

9	1.0002	2.0001	-2.0001

10	1.0000	2.0000	-2.0000

Solution: x = 1.000, y = 2.000 and z = -2.000
```