Gauss Jordan Method
Algorithm Of Gauss-Jordan Method GaussJordan(a,n) { Here a denotes the coefficient matrix and n is the size of the matrix for(k=0 to k<n) { for(c=n to c>=0) { a[k][c]=a[k][c]/a[k][k] } for(r=0 to r<n) { if(k!=r) { m=a[r][k] for(col=0 to col<=n) a[r][col]=a[r][col]-m*a[k][col] } } } for(i=0;i<n;i++) print(a[i][n]) // Prints the roots } C Program For Gauss Jordan Method Source Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> linkfloat() { float a,*b; b=&a; a=*b; } void show(float **A,int n1) { int i,j; for(i=1;i<=n1;i++) { for(j=1;j<=n1+1;j++) { printf("\t%g",A[i][j]); } printf("\n"); } } void gauss_jordan(float **A,int n) { int k,i,j,row,col,n1; float m; n1=n; for(k=1;k<=n;k++) { for(col=n+1;col>=1;col--) { A[k][col]=A[k][col]/A[k][k]; } for(row=1;row<=n;row++) { if(k!=row) { m=A[row][k]; for(col=1;col<=n+1;col++) { A[row][col]=A[row][col]-m*A[k][col]; } } } printf("\nStep %d:",k); show(A,n1); printf("--------------------------------------"); } printf("\nLast step:\n"); show(A,n1); for(i=1;i<=n;i++) { printf("\nX%d=%g",i,A[i][n+1]); } } void main() { int n,r,c,j,i; float **A; clrscr(); printf("\nEnter number of variables:"); scanf("%d",&n); r=n+1; c=n+2; A=(float **)malloc(sizeof(float *)*r); for(i=1;i<=r;i++) { A[i]=(float *)malloc(sizeof(float)*c); } for(i=1;i<=n;i++) { for(j=1;j<=n+1;j++) { printf("\nEnter term %d of equation %d:",j,i); fflush(stdin); scanf("%f",&A[i][j]); } } gauss_jordan(A,n); getch(); } OUTPUT : Enter number of variables:3 Enter term 1 of equation 1:2 Enter term 2 of equation 1:-3 Enter term 3 of equation 1:4 Enter term 4 of equation 1:8 Enter term 1 of equation 2:1 Enter term 2 of equation 2:1 Enter term 3 of equation 2:4 Enter term 4 of equation 2:15 Enter term 1 of equation 3:3 Enter term 2 of equation 3:4 Enter term 3 of equation 3:-1 Enter term 4 of equation 3:8 Step 1: 1 -1.5 2 4 0 2.5 2 11 0 8.5 -7 -4 -------------------------------------- Step 2: 1 0 3.2 10.6 0 1 0.8 4.4 0 0 -13.8 -41.4 -------------------------------------- Step 3: 1 0 0 1 0 1 0 2 0 0 1 3 -------------------------------------- Last step: 1 0 0 1 0 1 0 2 0 0 1 3 X1=1 X2=2 X3=3
Tags:
Linear
0 comments