Question

In: Computer Science

Write a C program to implement Jacobi iterative method for sovling a system of linear equations....

Write a C program to implement Jacobi iterative method for sovling a system of linear equations. Use the skeleton code.

skeleton code:

/* 
   CS288 HOMEWORK 8
   Your program will take in two command-line parameters: n and error
   command: jacobi 5 0.0001
   command: jacobi 10 0.000001
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define N 100
#define MAX_ITER 10000

int jacobi();
void init();
int convergence();
void srand();
void print_vector();
void print_equation();

float a[N][N], b[N];
float x[N], buf[N];
int n;
float error;

int main(int argc, char **argv){
  int n_iter;                   /* number of iterations */
  n = atoi(argv[1]);
  error = atof(argv[2]);

  init();                  /* initalize a, x0 and b - DO not change */
  n_iter = jacobi();

  return 0;
}

int jacobi(){
  int i,j,k;
  float sum;
  // ...
  // ...
  // ...
  return k;
}

// returns 1 if converged else 0
int convergence(int iter){
  int i,j,k,flag=1;
  // ...
  // ...
  // ...

  return flag;
}

// Try not to change this. Use it as is.
void init(char **argv){
  int i,j,k,flag=0;
  float sum;
  int seed = time(0) % 100;     /* seconds since 1/1/1970 */

  srand(seed);
  for (i=0;i<n;i++) {
    for (j=0;j<n;j++) {
      a[i][j] = rand() & 0x7;
      if (rand() & 0x1) a[i][j] = -a[i][j];
    }
    sum = 0;
    for (j=0;j<n;j++) if(i!=j) sum = sum + abs(a[i][j]);
    if (a[i][i] < sum) a[i][i] = sum + a[i][i];
  }

  for (i=0;i<n;i++) x[i]=1;

  srand(seed);
  for (i=0;i<n;i++){
    b[i]=rand() & 0x7;
    if (rand() & 0x1) b[i] = -b[i];
  }

  print_equation();

}

void print_equation(){
  int i,j;

  printf("A*x=b\n");
  for (i=0;i<n;i++) {
    for (j=0;j<n;j++) printf("%2d ",(int)(a[i][j]));
    printf(" * x%d = %d\n",i,(int)(b[i]));
  }
  printf("\n");
}

void print_vector(float *l){
  int i;
  for (i=0; i<n; i++) printf("%.6f ",l[i]);
  printf("\n");
}

// end of file

Solutions

Expert Solution

Working code implemented in C and appropriate comments provided for better understanding:

Source code for jacobi.c:

/*
CS288 HOMEWORK 8
Your program will take in two command-line parameters: n and error
command: jacobi 5 0.0001
command: jacobi 10 0.000001
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define N 100
#define MAX_ITER 10000

int jacobi();
void init();
int convergence();
void srand();
void print_vector();
void print_equation();

float a[N][N], b[N];
float x[N], buf[N];
int n;
float error;

int main(int argc, char **argv){
int n_iter;           /* number of iterations */
if(argc!=3){
   printf("wrong # of arguments\n");
   return 0;
}
n = atoi(argv[1]);
error = atof(argv[2]);

init();       /* initalize a, x0 and b - DO not change */
  
n_iter = jacobi();
printf("# of iteration:%d\n",n_iter);
print_vector(x);

return 0;
}

int jacobi(){
int i,j,k=0;
float sum;
// ...
while(!convergence()&& (k<MAX_ITER)){
   for(i=0;i<n;i++){
       sum=0;
       for(j=0;j<n;j++)
           if(j!=i) sum+=a[i][j]*x[j];
       x[i]=(b[i]-sum)/a[i][i];
   }
   k++;
}
return k;
}

// returns 1 if converged else 0
int convergence(){
int i,j;
float abs,sum;
// ...
for(i=0;i<n;i++){
       sum=0;
       for(j=0;j<n;j++)
           sum+=a[i][j]*x[j];
       abs=sum-b[i];
       if(abs<0)abs=-abs;
       //printf("abs:%f ",abs);
       if(abs>error){
           //printf("\n");
           return 0;
       }
}
//printf("\n");
return 1;
}

// Try not to change this. Use it as is.
void init(char **argv){
int i,j,k,flag=0;
float sum;
int seed = time(0) % 100;   /* seconds since 1/1/1970 */

srand(seed);
for (i=0;i<n;i++) {
for (j=0;j<n;j++) {
a[i][j] = rand() & 0x7;
if (rand() & 0x1) a[i][j] = -a[i][j];
}
sum = 0;
for (j=0;j<n;j++) if(i!=j) sum = sum + abs(a[i][j]);
if (a[i][i] < sum) a[i][i] = sum + a[i][i];
}

for (i=0;i<n;i++) x[i]=1;

srand(seed);
for (i=0;i<n;i++){
b[i]=rand() & 0x7;
if (rand() & 0x1) b[i] = -b[i];
}

print_equation();

}

void print_equation(){
int i,j;

printf("A*x=b\n");
for (i=0;i<n;i++) {
for (j=0;j<n;j++) printf("%2d ",(int)(a[i][j]));
printf(" * x%d = %d\n",i,(int)(b[i]));
}
printf("\n");
}

void print_vector(float *l){
int i;
for (i=0; i<n; i++) printf("%.6f ",l[i]);
printf("\n");
}

// end of file

Output Screenshots:

Hope it helps, if you like the answer give it a thumbs up. Thank you.


Related Solutions

Iterative method of solving large systems Is there any example for which Jacobi method fails and...
Iterative method of solving large systems Is there any example for which Jacobi method fails and Gauss-Seidel method succeeds?
The Gauss-Seidel method as an iterative technique often refers to an improved version of the Jacobi...
The Gauss-Seidel method as an iterative technique often refers to an improved version of the Jacobi method, since the Gauss-Seidel method generally achieves a faster convergence. Describe the difference between the Gauss-Seidel and Jacobi methods.
Write a python program that can solve system of linear equations in three variables using input...
Write a python program that can solve system of linear equations in three variables using input function. Paste your program in a word document or notepad. Note that I am using pycharm. please use a not really complex codes, thanks
Use a software program or a graphing utility to solve the system of linear equations. (If...
Use a software program or a graphing utility to solve the system of linear equations. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, set x5 = t and solve for x1, x2, x3, and x4 in terms of t.) x1 − x2 + 2x3 + 2x4 + 6x5 = 16 3x1 − 2x2 + 4x3 + 4x4 + 12x5 = 33 x2 − x3 − x4 − 3x5 = −9 2x1...
Use a software program or a graphing utility to solve the system of linear equations. (If...
Use a software program or a graphing utility to solve the system of linear equations. (If there is no solution, enter NO SOLUTION. If the system has an infinite number of solutions, set x5 = t and solve for x1, x2, x3, and x4 in terms of t.) x1 − x2 + 2x3 + 2x4 + 6x5 = 16 3x1 − 2x2 + 4x3 + 4x4 + 12x5 = 33 x2 − x3 − x4 − 3x5 = −9 2x1...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no...
1) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION. If there are infinitely many solutions, express your answer in terms of the parameters t and/or s.) 3y + 2z = 1 2x − y − 3z = 4 2x + 2y − z = 5 (x, y, z) = 2) Solve the system of linear equations, using the Gauss-Jordan elimination method. (If there is no solution, enter NO SOLUTION....
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a function to solve a system of linear equations of the form Ax= b using...
Write a function to solve a system of linear equations of the form Ax= b using the iterative Gauss-Seidel method. You are free to use any basic MATLAB operation to implement the algorithm (i.e. you may use any combination of loops, indexing, math, etc.), but avoid “built-in” solution methods — you would not be allowed to use the GaussSeidel function if such a function existed. The function must also test for a number of possible issues. If an issue is...
18.11 Lab: Linear diophantine equations In this program, you will be solving linear Diophantine equations recursively....
18.11 Lab: Linear diophantine equations In this program, you will be solving linear Diophantine equations recursively. A linear Diophantine equation is an equation in the form: ax + by = c where a, b, and c are all integers and the solutions will also be integers. See the following entry in Wikipedia: Linear Diophantine equations. You will be solving this using the recursive version of the Extended Euclidean algorithm for finding the integers x and y in Bezout's identity: ax...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT