naive gaussian elimination program not working

Discussion in 'C' started by Shae, Dec 4, 2007.

  1. Shae

    Shae New Member

    Joined:
    Sep 30, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    ASP.NET web developer
    Location:
    Indianapolis
    Home Page:
    http://www.shaesplace.com/blog
    Hey all,

    I'm trying to write this exceedingly simple-looking naive Gaussian elimination program, but it isn't working. I got the algorithm right out of the math book. It even looks a lot like other working examples I've seen.

    It appears that it gives me the the correct values in the upper-right triangle, but it doesn't give me the zeros in the lower-left. I've even tried a couple of other algorithms and still had trouble. Oh, and I know that the line for operation on the vector B is missing. Just trying to get one step at a time.

    Can anyone give me any idea what I'm doing wrong?

    thanks!


    Code:
    /* Naive Gaussian Elimination */
    
    
    
    /* includes */
    
    #include<math.h>
    
    #include<stdlib.h>
    
    #include<stdio.h>
    
    /* ********************* */
    
    /* global variables */
    
    
    // I know, global variables are bad :/
    
    // two-dimensional array for the default 'A' array
    // zeros make array indexes match up w/matrix ones
    double A[8][8] = { { 0, 0, 0, 0, 0, 0, 0 }, 
    		{ 0, 0.1897, 0.3784, 0.6449, 0.7271, 0.4449, 0.1730, 0.0118 }, 
    		{ 0, 0.1934, 0.8600, 0.8180, 0.3093, 0.6946, 0.9797, 0.8939 }, 
    		{ 0, 0.6822, 0.8537, 0.6602, 0.8385, 0.6213, 0.2714, 0.1991 },
    		{ 0, 0.3028, 0.5936, 0.3420, 0.5681, 0.7948, 0.2523, 0.2987 },
    		{ 0, 0.5417, 0.4966, 0.2897, 0.3704, 0.9568, 0.8757, 0.6614 },
    		{ 0, 0.1509, 0.8998, 0.3412, 0.7027, 0.5226, 0.7373, 0.2844 },
    		{ 0, 0.6979, 0.8216, 0.5341, 0.5466, 0.8801, 0.1365, 0.4692 } };
    
    /* another known problem for debugging */
    // zeros make array indexes match up w/matrix ones
    /* double A[8][8] = { { 0, 0, 0, 0, 0, 0, 0 }, 
    		{ 0, 6, -2, 2, 4, 0, 0, 0 }, 
    		{ 0, 12, -8, 6, 10, 0, 0, 0 }, 
    		{ 0, 3, -13, 9, 3, 0, 0, 0 },
    		{ 0, -6, 4, 1, -18, 0, 0, 0},
    		{ 0, 0, 0, 0, 0, 0, 0 },
    		{ 0, 0, 0, 0, 0, 0, 0  },
    		{ 0, 0, 0, 0, 0, 0, 0  } }; */
    
    // one-dimensional array for default vector 'b'
    double b[8] = { 0, 0.5000, -0.3000, -0.2000, 0.7000, -0.9000, 0.3000, -0.1000 };
    
    
    /* ********************* */
    
    /* Main */
    int main(){
    
    /* variables */
    char overwrite;
    
    /* prototypes */
    void printArray();
    void printVector();
    void performGaussianElimination();
    
    /* Say hello */
    
    printf("\n\n\nThis program will solve a system of equations with Naive Gaussian Elimination. \n\n");
    
    printf("The default matrix used will be: ");
    printArray();
    
    printf("\n\nThe default vector used will be: ");
    printVector();
    
    performGaussianElimination();
    
    printf("\nThe matrix after forward elimination is:");
    printArray();
    
    return 0;
    }
    
    /* ********************* */
    /* Print Array Function (working correctly) */
    
    void printArray(){
    int i,j;
    
    
    printf("\n");
    
       // loop through rows
       for(i=1; i <=7; i++){
    	
    	// output column values
    	for(j=1; j<=7; j++) {
    	printf("%lf   ", A[i][j]);
    	} // end inner 'for'
    
    	printf("\n");
       } // end outer 'for'
    
    return; 
    }
    
    /* ********************* */
    /* Print Vector Function */
    void printVector(){
    int j;
    
    
    printf("\n");
    
    	// output column values
    	for(j=1; j<=7-1; j++) {
    	printf("%lf   ", b[j]);
    	} // end inner 'for'
    
    	printf("\n");
    
    return;
    }
    
    /* ********************* */
    /* Perform Gaussian Elimination Function */
    
    void performGaussianElimination(){
    int i,j,k,m;
    double xmult;
    
    // forward elimination
    
    
     for(k=1; k<7; k++)
     {
      for(i=k+1; i<=7; i++){
         if(A[k][k]!=0){
         xmult = A[i][k]/A[k][k];
         }
         A[i][k] = xmult;
         for(j=k+1; j<=7; j++){
           A[i][j] = A[i][j] - xmult * A[k][j];
         }
       }
     }
    
    // do backward substitution, not done yet
    
    return;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have submitted the above thread as an article and I have moved to forum for discussion. Remember to click on New Thread button rather than Submit Article button.
     
  3. Shae

    Shae New Member

    Joined:
    Sep 30, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    ASP.NET web developer
    Location:
    Indianapolis
    Home Page:
    http://www.shaesplace.com/blog
    Oops. Sorry.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice