Gauss Seidel Method to solve Linear equations in Python
I am trying to solve a linear algebraic equation using Gauss-seidel method in python but cannot seem to find the error here. The equation i am trying to write along with my code is attached in the image below. Thank you.
Code:
import numpy as np
A= np.array([4.,-1.,1.,-1.,4.,-2.,1.,-2.,4.]).reshape(3,3)
B= np.array([12.,-1.,5.]).reshape(3,1)
N= len(B)
print (N)
x=np.zeros(N)
xold= np.array([10.,10.,10.]).reshape(3,1)
tol=0.01
x=np.array([5.,5.,5.]).reshape(3,1)
#for i in range (N):
# print ("start at i=",i, "and xi=",x[i])
temp=0
while (abs(x[0]-xold[0])>tol):
xold=x
print ("absstart=",abs(x-xold))####
for i in range (N):
for j in range (N):
if j!= i:
temp+=A[i,j]*x[j]
#print ("temp=",temp)####
x[i]=(1/A[i,i])*(B[i]-temp)
#print ("end at x",i,"=",x[i])####
#print ("abs= ",abs(x-xold))####
print (x)
print (xold)
from Recent Questions - Stack Overflow https://ift.tt/3nVgXv8
https://ift.tt/3o6xsoB
Comments
Post a Comment