170 likes | 418 Views
GAUSS ELIMINATION & BACK SUBSTITUTION. Dayun Yu Seungmuk Ji. Gauss Elimination. n n matrix : A , n-vector : x and b , Ax = b The solution vector x can be obtained without difficulty in case A is upper-triangular with all diagonal. Ax = b .
E N D
GAUSS ELIMINATION & BACK SUBSTITUTION Dayun Yu Seungmuk Ji
Gauss Elimination nn matrix : A , n-vector : x and b, Ax = b The solution vector x can be obtained without difficulty in case A is upper-triangular with all diagonal. Ax = b
Back Substitution ann 0, We must have We now know xn, the second last equation Similarly, In general
Define matrix W Define matrix W in the system Ax = b
Pivoting Partial-pivoting is interchanging row. ☞ If first column(a11=0) of first row is zero, we have to interchange other row . Full-pivoting is interchanging row and columns. ☞ If is satisfy, we must find . So we execute interchanging row ( row i row p).
Example Ex)
Algorithm 1 (Upper-triangular) Initialize the n-vector p to have pi = i, i = 1,,n. For k = 1,∙∙∙,n-1, do; For i = k+1, ∙∙∙,n , do; set m and , m equal to . For j = k+1,∙∙∙, n+1, do; set .
Algorithm 2 (Back substitution) For k = n, n-1,∙∙∙,1 , do; calculate .
Programming with C #include <stdio.h> #include <math.h> #include <conio.h> main() { int i, j, k, t; int n, N; float m, s; float w[3][4]={{2,3,-1, 5},{4,4,-3, 3},{-2,3,-1, 1}}; float p[3]={0.0}; clrscr(); n = 3; N = 4; printf("\n\n***** Matrix W ****\n\n"); for(i = 0; i < n; i++) { for(j = 0; j < N; j++) printf("%10.3f", w[i][j]); printf("\n"); p[i] = i; } for(k = 0; k < n-1; k ++) { for(j = k; j < N; j++) { if(j == n) { printf(" W is not invertible ! "); break; } else if(w[p[j]][k] == 0) continue; else { for(t = k; t < N; t++) { s = w[p[j]][t]; w[p[j]][t] = w[p[k]][t]; w[p[k]][t] = s; } } break; } for(i = k+1; i < n; i++) { q = w[p[i]][k] / w[p[k]][k]; for(j = k; j < N; j++) w[p[i]][j] = w[p[i]][j] - q*w[p[k]][j]; } }
Programming with C printf("\n\n"); for(i = 0; i < n; i++) { for(j =0; j < N; j++) printf("%10.3f", w[p[i]][j]); printf("\n"); } } getch(); return(0); }
Result ***** Matrix W **** 2.000 3.000 -1.000 5.000 4.000 4.000 -3.000 3.000 -2.000 3.000 -1.000 1.000 2.000 3.000 -1.000 5.000 0.000 -2.000 -1.000 -7.000 0.000 0.000 -5.000 -15.000
General program with C #include <stdio.h> #include <conio.h> #include <math.h> #include <stdlib.h> #define MAXROW 20 typedef struct { int Row,Column; double Element[MAXROW][MAXROW+1]; } Matrix; Matrix M; void gainp(int a, int b); void gapiv(int row, int i); void gacal(int row, int col); int main() { int a,b; clrscr(); printf(“Row is :”); scanf(“%2d”,&a); printf(“Column is :”); scanf(“%2d”,&b); gainp(a,b); gacal(a,b); getch(); return 0; } void gainp(int a, int b) { int i,j; double tmp; printf(“\n”); printf("Enter the matrix (%2d,%2d) \n",a,b); for(i=0;i<a;i++) { for(j=0;j<b;j++){ printf("Enter for element (%2d,%2d) :", i+1,j+1); scanf(" %lf",&tmp); M.Element[i][j]=tmp; } printf("\n"); } }
General program with C void gapiv(int row, int i) { int j,ii; double tmp; for(ii=i+1;ii<row;ii++){ if(M.Element[ii][i]!=0.0){ for(j=i;j<row+1;j++){ tmp=M.Element[i][j]; M.Element[i][j]=M.Element[ii][j]; M.Element[ii][j]=tmp; } break; } } } void gacal(int row, int col) { int i,j,k, N; double div, sum, X[MAXROW]; for(i=0;i<row;i++){ if(M.Element[i][i]==0.0) gapiv(row,i); for(j=i+1;j<row;j++){ div=M.Element[j][i]/M.Element[i][i]; for(k=0;k<col;k++) M.Element[j][k]-=M.Element[i][k]*div; } } N=row; X[N-1]=M.Element[N-1][N]/M.Element[N-1][N-1]; for(i=N-2;i>=0;i--){ sum=0.0; for(j=i+1;j<N;j++) sum += M.Element[i][j]*X[j]; X[i]=(M.Element[i][N]-sum)/M.Element[i][i]; } printf("Solution is :\n"); for(i=0;i<N;i++) printf(" X%-d = %f\n",i+1,X[i]); }