Gauss Elimination Method

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Gauss Elimination Method:

In[ ]:= a = {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}}


Out[ ]= {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}}

In[ ]:= a[[2, 3]]


Out[ ]= 10

Out[ ]= {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}}

In[ ]:= a // MatrixForm


Out[ ]//MatrixForm=
1 2 3
2 6 10
3 14 28

In[ ]:= Take[a, 2] // MatrixForm


Out[ ]//MatrixForm=

1 2 3 
2 6 10

In[ ]:= Take[a, {1, 2}]


Out[ ]= {{1, 2, 3}, {2, 6, 10}}

In[ ]:= Take[a, {2, 3}]


Out[ ]= {{2, 6, 10}, {3, 14, 28}}

Solve the following system of equations (without partial pivoting)


x1 + 2 x2+ 3 x3 = 1
2 x1 + 6 x2 + 10 x3 = 0
3 x1 + 14 x2 + 28 x3 = -8

In[ ]:= A = {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}};

In[ ]:= A // MatrixForm


Out[ ]//MatrixForm=
1 2 3
2 6 10
3 14 28

In[ ]:= X = {x1, x2, x3};

In[ ]:= X // MatrixForm


Out[ ]//MatrixForm=
x1
x2
x3

In[ ]:= X[[1]]


Out[ ]= x1

In[ ]:= b = {{1}, {0}, {- 8}};


2 GaussEliminationMethod.nb

In[ ]:= b // MatrixForm


Out[ ]//MatrixForm=
1
0
-8

In[ ]:= Flatten[{{1}, {0}, {- 8}}]


Out[ ]= {1, 0, - 8}

In[ ]:= aug = ArrayFlatten[{{A, b}}];

In[ ]:= aug // MatrixForm


Out[ ]//MatrixForm=
1 2 3 1
2 6 10 0
3 14 28 - 8

In[ ]:= aug[[2]] = aug[[2]] - 2 aug[[1]];


aug[[3]] = aug[[3]] - 3 aug[[1]];

In[ ]:= aug // MatrixForm


Out[ ]//MatrixForm=
1 2 3 1
0 2 4 -2
0 8 19 - 11

In[ ]:= aug[[3]] = aug[[3]] - 4 aug[[2]];

In[ ]:= aug // MatrixForm


Out[ ]//MatrixForm=
1 2 3 1
0 2 4 -2
0 0 3 -3

In[ ]:= upper = Take[aug, All, 3];

In[ ]:= upper // MatrixForm


Out[ ]//MatrixForm=
1 2 3
0 2 4
0 0 3

In[ ]:= c = Take[aug, All, - 1];

In[ ]:= c // MatrixForm


Out[ ]//MatrixForm=
1
-2
-3

In[ ]:= Solve[upper.X == c]


Out[ ]= {{x1 → 2, x2 → 1, x3 → - 1}}
GaussEliminationMethod.nb 3

Solve the above question with partial pivoting.

In[ ]:= A = {{1, 2, 3}, {2, 6, 10}, {3, 14, 28}};


A // MatrixForm
Out[ ]//MatrixForm=
1 2 3
2 6 10
3 14 28

In[ ]:= X = {x1, x2, x3};


X // MatrixForm
Out[ ]//MatrixForm=
2
1
-1

In[ ]:= B = {{1}, {0}, {- 8}};


B // MatrixForm
Out[ ]//MatrixForm=
1
0
-8

In[ ]:= aug = ArrayFlatten[{{A, B}}];


aug // MatrixForm
Out[ ]//MatrixForm=
1 2 3 1
2 6 10 0
3 14 28 - 8

In[ ]:= Max[Abs[Take[aug, All, 1]]]


Out[ ]= 3

In[ ]:= row1 = aug[[1]];


aug[[1]] = aug[[3]];
aug[[3]] = row1;

In[ ]:= aug // MatrixForm


Out[ ]//MatrixForm=
3 14 28 - 8
2 6 10 0
1 2 3 1

In[ ]:= aug[[2]] = aug[[2]] - aug[[2, 1]]  aug[[1, 1]] aug[[1]];


aug[[3]] = aug[[3]] - aug[[3, 1]]  aug[[1, 1]] aug[[1]];
aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 - 8
0 - 10 - 26 16
3 3 3
0 -8 - 19 11
3 3 3

In[ ]:= Max[Abs[Take[aug, - 2, {2}]]]


10
Out[ ]=
3
4 GaussEliminationMethod.nb

In[ ]:= aug[[3]] = aug[[3]] - aug[[3, 2]]  aug[[2, 2]] aug[[2]];


aug // MatrixForm
Out[ ]//MatrixForm=
3 14 28 -8
0 - 10 - 26 16
3 3 3
3
0 0 -3
5 5

In[ ]:= upper = Take[aug, All, 3];


upper // MatrixForm
Out[ ]//MatrixForm=
3 14 28
0 - 10 - 26
3 3
3
0 0
5

In[ ]:= c = Take[aug, All, - 1];


c // MatrixForm
Out[ ]//MatrixForm=
-8
16
3
-3
5

In[ ]:= m = Solve[upper.X == c]


Out[ ]= {{}}

In[ ]:= x1 = x1 /. m[[1]];


x2 = x2 /. m[[1]];
x3 = x3 /. m[[1]];
X
Out[ ]= {2, 1, - 1}

In[ ]:= A.X


Out[ ]= {1, 0, - 8}

You might also like