Upload
chase-williamson
View
36
Download
0
Embed Size (px)
DESCRIPTION
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 . - PowerPoint PPT Presentation
Citation preview
SNPLSNPL 11
GAUSS ELIMINATION GAUSS ELIMINATION
& BACK & BACK SUBSTITUTIONSUBSTITUTION
Dayun YuDayun YuSeungmuk JiSeungmuk Ji
SNPLSNPL 22
Gauss EliminationGauss EliminationGauss EliminationGauss 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
nnnnnn
n
n
b
b
b
x
x
x
aaa
aaa
aaa
2
1
2
1
21
22221
11211
nnnn
n
n
b
b
b
x
x
x
a
aa
aaa
2
1
2
1
222
11211
00
0b x A
SNPLSNPL 33
Back SubstitutionBack SubstitutionBack SubstitutionBack Substitutionann 0, We must have
We now know xn, the second last equation
Similarly,
In general
nn
nn ab
x
1,1
,11
1
1,11,111,1 0,
nn
nnnn
n
nnnnnnnnn
a
xabx
abxaxa
0, 2,2
2,2
,211,22
2
nn
nn
nnnnnnn
n aa
xaxabx
0,1
kk
kk
n
kjjkjk
k aa
xabx
SNPLSNPL 44
Define matrix Define matrix WWDefine matrix Define matrix WW
Define matrix W in the system Ax = b
nnnn
n
n
baa
baa
baa
1
2221
1111
W
SNPLSNPL 55
PivotingPivotingPivotingPivoting
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).
ki max
nki pi aa i p
SNPLSNPL 66
ExampleExampleExampleExampleEx)
132
3344
532
321
2321
1321
xxx
pxxx
pxxx
15500
7120
5132
6260
7120
5132
1132
3344
5132
21pp
W
SNPLSNPL 77
Algorithm 1Algorithm 1Algorithm 1Algorithm 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 .
kpiw
kp
kp
k
i
w
w
jpjpjp kii
mwww
SNPLSNPL 88
Algorithm 2Algorithm 2Algorithm 2Algorithm 2(Back substitution) For k = n, n-1,∙∙∙,1 , do;
calculate .kk
n
kj jkjk
k a
xabx
1
SNPLSNPL 99
Programming with CProgramming with CProgramming with CProgramming 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]; } }
SNPLSNPL 1010
Programming with CProgramming with CProgramming with CProgramming with Cprintf("\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);
}
SNPLSNPL 1111
ResultResultResultResult
***** 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
SNPLSNPL 1212
General program with CGeneral program with CGeneral program with CGeneral 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"); }}
SNPLSNPL 1313
General program with CGeneral program with CGeneral program with CGeneral program with Cvoid 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]);}