Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views6 pages

Coding Program

The document presents a solution to a system of linear equations using the Gauss-Seidel method, specifying the matrix A and vector B. It includes a C program that iteratively calculates the solution with a tolerance for convergence set to four decimal places. The final output shows the solution after 9 iterations, achieving convergence with the values for x rounded to four decimal places.

Uploaded by

Chhattu
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)
6 views6 pages

Coding Program

The document presents a solution to a system of linear equations using the Gauss-Seidel method, specifying the matrix A and vector B. It includes a C program that iteratively calculates the solution with a tolerance for convergence set to four decimal places. The final output shows the solution after 9 iterations, achieving convergence with the values for x rounded to four decimal places.

Uploaded by

Chhattu
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/ 6

ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

10. Solve the system of linear equation AX = B using Gauss-Seidal method correct to 4D.

𝟒. 𝟒𝟗 + 𝒑 𝟏. 𝟒𝟏 𝟏. 𝟐𝟖 𝟏. 𝟑𝟓
𝟏. 𝟏𝟗 −𝟕. 𝟑𝟏 + 𝒑 −𝟑. 𝟏𝟒 𝟐. 𝟐𝟑
Where A = [ ] and B = (𝟒. 𝟒𝟏, 𝟓. 𝟏𝟒, −𝟒. 𝟐𝟕, 𝟑. 𝟕𝟔)𝑻
𝟏. 𝟖𝟖 −𝟐. 𝟕𝟒 𝟕. 𝟕𝟓 + 𝒑 𝟏. 𝟖𝟓
𝟏. 𝟖𝟓 𝟐. 𝟐𝟑 𝟏. 𝟔𝟓 𝟔. 𝟒𝟏 + 𝒑
𝒓
Where p = 1+ 𝟏𝟎 .

ANS:

Iterati#include <stdio.h>

#include <math.h>

#define N 4 // Size of the system

#define MAX_ITER 100 // Maximum number of iterations

#define TOLERANCE 0.00005 // For 4 decimal places, 0.5 * 10^(-4)

int main() {

double A[N][N];

double B[N];

double x[N]; // Current solution

double prev_x[N]; // Previous solution

int i, j, iter;

double sum;

double p = 1.0; // Assuming r = 0, so p = 1 + 0/10 = 1

// Initialize matrix A with p = 1

A[0][0] = 4.49 + p; A[0][1] = 1.41; A[0][2] = 1.28; A[0][3] = 1.35;

A[1][0] = 1.19; A[1][1] = -7.31 + p; A[1][2] = -3.14; A[1][3] = 2.23;

A[2][0] = 1.88; A[2][1] = -2.74; A[2][2] = 7.75 + p; A[2][3] = 1.85;

A[3][0] = 1.85; A[3][1] = 2.23; A[3][2] = 1.65; A[3][3] = 6.41 + p;


ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

// Initialize vector B

B[0] = 4.41;

B[1] = 5.14;

B[2] = -4.27;

B[3] = 3.76;

// Initial guess for x (usually zeros)

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

x[i] = 0.0;

prev_x[i] = 0.0;

printf("Gauss-Seidel Method:\n");

printf("Initial guess: x = [0.0, 0.0, 0.0, 0.0]\n");

for (iter = 1; iter <= MAX_ITER; iter++) {

printf("\nIteration %d:\n", iter);

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

prev_x[i] = x[i]; // Store previous iteration's values

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

sum = 0.0;

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

if (i != j) {

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

x[i] = (B[i] - sum) / A[i][i];


ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

printf("x[%d] = %.10f\n", i, x[i]); // Print with more precision for intermediate steps

// Check for convergence

double max_diff = 0.0;

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

double diff = fabs(x[i] - prev_x[i]);

if (diff > max_diff) {

max_diff = diff;

if (max_diff < TOLERANCE) {

printf("\nConvergence achieved after %d iterations.\n", iter);

printf("Solution (correct to 4 decimal places):\n");

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

printf("x[%d] = %.4f\n", i, x[i]);

return 0;

printf("\nMaximum iterations reached without convergence.\n");

printf("Approximate solution:\n");

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

printf("x[%d] = %.4f\n", i, x[i]);

return 0;
ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

//********OUTPUT*******

Gauss-Seidel Method:

Initial guess: x = [0.0, 0.0, 0.0, 0.0]

Iteration 1:

x[0] = 0.8032786885

x[1] = -0.6630900730

x[2] = -0.8682320839

x[3] = 0.6997581953

Iteration 2:

x[0] = 1.0039381615

x[1] = 0.0541039511

x[2] = -0.8347098947

x[3] = 0.4263608524

Iteration 3:

x[0] = 0.8791539059

x[1] = -0.0827310747

x[2] = -0.7929440074

x[3] = 0.4893944916

Iteration 4:

x[0] = 0.8890594865

x[1] = -0.0793700969

x[2] = -0.8073469383

x[3] = 0.4891171004
ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

Iteration 5:

x[0] = 0.8916225559

x[1] = -0.0718175338

x[2] = -0.8054739524

x[3] = 0.4857872326

Iteration 6:

x[0] = 0.8900649577

x[1] = -0.0742201206

x[2] = -0.8051876150

x[3] = 0.4868353930

on 7:

x[0] = 0.8903575112

x[1] = -0.0739370086

x[2] = -0.8053834288

x[3] = 0.4867207545

Iteration 8:

x[0] = 0.8903586434

x[1] = -0.0738798677

x[2] = -0.8053415409

x[3] = 0.4866939484

Iteration 9:

x[0] = 0.8903407934

x[1] = -0.0739135519

x[2] = -0.8053425861

x[3] = 0.4867087746
ROLL NO: 223513-21-0085 REG NO: 513-1112-0417-22

Convergence achieved after 9 iterations.

Solution (correct to 4 decimal places):

x[0] = 0.8903

x[1] = -0.0739

x[2] = -0.8053

x[3] = 0.4867

You might also like