Jacobi Iteration Method In C

Jacobi iteration presents a method for solving linear equation systems using numerical techniques. It estimates the solution by repeatedly iterating and belongs to the group of iterative strategies. Due to the challenges posed by memory and computational requirements when dealing with extensive sparse systems, this technique proves valuable, particularly in cases where traditional methods such as Gaussian elimination are impractical.

In the procedure of solving Jacobi iteration involves breaking down the equation Ax=b into a series of adjustments for each variable. Here, A represents a square matrix of coefficients, x denotes the vector of unknowns, and b stands for the vector of constants.

To initiate this procedure, start by making an initial approximation of x(0). Employ the provided equation to calculate the revised value of each variable xi within the set x:

Example 1:

Let's consider a specific scenario to demonstrate the Jacobi Iteration Technique in the C programming language.

Example

#include <stdio.h>

#include <math.h>

#define N 3 // Size of the system of equations

// Function to calculate the next iteration of x

void jacobi_iteration(double A[N][N], double b[N], double x[N]) {

    double x_new[N]; // Store the updated values of x

    int i, j;

    // Iterate through each equation

    for (i = 0; i < N; i++) {

        double sum = 0.0;

        for (j = 0; j < N; j++) {

            if (j != i) {

                sum += A[i][j] * x[j];

            }

        }

        x_new[i] = (b[i] - sum) / A[i][i];

    }

    // Update the original x values

    for (i = 0; i < N; i++) {

        x[i] = x_new[i];

    }

}

int main() {

    double A[N][N] = {{2, -1, 0}, {-1, 2, -1}, {0, -1, 2}}; // Coefficient matrix

    double b[N] = {1, 0, 1}; // Constants vector

    double x[N] = {0}; // Initial guess for x

    double tolerance = 1e-6; // Desired tolerance for convergence

    int max_iterations = 1000; // Maximum number of iterations

    int iteration = 0;

    double error;

    // Perform iterations until convergence or maximum iterations reached

    do {

        jacobi_iteration(A, b, x);

        // Calculate the error (norm of the residual)

        error = 0.0;

        for (int i = 0; i < N; i++) {

            double residual = b[i];

            for (int j = 0; j < N; j++) {

                residual -= A[i][j] * x[j];

            }

            error += residual * residual;

        }

        error = sqrt(error);

        iteration++;

    } while (error > tolerance && iteration < max_iterations);

    // Output the solution

    printf("Solution after %d iterations:\n", iteration);

    for (int i = 0; i < N; i++) {

        printf("x[%d] = %lf\n", i, x[i]);

    }

    return 0;

}

Output:

Output

Solution after 41 iterations:

x[0] = 1.000000

x[1] = 0.999999

x[2] = 1.000000

Example 2:

Let us take another example to illustrate the Jacobi Iteration Method in C.

Example

#include <stdio.h>

#include <math.h>

#define N 3 // Size of the system of equations

// Function to perform Jacobi iteration

void jacobi_iteration(double A[N][N], double b[N], double x[N]) {

    double x_new[N]; // Store the updated values of x

    int i, j;

    // Iterate through each equation

    for (i = 0; i < N; i++) {

        double sum = 0.0;

        for (j = 0; j < N; j++) {

            if (j != i) {

                sum += A[i][j] * x[j];

            }

        }

        x_new[i] = (b[i] - sum) / A[i][i];

    }

    // Update the original x values

    for (i = 0; i < N; i++) {

        x[i] = x_new[i];

    }

}

int main() {

    // Define the coefficient matrix A

    double A[N][N] = {{4, -1, 0}, {-1, 4, -1}, {0, -1, 4}};

    // Define the constants vector b

    double b[N] = {12, 6, 6};

    // Initialize the solution vector x with zeros

    double x[N] = {0};

    // Define the tolerance for convergence

    double tolerance = 1e-6;

    // Define the maximum number of iterations

    int max_iterations = 1000;

    int iteration = 0;

    double error;

    // Perform iterations until convergence or maximum iterations reached

    do {

        jacobi_iteration(A, b, x);

        // Calculate the error (norm of the residual)

        error = 0.0;

        for (int i = 0; i < N; i++) {

            double residual = b[i];

            for (int j = 0; j < N; j++) {

                residual -= A[i][j] * x[j];

            }

            error += residual * residual;

        }

        error = sqrt(error);

        iteration++;

    } while (error > tolerance && iteration < max_iterations);

    // Prints the Output of the solution

    printf("Solution after %d iterations:\n", iteration);

    for (int i = 0; i < N; i++) {

        printf("x[%d] = %lf\n", i, x[i]);

    }

    return 0;

}

Output:

Output

Solution after 16 iterations:

x[0] = 3.750000

x[1] = 3.000000

x[2] = 2.250000

Input Required

This code uses input(). Please provide values below: