5 Replies - 17292 Views - Last Post: 25 March 2009 - 04:50 AM Rate Topic: -----

#1 shwetha03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-March 09

jacobi iterative method

Posted 23 March 2009 - 05:29 AM

hiiiii ppl dis is the code fr jacobi iterative method...... and working without any errors with correct output :)

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
	float a[20][20],x[20],e,big,temp,relerror,sum;
	int n,i,j,maxit,itr;
	char ch;
	clrscr();
	printf("\n\nENTER THE SIZE OF THE EQUATION :: ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("\n\nEnter the coefficints of  equation %d and RHS \n",i);
		for(j=1;j<=n+1;j++)
		scanf("%f",&a[i][j]);
	}
	printf("\n\nEnter relative error and number of iteration ::  \n");
	scanf("%f%d",&e,&maxit);
	for(i=1;i<=n;i++)
	x[i]=0;
	for(itr=1;itr<=maxit;itr++)
	{
		big=0;
		for(i=1;i<=n;i++)
		{
			sum=0;
			for(j=1;j<=n;j++)
			 {
			   if(i!=j
			   )
			      sum=sum+a[i][j]*x[j];
			  }
			temp=(a[i][n+1]-sum)/a[i][i];
			relerror=fabs((x[i]-temp)/temp);
			if(relerror>big)
			   big=relerror;
			x[i]=temp;
		}
	if(big<=e)
	{
		printf("Converges to a solution in %d iterations\n",itr);
		for(i=1;i<=n;i++)
		printf("\n%.4f\t",x[i]);
		getch();
		exit(1);
	}

     }
  printf("does not converge in %d iteration \n",maxit);
  getch();

 }


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 23 March 2009 - 07:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: jacobi iterative method

#2 Pseudocrat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 22-March 09

Re: jacobi iterative method

Posted 23 March 2009 - 05:32 AM

I think you may have forgotten to enter the code or attach the file. ... and possibly a few consonants and vowels.

This might belong in snippets as well.

:blink:

Ah, very nice, but you should probably heed this -> :code:

This post has been edited by Pseudocrat: 23 March 2009 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: jacobi iterative method

Posted 23 March 2009 - 05:50 AM

function main should return an int, which is 0 in case of success !
Was This Post Helpful? 1
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: jacobi iterative method

Posted 23 March 2009 - 07:58 AM

Did you have a question regarding this code?
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: jacobi iterative method

Posted 24 March 2009 - 11:10 PM

After looking at you code some ... I decided to research a little and found some more promising code on the web at Programmer's Heaven ... that was claimed by AlexTan, Segamat Baru 85000, Segamat, Johor Malaysia.

It needed 2 major corrections (that I could find)... and after some editing and enhancements ... here is the revised code that uses all dynamic arrays ... with a built in x-check to SEE if the cal's are reasonably correct.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

/* globals ... */

#define HEADER "**********************************************************\n" \
               "             JACOBI ITERATION SOLUTION METHOD             \n" \
               "    (for strictly diagonally dominant square matrices)    \n" \
               "**********************************************************\n"
#define EPSILON 0.0000001
#define MAX_PRINT_ITERS 333

int SHOW_ITERS = 0; /* defaults to NO ... */

/* input matrix elements */
void getInput( int, double** mat );
/* check if diagonally dominant */
int checkIfDD( int, double** mat );
/* calculate and display answers */
void jacobiCalcDisplay( int, double** mat );

double dotProd( int num, double* v1, double* v2 );

void showXcheck( int num, double** mat, double* v1, double* v2 );

int checkFlags( int num, int flags[] );

/* free dynamic memory occupied by 2D square matrix */
void freeMat( int num, double** sqMat );

int more()/* defaults to yes ... */
{
    int c, reply ;
    printf("\nMore ... (y/n) ? ");
    reply = c = getchar();
    while( c!='\n' ) c=getchar(); /* flush stdin ... */
    return !(reply=='n' || reply=='N');
}



int main()
{
	double **mat;
	int dummy, num;

    do
    {
        puts( HEADER );
    	
        printf( "Show each iteration result 1=Yes, 0=No ? " );
        scanf("%c", &SHOW_ITERS);
        dummy = SHOW_ITERS; /* get a backup copy ... */
        while( dummy != '\n' ) dummy = getchar(); /* flush stdin ... */
        
        if( !(SHOW_ITERS=='1'||SHOW_ITERS=='y'||SHOW_ITERS=='Y') )
        {
            puts("Ok ... 'No' it is ...");
            SHOW_ITERS =0;
        }
        else puts("Ok ... 'Yes' show iterations ... (This may take some time.)");


    	printf("\nTo solve a system of linear equations ...\n");
    	printf("Enter the number of 'equations/unknowns' to find: ");
    	num = 0;
    	scanf("%d", &num); /* num of unknowns */
        while( getchar() != '\n' ) ; /* flush stdin ... */
        
    	mat = NULL;
    	if( num>0 && (mat = (double**) malloc(num*sizeof(double*))) )
    	{
    		getInput(num, mat);
    		if( checkIfDD(num, mat) ) jacobiCalcDisplay(num,mat);
    		else freeMat(num, mat);
    	}
    	else
    	{
    		puts("Invalid choice or memory available was exceeded ... Try again.");
    		if( mat != NULL ) free( mat );
    	}
    }while( more() );
    return 0;
}


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();
}

/* Check if the matrix entered is strictly diagonally dominant */
int checkIfDD( int numUnKnowns, double** mat )
{
	int   m, n, dd = 0;
    double* chkdd;
	double* sumdd = (double*) malloc( numUnKnowns*sizeof(double) );
	chkdd = (double*) malloc( numUnKnowns*sizeof(double) );

    for( m = 0 ; m < numUnKnowns ; m++ )
        chkdd[m] = sumdd[m] = 0; /* all set to zero ... */
	
	printf("\nChecking if the matrix is (strictly) diagonally dominant...");

	for( m = 0 ; m < numUnKnowns ; m++ )
	{
		for( n = 0 ; n < numUnKnowns ; n++ )
		{
			sumdd[m] += fabs(mat[m][n] );
		}
		sumdd[m] -= fabs(mat[m][m]);
		chkdd[m] = fabs(mat[m][m]);
		
		if(chkdd[m] > sumdd[m])
		{
			printf("\n%f > %f",chkdd[m],sumdd[m]);
			dd++;
		}
		else
			printf("\n%f !> %f",chkdd[m],sumdd[m]);

	}
	if(dd == numUnKnowns)
	{
		printf
        (
            "\nYES ..."
            "\nThe matrix is (strictly) diagonally dominant."
            "\nPress 'Enter' to continue with 'Jacobi Iterative Method'...\n"
        );
        getchar();
    }
	else
	{
        printf
        (
            "\nNO ..."
            "\nThe matrix is NOT (strictly) diagonally dominant ... so STOP!"
            "\n(But ... consider exchanging rows in the matrix "
            "and then to try again.)"

            "\n\nPress 'Enter' to continue ... "
        );
        getchar();
        free( sumdd );
        free( chkdd );
        return 0; /* false */
	}
	
	free( sumdd );
	free( chkdd );
	return 1; /* true */
}

/* uses global SHOW_ITERS ... */
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 ...*/
    
    //puts("\nCross checking ... Matrix times result vector = ");
    for( i = 0 ; i < numUnKnowns ; i++)
	{
        var[i] = dotProd( numUnKnowns, mat[i], res );
        //printf("%f =? %f\n", var[i], mat[i][numUnKnowns]);
    }
    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 );
}

int checkFlags( int num, int flags[] )
{
    int i;
    for( i=0; i<num; ++ i)
        if( flags[i] == 0 ) return 0;
    return 1;
}

double dotProd( int num, double* v1, double* v2 )
{
    int i;
    double sum =0;
    for( i=0; i<num; ++i ) sum += v1[i]*v2[i];
    return sum;
}

void showXcheck( int num, double** mat, double* v1, double* v2 )
{
    int i, j;
    puts("\nCross checking ... \nMatrix times sol'n vector ="
         " cal. vector vs. original RHS vector");
    for( i = 0 ; i < num ; i++)
	{
        printf("|");
        for( j =0 ; j < num ; j++ )
            printf("%+9f ", mat[i][j] );
        printf("| |%+9f| |%+9f|vs|%+9f|\n", v1[i], v2[i], mat[i][num]);
    }
}


void freeMat( int num, double** sqMat )
{
    int i;
    for( i=num-1; i>=0; --i )
        free( sqMat[i] );
    free( sqMat );
}

Was This Post Helpful? 1
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: jacobi iterative method

Posted 25 March 2009 - 04:50 AM

You may like to see this Gauss-Siedel method also ...

http://www.dreaminco...wtopic64817.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1