import numpy as np
# Function to implement the SOT method
def sot(A, b, omega=1.25, tol=1e-5, max_iterations=10000):
n = len(b)
x = [Link](n) # Initial guess (zero vector)
# Tabulate (cache) inverse of diagonal elements of A
D_inv = 1 / [Link](A)
for k in range(max_iterations):
x_old = [Link](x) # Store old values of x for comparison
for i in range(n):
# Compute the sum of non-diagonal terms (this part is repeated
in each iteration)
sigma = sum(A[i, j] * x[j] for j in range(n) if j != i)
# Use tabulated diagonal inverse to update the value of x[i]
x[i] = (1 - omega) * x[i] + omega * D_inv[i] * (b[i] - sigma)
# Check for convergence based on the infinity norm of the
difference
if [Link](x - x_old, ord=[Link]) < tol:
print(f"Converged after {k+1} iterations.")
return x
print("Maximum iterations reached without convergence.")
return x
# Coefficient matrix A and vector b representing the system of
equations
A = [Link]([[6, -2],
[2, -8]], dtype=float)
b = [Link]([10, 0], dtype=float)
# Relaxation factor (omega)
omega = 1.25 # Can be tuned for better performance
# Solve the system using SOT
solution = sot(A, b, omega)
# Print the currents I1 and I2
print("Solution (Currents):")
print("I1:", solution[0], "A")
print("I2:", solution[1], "A")
Converged after 15 iterations.
Solution (Currents):
I1: 2.0 A
I2: 0.5 A