Untitled

Anonymous
plain_text
02/28/2026 5:37 AM
4.3 KB
16
Indexable
def jacobi(a, b, x, n, iterations):
    x_new = x.copy()
        for it in range(iterations):
                for i in range(n):
                            sum1 = 0
                                        for j in range(n):
                                                        if j != i:
                                                                            sum1 += a[i][j] * x[j]
                                                                                        x_new[i] = (b[i] - sum1) / a[i][i]
                                                                                                x = x_new.copy()
                                                                                                    return x


                                                                                                    # Number of equations
                                                                                                    n = 3

                                                                                                    # Coefficient matrix
                                                                                                    a = [
                                                                                                            [4, -1, 1],
                                                                                                                [-1, 5, -1],
                                                                                                                    [2, -1, 6]
                                                                                                    ]

                                                                                                    # Constant vector
                                                                                                    b = [7, -8, 6]

                                                                                                    # Initial guess
                                                                                                    x = [0, 0, 0]

                                                                                                    # Number of iterations
                                                                                                    iterations = 10

                                                                                                    # Solve the system
                                                                                                    solution = jacobi(a, b, x, n, iterations)

                                                                                                    # Display result
                                                                                                    print("Solution:")
                                                                                                    for i in range(n):
                                                                                                        print(f"x{i+1} = {solution[i]}")
                                                                                                        
                                                                                                    ]
Editor is loading...
Leave a Comment