In: Computer Science
import math import numpy as np import numpy.linalg from scipy.linalg import solve A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0, 0], [-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0], [math.cos(math.pi/6), 1, 0, 1, 0, 0], [math.sin(math.pi/6), 0, 0, 0, 1, 0], [0, -1, -math.cos(math.pi/3), 0, 0, 0], [0, 0, math.sin(math.pi/3), 0, 0, 1]]) b = np.array([0, 2000, 0, 0, 0, 0])
x = [0, 0, 0, 0, 0, 0] def seidel(a, x, b): # Finding length of a(3) n = len(a) # for loop for 3 times as to calculate x, y , z for j in range(0, n): # temp variable d to store b[j] d = b[j] # to calculate respective xi, yi, zi for i in range(0, n): if (j != i): d -= a[j][i] * x[i] # updating the value of our solution x[j] = d / a[j][j] # returning our updated solution return x seidel(A, x, b)
RESULTS:
:\Users\mauri\Anaconda3\python.exe
"C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL
METHODS/HW 7.py"
C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL
METHODS/HW 7.py:75: RuntimeWarning: divide by zero encountered in
double_scalars
x[j] = d / a[j][j]
C:/Users/mauri/PycharmProjects/OCEN 261 APPLIED NUMERICAL
METHODS/HW 7.py:73: RuntimeWarning:
PLEASE HELP MEND MY GAUSS-Seidel Code
CODE:
import math
import numpy as np
import warnings
#import numpy.linalg
#from scipy.linalg import solve
A = np.array([[-math.cos(math.pi/6),0, math.cos(math.pi/3),0, 0,
0],
[-math.sin(math.pi/6), 0, -math.sin(math.pi/3), 0, 0, 0],
[math.cos(math.pi/6), 1, 0, 1, 0, 0],
[math.sin(math.pi/6), 0, 0, 0, 1, 0],
[0, -1, -math.cos(math.pi/3), 0, 0, 0],
[0, 0, math.sin(math.pi/3), 0, 0, 1]])
b = np.array([0, 2000, 0, 0, 0, 0])
x = [0, 0, 0, 0, 0, 0]
def seidel(a, x, b):
# Finding length of a(3)
n = len(a)
# for loop for 3 times as to calculate x, y , z
for j in range(0, n):
# temp variable d to store b[j]
d = b[j]
# to calculate respective xi, yi, zi
for i in range(0, n):
if (j != i):
d -= a[j][i] * x[i]
# updating the value of our solution
warnings.filterwarnings("ignore")
x[j] = d / a[j][j]
# returning our updated solution
return x
seidel(A, x, b)
OUTPUT: