0% found this document useful (0 votes)
18 views4 pages

Maths

Uploaded by

amrithavarsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Maths

Uploaded by

amrithavarsha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“JNANA SANGAMA”, BELAGAVI - 590 018

ASSIGNMENT REPORT on

“Numerical Methods and Applications”

Submitted by

Amrithavarsha 4SF21CY008

In partial fulfillment of the requirements for the VII semester

BACHELOR OF ENGINEERING

in

COMPUTER SCIENCE & ENGINEERING

Under the Guidance of

Mr. Sunil Kumar K

Assistant Professor, Department of Civil Engineering

at

Sahyadri College of Engineering and Management,

An Autonomous Institution, Mangaluru 2024-25


GAUSS-SIEDEL METHOD

1) Flowchart
2) Code

def gauss_seidel(A, b, x0, tol=1e-10, max_iter=100):


"""
Solve Ax = b using the Gauss-Seidel method.

Parameters:
A: Coefficient matrix (list of lists or 2D array)
b: Constant terms vector (list or 1D array)
x0: Initial guess vector (list or 1D array)
tol: Tolerance for convergence (default: 1e-10)
max_iter: Maximum number of iterations (default: 100)

Returns:
x: Solution vector
"""
n = len(b)
x = x0.copy()

for _ in range(max_iter):
x_new = x.copy()
for i in range(n):
sum1 = sum(A[i][j] * x_new[j] for j in range(i))
sum2 = sum(A[i][j] * x[j] for j in range(i + 1, n))
x_new[i] = (b[i] - sum1 - sum2) / A[i][i]

# Check for convergence


if max(abs(x_new[i] - x[i]) for i in range(n)) < tol:
return x_new
x = x_new

print("Did not converge within the maximum number of iterations.")


return x

# Interactive Input
def main():
n = int(input("Enter the number of equations: "))
print("Enter the coefficient matrix (row by row):")
A = []
for i in range(n):
row = list(map(float, input(f"Row {i + 1}: ").split()))
A.append(row)

print("Enter the constant terms vector:")


b = list(map(float, input().split()))

print("Enter the initial guess vector:")


x0 = list(map(float, input().split()))

tol = float(input("Enter the tolerance for convergence: "))


max_iter = int(input("Enter the maximum number of iterations: "))
solution = gauss_seidel(A, b, x0, tol, max_iter)
print("Solution:", solution)

if __name__ == "__main__":
main()

3) Output

You might also like