Thread: Read coefficients of linear equations from file and use jacobi method

1. Read coefficients of linear equations from file and use jacobi method

hi;
i did a function that get inputs of equation from user but i want to get from file

Code:
```void getInput( int numUnKnowns, double** mat ){
int i, j;

printf
(
"\nEnter values for the specified row and column below ...\n"
"(The last column is the value for the RHS of the equation.)\n"
);

for( i = 0 ; i < numUnKnowns ; i++ )
{
mat[i] = (double*) malloc( (numUnKnowns+1)*sizeof(double) );
puts("");
for( j = 0 ; j < numUnKnowns+1 ; j++ )
{
printf("matrix[%d][%d] : ", i, j);
if( scanf("%lf", &mat[i][j]) != 1 )
{
--j;
puts("Bad entry ... try again with a 'real' number.");
}
while( getchar() != '\n' ) ; /* flush stdin ... */
}
}

printf("\nThe matrix entered:\n\n");
for( i = 0 ; i < numUnKnowns ; i++ )
{
for( j = 0 ; j < numUnKnowns+1 ; j++ ) printf("%+9f ", mat[i][j]);
puts("");
}

printf("\nPress 'Enter' to start iteration ... ");
getchar();
}```
let file :
7a - 2b + c + 2d = 3
2a + 8b + 3c + d = -2
-a + 5c + 2d = 5
2b - c + 4d = 4

can any one help me how function read from file.
thanks

2. Is this for a homework assignment or what? If it's for a homework assignment then fine, otherwise the Jacobi method is a waste of time. If you're going to use something that updates each value one by one then at least use Gauss-Seidel or Successive Over-relaxation (SOR).

Looking at the system of equations you provided, I'm not sure the diagonal values are prominent enough to converge using any of those methods anyhow, you might need to use Gaussian elimination.

Anyway, concerning your original question, you could make things much easier on yourself if you had a file like this:
7 2 1 2 3
2 8 3 1 -2
-1 0 5 2 5
0 2 -1 4 4

Then you could use getline using a space as the delimiter to split those coefficients up into individual values, converting from string to a numerical type, of course.

3. Originally Posted by Epy
Is this for a homework assignment or what? If it's for a homework assignment then fine, otherwise the Jacobi method is a waste of time. If you're going to use something that updates each value one by one then at least use Gauss-Seidel or Successive Over-relaxation (SOR).

Looking at the system of equations you provided, I'm not sure the diagonal values are prominent enough to converge using any of those methods anyhow, you might need to use Gaussian elimination.

Anyway, concerning your original question, you could make things much easier on yourself if you had a file like this:
7 2 1 2 3
2 8 3 1 -2
-1 0 5 2 5
0 2 -1 4 4

Then you could use getline using a space as the delimiter to split those coefficients up into individual values, converting from string to a numerical type, of course.

yes for a homework
Gauss-Seidel or Successive Over-relaxation (SOR) are better than jacobi
In homework assume for for strictly diagonally dominant matrices.
just i want to read from file that have linear equations which are strictly diagonall

4. This looks like C, not C++. Did you post it in the wrong section?

5. Originally Posted by Elysia
This looks like C, not C++. Did you post it in the wrong section?
yes in C
thanks