Gauss Elimination Method
Algorithm:Gauss Elimination Method GaussElimination(a,n) { Here a denotes the coefficient matrix and n is the size of the matrix. for(k=0 to k<n) { for(r=k+1 to r<=n-1) { m=a[r][k]/a[k][k] for(c=0 to c<=n-1) { a[r][c]=a[r][c]-m*a[k][c] } } } x[n-1]=a[n-1][n]/a[n-1][n-1] // Here x is the constant matrix for(i=n-2 to igt;=0) { sum=0 for(j=i+1 to j<=n-1) { sum=sum+a[i][j]*x[j] } x[i]=(a[i][n]-sum)/a[i][i] } for(i=0 to i<n) print(x[i]) // Prints the roots } C Program For Gauss Elimination Method Source Code: #include<stdio.h> #include<conio.h> #include<alloc.h> 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_eli(float **A,int n) { int k,i,j,row,col,n1; float m,sum,X[30]; n1=n; for(k=1;k<=n-1;k++) { for(row=k+1;row<=n;row++) { m=(A[row][k])/(A[k][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("--------------------------------------"); } X[n]=(A[n][n+1])/(A[n][n]); printf("\nLast step:\n"); show(A,n1); for(i=n-1;i>=1;i--) { sum=0; for(j=i+1;j<=n;j++) { sum=sum+A[i][j]*X[j]; } X[i]=(A[i][n+1]-sum)/(A[i][i]); } for(i=1;i<=n;i++) { printf("\nX%d=%g",i,X[i]); } } 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); scanf("%f",&A[i][j]); } } gauss_eli(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: 2 -3 4 8 0 2.5 2 11 0 8.5 -7 -4 -------------------------------------- Step 2: 2 -3 4 8 0 2.5 2 11 0 -2.38419 -13.8 -41.4 -------------------------------------- Last step: 2 -3 4 8 0 2.5 2 11 0 -2.38419 -13.8 -41.4 X1=1 X2=2 X3=3
Tags:
Linear
0 comments