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

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    Read coefficients of linear equations from file and use jacobi method

    Hi,

    I did a function that get inputs of equations from user but I am trying to get from a 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();
    }

    can anyone help me how the function read the linear equation from file. assume the file has:
    6x + 2y -z = 5
    -2x + 4y + z = 8
    3x + y + 7z = 3

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    jacobi function

    Code:
    void jacobiCalcDisplay( int numUnKnowns, double** mat ){
        int* flag;
        int i, j, counter = 0;
        double* res;
        double* var = (double*) malloc( numUnKnowns*sizeof(double) );
        res = (double*) malloc( numUnKnowns*sizeof(double) );
        flag = (int*) malloc( numUnKnowns*sizeof(int) );
    
    
        for(i = 0 ; i < numUnKnowns ; i++ )
        var[i] = res[i] = flag[i] = 0;
        printf("The initial value of each array element was set to zero ...\n\n");
    
    
        printf( "*********************\n");
        printf( "START CALCULATING ...\n");
        printf( "*********************\n");
    
    
        do
        {
    	counter++;
    	/* for each iteration keep a copy of the old results ... */
    	for(i = 0 ; i < numUnKnowns ; i++ )
    	{
    	    var[i] = res[i];
    	}
    
    
    	if( SHOW_ITERS ) printf("\nIteration number %d ...\n", counter);
    
    
    	for(i = 0 ; i < numUnKnowns ; i++ ) /* calculation */
    	{
    	    res[i] = mat[i][numUnKnowns];
    	    for(j = 0 ; j < numUnKnowns ; j++ )
    		res[i] = res[i] - mat[i][j]*var[j] ;
    
    
    	    res[i] = res[i] + mat[i][i]*var[i] ;
    	    res[i] = res[i] / mat[i][i] ;
    	    if( SHOW_ITERS ) printf("%c = %f\n", 'a'+i, res[i]);
    	    if( fabs(res[i] - var[i]) < EPSILON ) /* stop condition */
    	    flag[i]++;
    
    
    	    if( counter==MAX_PRINT_ITERS) SHOW_ITERS = 0;
    	}
    
    
        }while( !checkFlags( numUnKnowns, flag ) );
    
    
        printf( "\n********************************\n");
        printf( "The RESULTS of %d ITERATIONS ... \n", counter);
        printf( "********************************\n");
    
    
        /*  cross check ...*/
    
    
        for( i = 0 ; i < numUnKnowns ; i++)
    	var[i] = dotProd( numUnKnowns, mat[i], res );
    
    
        showXcheck( numUnKnowns, mat, res, var );
    
    
        /* show sol'n vector (again) ... and free up all dynamic memory  */
    
    
        printf("\nSolution vector ...\n");
        for( i = 0 ; i < numUnKnowns ; i++)
        {
    	printf("%c = %+f\n", 'a'+i, res[i]);
    	free(mat[i]);
        }
        free( mat );
        free( flag );
        free( res );
        free( var );
    }

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I did a function that get inputs of equations from user but I am trying to get from a file
    No, you found some code that approximates what you need, and now, because you're nothing but a scoop and poop programmer, you're looking for us to give you the code to give you exactly what you need. Pretty sad.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    sorry, I did my assignment by myself
    I can post all my work.
    Am asking how can read from file not asking do that.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Read coefficients of linear equations from file and use jacobi method is bad enough, and a dupe thread...

    Also, you copied it all out of New thread especially for students of C and C++, don't be a ........ing liar.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by memcpy View Post
    Also, you copied it all out of New thread especially for students of C and C++, don't be a ........ing liar.
    Please do try to contain yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-16-2012, 12:34 PM
  2. Jacobi Iteration Method
    By elleshlar in forum C Programming
    Replies: 6
    Last Post: 05-10-2011, 05:32 PM
  3. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  4. LINEAR EQUATIONS please help
    By apple_ranger in forum C Programming
    Replies: 18
    Last Post: 09-08-2008, 05:49 AM
  5. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM