Employ the Gauss-Seidel method, solve the system.
10𝑥 + 𝑦 + 𝑧 = 12
2𝑥 + 2𝑦 + 10𝑧 = 14
2𝑥 + 10𝑦 + 𝑧 = 13
# 10x + y + z = 12 -> x = (12-y-z)/10
# 2x + 10y + z = 13 -> z = (13-2x-z)/10
# 2x + 2y + 10z = 14 -> y = (14-2x-2y)/10
f1 = lambda x,y,z: (12-y-z)/10
f2 = lambda x,y,z: (13-2*x-z)/10
f3 = lambda x,y,z: (14-2*x-2*y)/10
x0 = 0
y0 = 0
z0 = 0
n = 1
# tolerable error
err = 0.001
print("Gauss Seidel Iteration")
print('\nIterations\tx\t\ty\t\tz\n')
Flag = True
while(Flag):
x1 = f1(x0,y0,z0)
y1 = f2(x1,y0,z0)
z1 = f3(x1,y1,z0)
print('%d\t%8.4f\t%8.4f\t%8.4f\n' %(n, x1,y1,z1))
err1 = abs(x0-x1);
err2 = abs(y0-y1);
err3 = abs(z0-z1);
n =n+1
x0 = x1
y0 = y1
z0 = z1
Flag = (err1>err and err2>err and err3>err)
print('\nOutput Solution: x=%0.3f, y=%0.3f and z = %0.3f\n'% (x1,y1,z1))
Comments
Leave a comment