Aim:
☛
UNIT-III: Linear system of equations:
Jacobi's iteration method and Gauss-Seidal iteration method
Programs:
โข Solution of given system of linear equations using Jacobi's method.
โข Solution of given system of linear equations using Gauss-Seidal method.
Solution :
โข Solution of given system of linear equations using Jacobi's method.
PROGRAM: (Jacobis_Method.py)
import numpy as np
# Coefficient matrix A
A = np.array([[4, 1, 1],
[1, 5, 2],
[1, 2, 4]], dtype=float)
# Constant vector B
B = np.array([6, 8, 7], dtype=float)
# Initial guess
x = np.zeros(len(B))
# Tolerance and max iterations
e = 0.001
max_iter = 50
for k in range(max_iter):
x_new = np.zeros_like(x)
for i in range(len(A)):
s = 0
for j in range(len(A)):
if i != j:
s += A[i][j] * x[j]
x_new[i] = (B[i] - s) / A[i][i]
print(f"Iteration {k+1}: {x_new}")
if np.allclose(x, x_new, atol=e):
break
x = x_new
print("\nFinal Solution:", x)
OUTPUT:
Iteration 1: [1.5 1.6 1.75]
Iteration 2: [0.6625 0.6 0.575 ]
Iteration 3: [1.20625 1.2375 1.284375]
Iteration 4: [0.86953125 0.845 0.8296875 ]
Iteration 5: [1.08132812 1.09421875 1.11011719]
Iteration 6: [0.94891602 0.9396875 0.93255859]
Iteration 7: [1.03193848 1.03719336 1.04292725]
Iteration 8: [0.97996985 0.97644141 0.9734187 ]
Iteration 9: [1.01253497 1.01463855 1.01678683]
Iteration 10: [0.99214365 0.99077827 0.98954698]
Iteration 11: [1.00491869 1.00575248 1.00657495]
Iteration 12: [0.99691814 0.99638628 0.99589409]
Iteration 13: [1.00192991 1.00225874 1.00257732]
Iteration 14: [0.99879099 0.99858309 0.99838816]
Iteration 15: [1.00075719 1.00088654 1.00101071]
Iteration 16: [0.99952569 0.99944428 0.99936743]
Iteration 17: [1.00029707 1.00034789 1.00039644]
Iteration 18: [0.99981392 0.99978201 0.99975179]
Final Solution: [1.00029707 1.00034789 1.00039644]
โข Solution of system of linear equations using Gauss- Seidal method.
PROGRAM: (Gauss_Seidal_Method.py)
import numpy as np
# Coefficient matrix A
A = np.array([[4, 1, 1],
[1, 5, 2],
[1, 2, 4]], dtype=float)
# Constant vector B
B = np.array([6, 8, 7], dtype=float)
# Initial guess
x = np.zeros(len(B))
# Tolerance and max iterations
e = 0.001
max_iter = 50
print("Gauss-Seidel Iterations:\n")
for k in range(max_iter):
x_old = x.copy()
for i in range(len(A)):
s1 = 0
s2 = 0
for j in range(i):
s1 += A[i][j] * x[j] # updated values
for j in range(i+1, len(A)):
s2 += A[i][j] * x_old[j] # old values
x[i] = (B[i] - s1 - s2) / A[i][i]
print(f"Iteration {k+1}: {x}")
if np.allclose(x, x_old, atol=e):
break
print("\nFinal Solution:", x)
OUTPUT:
Gauss-Seidel Iterations:
Iteration 1: [1.5 1.3 0.725]
Iteration 2: [0.99375 1.11125 0.9459375]
Iteration 3: [0.98570313 1.02448438 0.99133203]
Iteration 4: [0.9960459 1.00425801 0.99885952]
Iteration 5: [0.99922062 1.00061207 0.99988881]
Iteration 6: [0.99987478 1.00006952 0.99999655]
Final Solution: [0.99987478 1.00006952 0.99999655]
๐ป Run This Python Program
#
The selected Python program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.
The compiler is embedded using OneCompiler.
Your Python program runs inside the embedded
Python environment; Study Glance does not execute
Python code directly on your web server.
MATLAB programs shown in the experiment should be run in
MATLAB (or GNU Octave), and programs that use libraries such as
NumPy, SciPy or SymPy depend on what the embedded environment provides.