Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include<stdlib.h>
/*
Solving a system of linear equations using the Jacobi method.
This program reads in data of the form
3
4.0 0.3 0.5 1.0
2.0 4.0 0.8 2.0
1.0 0.6 9.0 3.0
where the first number N (in this case 3) indicates that the next
N lines will be an augmented matrix N x N+1
The N x N part of the augmented matrix is the matrix of coefficients S
of vector X, and the last column of the augmented matrix is vector T,
such that SX = T
This program uses the Jacobi method to iteratively solve for X.
*/
#define N 8
#define EPSILON 0.0001
void in(double s[][N], double t[], int * n);
void out2D(char * what, double s[][N], int n);
void out1D(char * what, double t[], int n);
void jacobi(double s[][N], double x[], double t[], int n);
void mult(double a[][N], double b[], double result[], int n) ;
char * check(char * name, double sx[], double t[], int n);
main()
{
char name[25] = "Jacobi" ;
double s[N][N], x[N], t[N], sx[N] ;
int i, j, n,q;
in(s, t, &n);
printf("-----------------------------------------\n") ;
printf("SOLVING S * X = T USING THE JACOBI METHOD\n") ;
printf("-----------------------------------------\n") ;
out2D("S", s, n);
/*
jacobi(s, x, t, n);
mult(s, x, sx, n);
out1D("Jacobi S * X", sx, n);
out1D("original T", t, n);
printf("%s\n", check(name, sx, t, n));*/
printf("\n\n\n");
}
/*
Reads a value *n which is the dimension of a two dimensional
square matrix, and then reads the n x n matrix s and the vector t
where the data have a shape as shown in the top comment.
*/
void in(double s[][N], double t[],int * n)
{
int i,j,both,row,col,row2;
double *matrixs, row3,col3;
FILE *f1;
f1=fopen("matrix20.txt", "r");
fscanf (f1, "%lf", &both);
n=&both;
row3=both;
col3=both;
row2=both+1;
row=both;
col=both;
matrixs = (double *) calloc (row*col, sizeof(double));
for (i=0; i<4; i++)
{
for (j=0; j<3; j++)
{
fscanf (f1, "%lf", &matrixs[(i*both)+j]);
s[i][j]=matrixs[j+(both*i)];
}
}
/*
for (j=1; j<row; j++)
{
fscanf (f1,"%lf", &t[both]);
}
*/
}
/*
Prints a two dimensional square matrix s which is n x n
preceded by the name stored in 'what'. For example, for the
input shown above and the matrix called 's':
s :
4.000000 0.300000 0.500000
2.000000 4.000000 0.800000
1.000000 0.600000 9.000000
*/
void out2D(char * what, double s[][N], int n)
{
int i,j;
FILE *in1,*in2;
char name [10], letter;
in1 = fopen ("what.txt", "r");
fgets (name, sizeof(name), in1);
printf ("%s", name);
fclose (in1);
for (i=0; i<4; i++)
{
for (j=0; j<3; j++)
{
printf ("%7.3lf", s[i][j]);
}
printf ("\n");
}
}
void out1D(char * what, double t[], int n)
{
int i;
FILE *f1;
f1=fopen("matrix20.txt", "r");
for (i=3;i<12; i++)
{
if (i==3)
{
fscanf (f1, "%lf", &t[i-4]);
}
else
if (i==7)
{
fscanf (f1, "%lf", &t[i-7]);
}
else
if (i==11)
{
fscanf (f1, "%lf", &t[i-10]);
}
else
{
fscanf (f1, "%*lf");
}
}
}
/*
Runs the jacobi iterative solution to S X = T.
The matrix S is given by s and is n x n.
The vector X is given by x, which has n elements.
The vector T is given by t, which has n elements.
The iteration stops if all new elements differ by no more than
EPSILON from the old elements.
*/
void jacobi(double s[][N], double x[], double t[], int n)
{
int i, j, flag = 1, k;
double new_x[N], sum;
// TO DO
}
/*
Multiplies an n x n matrix A by a vector X, i.e., an n x 1 matrix.
The resulting vector is called result.
*/
void mult(double a[][N], double x[], double result[], int n)
{
{
int i, j, k, sum ;
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j< n ; j++) {
sum = 0 ;
sum = sum + a[i][j] * x[i] ;
result[i] = sum ;
}
}
}
}
/*
Checks if two vectors are 'close', meaning that each pair of
corresponding elements differ by no more than EPSILON.
It returns a string "Jacobi check fails" if the vectors are not close
otherwise a string "Jacobi check is OK".
*/
char * check(char * name, double sx[], double t[], int n)
{
int i;
for(i = 0 ; i < n ; i++)
if(fabs(sx[i] - t[i]) > 10 * EPSILON)
return (strcat(name, " check fails"));
return (strcat(name, " check is OK"));
}