Jacobi Iteration Method

This is a discussion on Jacobi Iteration Method within the C Programming forums, part of the General Programming Boards category; Hello again, everyone. I've since moved away from my full-on matrice based solution and trying to do some manual computing ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Jacobi Iteration Method

    Hello again, everyone. I've since moved away from my full-on matrice based solution and trying to do some manual computing now. I've done the thing myself, solved a linear equation system with 10 unknowns by hand, and found out the approximate solutions. However badly coded, the program should still give (as I've to hand in the project in a few hours) adequate results.

    I cannot, however, for the love of god, get make my file-reading working. I've tried anything I can think of. It might be the lack of sleep, or whatever else.. Would anyone here be able to spot anything wrong with the file opening phase? It'd seem the error is there, as when I try to print the array I have got the file contents stored to, it just gives weird values... I'd greatly appreciate if anyone could help me out here and point out the problem.

    P.S.: I'm using the algorithm mentioned here Here's the code and the 2 data textfiles..

    SOURCE:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define VAR 100
    #define n 10
    
    void Read();
    void Calculeet(float temp[VAR]);
    
    int main()
    {
    	
    	Read();
    	
    	
    
    	
    	return 0;
    }
    	
    void Read()
    {
    	int i = 0;
    	float temp[VAR];
    	
    	
    	FILE *pData;
    	pData = fopen("data2.txt", "r");
    	
    	if (pData == NULL)
    	{
    		fprintf(stderr, "Dosya ašılamadı\n");
    		exit(1);
    	}
    	
    	while (!feof(pData))
    	{
    		fscanf (pData, "%f ", &temp[i]);
    		i++;
    			//for (i = 0;i < 30;i++)
    	printf("%f\n", temp[i]);
    	}
    	
    	fclose (pData);
    	
    	Calculeet(temp);
    
    }
    
    void Calculeet(float temp[VAR])
    {
    
    
    	float A[n],B[n],C[n],D[n],E[n],F[n],G[n],H[n],I[n],J[n],Z[n],tempa[1000],tempb[1000],tempc[1000]
    	,tempd[1000],tempe[1000],tempf[1000],tempg[1000],temph[1000],tempi[1000],tempj[1000];
    	int i = 0;
    	
    	
    	
    	FILE *pDataB;
    	pDataB = fopen("DataB.txt", "r");
    	
    	
    	if (pDataB == NULL)
    	{
    		fprintf(stderr, "Dosya ašılamadı\n");
    		exit(1);
    	}
    	
    	while (!feof(pDataB))
    	{
    		fscanf (pDataB, "%f", &Z[i]);
    		i++;
    		
    		printf("%f\n", Z[i]);
    	}
    	
    	fclose (pDataB);
    	
    	
    	printf ("%f\n%f\n", temp[0], temp[1]);
    	
    	i = 0;
    	
    
    	
    	do
    	{
    	
    	if (abs(i % 10) == 0)
    		A[i%10] = temp[i];
    	else if (abs(i % 10) == 1)
    		B[i%10] = temp[i];
    	else if (abs(i % 10) == 2)
    		C[i%10] = temp[i];
    	else if (abs(i % 10) == 3)
    		D[i%10] = temp[i];	
    	else if (abs(i % 10) == 4)
    		E[i%10] = temp[i];
    	else if (abs(i % 10) == 5)
    		F[i%10] = temp[i];
    	else if (abs(i % 10) == 6)
    		G[i%10] = temp[i];
    	else if (abs(i % 10) == 7)
    		H[i%10] = temp[i];
    	else if (abs(i % 10) == 8)
    		I[i%10] = temp[i];
    	else if (abs(i % 10) == 9)
    	{
    		J[i%10] = temp[i];
    	}
    	
    	i++;
    		
    		
    	} while (i < 100);
    	printf("%f\n", A[0]);
    	printf("%f\n", A[1]);
    	printf("%f\n", A[2]);
    	printf("%f\n", A[3]);
    	printf("%f\n", A[4]);
    	printf("%f\n", A[5]);
    	printf("%f\n", A[6]);
    	printf("%f\n", A[7]);
    	printf("%f\n", A[8]);
    	
    	
    		
    	while (i <= 50)
    	{
    	tempa[i+1] = (Z[0]/A[0]) - (B[0]*tempb[i]/A[0]) - (C[0]*tempc[i]/A[0]) - (D[0]*tempd[i]/A[0]) - (E[0]*tempe[i]/A[0]) - (F[0]*tempf[i]/A[0])
    	- (G[0]*tempg[i]/A[0]) - (H[0]*temph[i]/A[0]) - (I[0]*tempi[i]/A[0]) - (J[0]*tempj[i]/A[0]);
    	tempb[i+1] = (Z[1]/B[1]) - (A[1]*tempa[i]/B[1]) - (C[1]*tempc[i]/B[1]) - (D[1]*tempd[i]/B[1]) - (E[1]*tempe[i]/B[1]) - (F[1]*tempf[i]/B[1]) 
    	- (G[1]*tempg[i]/B[1]) - (H[1]*temph[i]/B[1]) - (I[1]*tempi[i]/B[1]) - (J[1]*tempj[i]/B[1]);
    	tempc[i+1] = (Z[2]/C[2]) - (B[2]*tempb[i]/C[2]) - (A[2]*tempa[i]/C[2]) - (D[2]*tempd[i]/C[2]) - (E[2]*tempe[i]/C[2]) - (F[2]*tempf[i]/C[2]) 
    	- (G[2]*tempg[i]/C[2]) - (H[2]*temph[i]/C[2]) - (I[2]*tempi[i]/C[2]) - (J[2]*tempj[i]/C[2]);
    	tempd[i+1] = (Z[3]/D[3]) - (B[3]*tempb[i]/D[3]) - (A[3]*tempa[i]/D[3]) - (C[3]*tempc[i]/D[3]) - (E[3]*tempe[i]/D[3]) - (F[3]*tempf[i]/D[3]) 
    	- (G[3]*tempg[i]/D[3]) - (H[3]*temph[i]/D[3]) - (I[3]*tempi[i]/D[3]) - (J[3]*tempj[i]/D[3]);
    	tempe[i+1] = (Z[4]/E[4]) - (B[4]*tempb[i]/E[4]) - (A[4]*tempa[i]/E[4]) - (C[4]*tempc[i]/E[4]) - (D[4]*tempd[i]/E[4]) - (F[4]*tempf[i]/E[4]) 
    	- (G[4]*tempg[i]/E[4]) - (H[4]*temph[i]/E[4]) - (I[4]*tempi[i]/E[4]) - (J[4]*tempj[i]/E[4]);
    	tempf[i+1] = (Z[5]/F[5]) - (B[5]*tempb[i]/F[5]) - (A[5]*tempa[i]/F[5]) - (C[5]*tempc[i]/F[5]) - (D[5]*tempd[i]/F[5]) - (E[5]*tempe[i]/F[5]) 
    	- (G[5]*tempg[i]/F[5]) - (H[5]*temph[i]/F[5]) - (I[5]*tempi[i]/F[5]) - (J[5]*tempj[i]/F[5]);
    	tempg[i+1] = (Z[6]/G[6]) - (B[6]*tempb[i]/G[6]) - (A[6]*tempa[i]/G[6]) - (C[6]*tempc[i]/G[6]) - (D[6]*tempd[i]/G[6]) - (E[6]*tempe[i]/G[6]) 
    	- (F[6]*tempf[i]/G[6]) - (H[6]*temph[i]/G[6]) - (I[6]*tempi[i]/G[6]) - (J[6]*tempj[i]/G[6]);
    	temph[i+1] = (Z[7]/H[7]) - (B[7]*tempb[i]/H[7]) - (A[7]*tempa[i]/H[7]) - (C[7]*tempc[i]/H[7]) - (D[7]*tempd[i]/H[7]) - (E[7]*tempe[i]/H[7]) 
    	- (F[7]*tempf[i]/H[7]) - (G[7]*tempg[i]/H[7]) - (I[7]*tempi[i]/H[7]) - (J[7]*tempj[i]/H[7]);
    	tempi[i+1] = (Z[8]/I[8]) - (B[8]*tempb[i]/I[8]) - (A[8]*tempa[i]/I[8]) - (C[8]*tempc[i]/I[8]) - (D[8]*tempd[i]/I[8]) - (E[8]*tempe[i]/I[8]) 
    	- (F[8]*tempf[i]/I[8]) - (G[8]*tempg[i]/I[8]) - (H[8]*temph[i]/I[8]) - (J[8]*tempj[i]/I[8]);
    	tempj[i+1] = (Z[9]/J[9]) - (B[9]*tempb[i]/J[9]) - (A[9]*tempa[i]/J[9]) - (C[9]*tempc[i]/J[9]) - (D[9]*tempd[i]/J[9]) - (E[9]*tempe[i]/J[9]) 
    	- (F[9]*tempf[i]/J[9]) - (G[9]*tempg[i]/J[9]) - (H[9]*temph[i]/J[9]) - (I[9]*tempi[i]/J[9]);
    	
    	printf("%.5f %.5f %.5f %.5f %.5f %.5f\n\n", tempa[i], tempb[i], tempc[i], tempd[i], tempe[i], tempf[i]);
    	printf("%.5f %.5f %.5f %.5f\n\n", tempg[i], temph[i], tempi[i], tempj[i]);
    	i++;
    	}
    }
    data2.TXT

    Code:
    -30 -11 47 -8 38 -45 26 7 11 18
    34 -36 43 39 13 48 40 -37 -39 -17
    -29 44 -7 32 25 21 -50 -45 27 15
    -23 42 -38 -33 1 -44 41 25 -36 -32
    -3 -39 -23 -20 -10 36 -16 -45 -32 16
    -31 -36 -41 0 -25 -23 37 24 -30 47
    43 34 -44 -26 5 -14 40 -50 -22 -12
    -49 -26 -38 34 35 -3 -7 6 50 -14
    31 49 44 -32 16 -6 46 -27 36 -15
    6 13 1 20 48 42 14 -3 -17 9
    DataB.txt:

    Code:
    10
    5
    6
    -30
    40
    -29
    -5
    0
    10
    7
    Last edited by elleshlar; 05-10-2011 at 04:29 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I dunno what you mean by "weird values" but this won't make it past the first newline, if there is one:

    Code:
    fscanf (pData, "%f ", &temp[i]);
    You should examine the return value of fscanf -- in fact, you should probably make use of it for error handling.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Code:
    	{
    
    	if (abs(i % 10) == 0)
    		A[i%10] = temp[i];
    	else if (abs(i % 10) == 1)
    		B[i%10] = temp[i];
    	else if (abs(i % 10) == 2)
    		C[i%10] = temp[i];
    	else if (abs(i % 10) == 3)
    		D[i%10] = temp[i];	
    	else if (abs(i % 10) == 4)
    		E[i%10] = temp[i];
    	else if (abs(i % 10) == 5)
    		F[i%10] = temp[i];
    	else if (abs(i % 10) == 6)
    		G[i%10] = temp[i];
    	else if (abs(i % 10) == 7)
    		H[i%10] = temp[i];
    	else if (abs(i % 10) == 8)
    		I[i%10] = temp[i];
    	else if (abs(i % 10) == 9)
    	{
    		J[i%10] = temp[i];
    	}
    1) Surely this is what switch statements were invented for.

    2) Don't control loops with feof()
    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    4
    Hi there,

    Apparently the problem was that when I copied over the data2.txt from linux over to windows, it has broken the file (not sure, maybe added some kind of invisible code to the name?) , and so data2.txt could not be read. When I created a new file, the program was able to read the file.

    I'll look into the switch statements and modify the code accordingly, as well as modifying the feof function I use.

    I'll also, obviously, have to figure out some kind of loop for the last part of the calculations, it's very ugly right now.

    I got some extra days to hand it in, so I'll try and work the kinks and I'll get back to you folks as needed.

    Thank you for your replies.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Sounds good. Keep working on it and aim to finish well before the deadline. There is no better feeling than having a good time and going to bed at your convenience the night before the deadline when slackers are chugging red bull and coffee. Post here any specific questions you have.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    This program screams: Use two dimensional arrays!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    978
    Quote Originally Posted by iMalc View Post
    This program screams: Use two dimensional arrays!
    Definitely, also use Fortran instead.

    Also, sounds like the OP is doing a homework assignment. If not, Jacobi/Gauss-Seidel are poor choices to solve matrices and will only converge for well-conditioned matrices. Successive over-relaxation (SOR) is like Gauss-Seidel but exhibits better convergence. There are a lot of options, but Jacobi/Gauss-Seidel are probably the last ones you want to use.
    Last edited by Epy; 05-10-2011 at 06:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jacobi algorithm problem... could wikipedia be wrong?
    By somekid413 in forum C Programming
    Replies: 2
    Last Post: 01-19-2010, 01:26 AM
  2. Iteration?
    By Aliaks in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2009, 12:17 AM
  3. jacobi iterative residue
    By burninfrost296 in forum C Programming
    Replies: 1
    Last Post: 03-05-2009, 09:27 AM
  4. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 06:25 AM
  5. Iteration help please
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21