Problem 1: Given the equations:
3x1 + 4x2 + 3x3 = 10
x1 + 5x2 - x3 = 7
6x1 + 3x2 + 7x3 = 15
(a) Solve the following system using Naive Gaussian Elimination method.
3x1 + 4x2 + 3x3 = 10
x1 + 5x2 - x3 = 7
6x1 + 3x2 + 7x3 = 15
Rewrite the system in matrix foam and solve it by naïve Gaussian Elimination method .
3 4 3 10
(1 5 −1 7 )
6 3 7 15
R1 / 3 → R1 (divide the 1st row by 3)
4 10
1 1
( 3 3)
1 5 −1 7
6 3 7 15
R2 - 1 R1 → R2 (multiply 1st row by 1 and subtract it from 2nd row); R3 - 6 R1 → R3 (multiply
1 row by 6 and subtract it from 3 row)
4 10
1 1
3 3
6
0 1 − 1
11
(0 −5 1 −5)
11 11
R2 / → R2 (divide the 2nd row by )
3 3
4 10
1 1
3 3
6
0 1 − 1
11
(0 −5 1 −5)
4 4
R1 - 3R2 → R1 (multiply 2nd row by 3 and subtract it from 1st row); R3 + 5 R2 → R3 (multiply
2nd row by 5 and add it to 3rd row)
19
1 0 2
11
6
0 1 − 1
11
19
(0 0 −
11
0)
−19 −19
R3 / → R3 (divide the 3rd row by )
11 11
19
1 0 2
11
6
0 1 − 1
11
(0 0 1 0)
19 19 6
R1 - 11R3 → R1 (multiply 3rd row by 11and subtract it from 1st row); R2 + 11R3 → R2 (multiply
6
3rd row by 11 and add it to 2nd row)
1 0 0 2
(0 1 0 1)
0 0 1 0
Answers:
x1 = 2
x2 = 1
x3 = 0
(b) Solve the following system using Naive Gaussian Elimination method with
partial pivoting
3x1 + 4x2 + 3x3 = 10
x1 + 5x2 - x3 = 7
6x1 + 3x2 + 7x3 = 15
Rewrite the system in matrix foam and solve it by naïve Gaussian Elimination with partial
pivoting method.
3 4 3 10
(1 5 −1 7 )
6 3 7 15
As we see from the matrix, the 3rd row is starting with greater number than other rows.
So, switch R1 and R3, New matrix become.
6 3 7 15
(1 5 −1 7 )
3 4 3 10
R1 / 6 → R1 (divide the 1st row by 6)
1 0.5 1.1667 2.5
(1 5 −1 7)
3 4 3 10
R2 - 1 R1 → R2 (multiply 1st row by 1 and subtract it from 2nd row); R3 – 3R1 → R3 (multiply
1st row by 3 and subtract it from 3rd row)
1 0.5 1.1667 2.5
(0 4.5 −2.1667 4.5)
0 2.5 −0.5 2.5
Now as we see from the matrix (4.5) in 2nd row and 2nd column is greater than (2.5). So,
there is no need for switching. Leave the matrix as it is and perform the next step.
Now, R2 / 4.5→ R2 (divide the 2nd row by 4.5)
1 0.5 1.1667 2.5
(0 1 −0.481 1 )
0 2.5 −0.5 2.5
R3 - 2.5 R2 → R2 (multiply 2nd row by 2.5 and subtract it from 3rd row)
1 0.5 1.1667 2.5
(0 1 −0.481 1 )
0 0 0.7025 0
As we solving by naïve Gaussian Elimination with partial pivoting method.
So, now write this matrix in the foam of equations.
1x1 + 0.5x2 + 1.1667x3 = 2.5
0x1 + x2 – 0.481x3 = 1
0x1 + 0x2 + 0.7025x3 = 0
By simultaneously solving these equations, we find the values of x1, x2, x3
x1 = 2
x2 = 1
x3 = 0
(c) Substitute your results into the original equations to check your answers
Make a check:
3x1 + 4x2 + 3x3 = 10
3(2) + 4(1) + 3(0) = 6 + 4 - 0 = 10
x1 + 5x2 - x3 = 7
2 + 5(1) - 0 = 2 + 5 + 0 = 7
6x1 + 3x2 + 7x3 = 15
6(2) + 3(1) + 7(0) = 12 + 3 - 0 = 15
Check completed successfully.
Answers:
x1 = 2
x2 = 1
x3 = 0
(d) Compare results to MATLAB functions Gauss Pivot and Gauss Naive
% guass Elimination methode
A=[3 4 3;1 5 -1;6 3 7]; % here we put the values of all eqations
b=[10;7;15]; % here we put the values of right hand side
% solve linea system Ax = b
% A is an n by n matrix
% %x is an n by 1 matrix i.e column matrix
[n,~] = size(A); % find size of matrix A which is also represents number
of variables
x = zeros(n,1); % intialize x
%code for forward elimination to convert the matrix A to uppar triangular
foam
for i = 1:n-1 % i=1 2 3
m = A(i+1:n,i)/A(i,i); %multipliers let for i=1,m= A(2,1)/(A(1,1))
A(i+1:n,:) = A(i+1:n,:)- m*A(i,:); %to make the element below diaagonals
as zero
b(i+1:n,:) = b(i+1:n,:)- m*b(i,:);
end
% code for baack substitution to find unknowns
x(n,:) = b(n,:)/A(n,n); % here we find the values of variables
for i = n-1:-1:1
x(i,:) = (b(i,:)-A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
Results:
X=[2,1,0]
x1 = 2
x2 = 1
x3 = 0
Problem 2: Given the equations:
8x1 + x2 - x3 = 8
-x1 + 7x2 - 2x3 = 4
2x1 + x2 + 9x3 = 12
(a) Solve the following system using Naive Gaussian Elimination method.
8x1 + x2 - x3 = 8
-x1 + 7x2 - 2x3 = 4
2x1 + x2 + 9x3 = 12
Rewrite the system in matrix foam and solve it by naïve Gaussian Elimination method.
8 1 −1 8
(−1 7 −2 4 )
2 1 9 12
R1 / 8 → R1 (divide the 1st row by 8)
1 0.125 −0.125 1
(−1 7 −2 4)
2 1 9 12
R2 + 1 R1 → R2 (multiply 1st row by 1 and add it to 2nd row); R3 - 2 R1 → R3 (multiply 1st row
by 6 and subtract it from 3rd row)
1 0.125 −0.125 1
(0 7.125 −2.125 5)
0 0.75 9.25 10
R2 / 7.125→ R2 (divide the 2nd row by 7.125)
1 0.125 −0.125 1
17 40
(0 1 − )
57 57
0 0.75 9.25 10
R1 – 0.125R2→ R1 (multiply 2nd row by 0.125 and subtract it from 1st row); R3 – 0.75 R2 →
R3 (multiply 2nd row by 0.75 and subtract it from 3rd row)
5
1 0 − 2
57
17
0 1 − 1
57
180 180
(0 0 19 19 )
180 180
R3 / → R3 (divide the 3rd row by )
19 19
5 52
1 0 −
57 57
17 40
0 1 −
57 57
(0 0 1 1)
5 5 17
R1 + 57R3 → R1 (multiply 3rd row by 57and add it to 1st row); R2 + 57R3 → R2 (multiply 3rd
17
row by 57 and add it to 2nd row)
1 0 0 1
(0 1 0 1)
0 0 1 1
Answers:
x1 = 1
x2 = 1
x3 = 1
(b) Solve the following system using Naive Gaussian Elimination method with
partial pivoting
8x1 + x2 - x3 = 8
-x1 + 7x2 - 2x3 = 4
2x1 + x2 + 9x3 = 12
Rewrite the system in matrix foam and solve it by naïve Gaussian Elimination with partial
pivoting method.
8 1 −1 8
(−1 7 −2 4 )
2 1 9 12
As we see from the matrix, the 1st row is starting with greater number than other rows.
So, there is no need for switching rows. Leave the matrix as it is and perform the next step.
R1 / 8 → R1 (divide the 1st row by 8)
1 0.125 −0.125 1
(−1 7 −2 4)
2 1 9 12
R2 + 1 R1 → R2 (multiply 1st row by 1 and add it to 2nd row); R3 - 2 R1 → R3 (multiply 1st row
by 6 and subtract it from 3rd row)
1 0.125 −0.125 1
(0 7.125 −2.125 5)
0 0.75 9.25 10
Now as we see from the matrix (7.125) in 2nd row and 2nd column is greater than (0.75). So,
there is no need for switching. Leave the matrix as it is and perform the next step.
R2 / 7.125→ R2 (divide the 2nd row by 7.125)
1 0.125 −0.125 1
(0 1 −0.2982 0.7017)
0 0.75 9.25 10
R3 – 0.75 R2 → R3 (multiply 2nd row by 0.75 and subtract it from 3rd row).
1 0.125 −0.125 1
(0 1 −0.2982 0.7017)
0 0 9.4737 9.4737
As we solving by naïve Gaussian Elimination with partial pivoting method.
So, now write this matrix in the foam of equations.
1x1 + 0.125x2 – 0.125x3 = 1
0x1 + 1x2 – 0.2982x3 = 0.7017
0x1 + 0x2 + 9.4737x3 = 9.4737
By simultaneously solving these equations, we find the values of x1, x2, x3
x1 = 1
x2 = 1
x3 = 1
(c) Substitute your results into the original equations to check your answers
i. 8x1 + x2 - x3 = 8
8(1) + 1 - 1 = 8 + 1 - 1 = 8
ii. -x1 + 7x2 - 2x3 = 4
-1 + 7(1) – 2(1) = -1 + 7 - 2 = 4
iii. 2x1 + x2 + 9x3 = 12
2(1) + 1 + 9(1) = 2 + 1 + 9 = 12
(d) Compare results to MATLAB functions Gauss Pivot and Gauss Naive
% guass Elimination methode
A=[8 1 -1;-1 7 -2;2 1 9]; % here we put the values of all eqations
b=[8;4;12]; % here we put the values of right hand side
% solve linea system Ax = b
% A is an n by n matrix
% %x is an n by 1 matrix i.e column matrix
[n,~] = size(A); % find size of matrix A which is also represents number
of variables
x = zeros(n,1); % intialize x
%code for forward elimination to convert the matrix A to uppar triangular
foam
for i = 1:n-1 % i=1 2 3
m = A(i+1:n,i)/A(i,i); %multipliers let for i=1,m= A(2,1)/(A(1,1))
A(i+1:n,:) = A(i+1:n,:)- m*A(i,:); %to make the element below diaagonals
as zero
b(i+1:n,:) = b(i+1:n,:)- m*b(i,:);
end
% code for baack substitution to find unknowns
x(n,:) = b(n,:)/A(n,n); % here we find the values of variables
for i = n-1:-1:1
x(i,:) = (b(i,:)-A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
Results:
X=[1,1,1]
x1 = 1
x2 = 1
x3 = 1
Problem 3: Solve the following system:
(a) Using Naive Gaussian Elimination method.
In equations foam:
5x1 - x2 = 2
-x1 + 5x2 - x3 = 3
-x2 + 5x3 - x4 = 4
-x3 + 5x4 - x5 = 5
-x4 + 5x5 = 6
Rewrite the system in matrix form and solve it by Gaussian Elimination (Gauss-Jordan
elimination)
5 −1 0 0 0 2
−1 5 −1 0 0 3
0 −1 5 −1 0 4
0 0 −1 5 −1 5
( 0 0 0 −1 5 6)
R1 / 5 → R1 (divide the 1st row by 5)
1 −0.2 0 0 0 0.4
−1 5 −1 0 0 3
0 −1 5 −1 0 4
0 0 −1 5 −1 5
(0 0 0 −1 5 6)
R2 + 1 R1 → R2 (multiply 1st row by 1 and add it to 2nd row)
1 −0.2 0 0 0 0.4
0 4.8 −1 0 0 3.4
0 −1 5 −1 0 4
0 0 −1 5 −1 5
( 0 0 0 −1 5 6)
R2 / 4.8 → R2 (divide the 2 row by 4.8)
nd
1 −0.2 0 0 0 0.4
5 17
0 4.8 − 0 0
24 24
0 −1 5 −1 0 4
0 0 −1 5 −1 5
(0 0 0 −1 5 6)
R1 + 0.2 R2 → R1 (multiply 2 row by 0.2 and add it to 1 row); R3 + 1 R2 → R3 (multiply
nd st
2nd row by 1 and add it to 3rd row)
1 13
1 0 − 0 0
24 24
5 17
0 4.8 − 0 0
24 24
115 113
0 0 −1 0
24 24
0 0 −1 5 −1 5
(0 0 0 −1 5 6 )
115 115
R3 / 24 → R3 (divide the 3 row by 24 )
rd
1 13
1 0 − 0 0
24 24
5 17
0 4.8 − 0 0
24 24
24 113
0 0 1 − 0
115 115
0 0 −1 5 −1 5
(0 0 0 −1 5 6 )
1 1 5
R1 + 24R3 → R1 (multiply 3rd st
row by 24 and add it to 1 row); R2 + R3 → R2 (multiply
24
5
3rd row by and add it to 2nd row); R4 + 1 R3 → R4 (multiply 3rd row by 1 and add it to
24
4th row)
1 67
1 0 0 0
115 115
1 21
0 4.8 − 0 0
23 23
24 113
0 0 1 − 0
115 115
551 688
0 0 0 −1
115 115
(0 0 0 −1 5 6 )
551 551
R4 / 115→ R4 (divide the 4 row by 115)
th
67 1
1 0 0 0
115115
1 21
0 4.8 − 0 0
23 23
24 113
0 0 1 − 0
115 115
115 688
0 0 0 1 −
551 551
(0 0 0 −1 5 6 )
1 1 1
R1 + 115 R4 → R1 (multiply 4 row by 115and add it to 1 row); R2 + 23R4 → R2 (multiply
th st
1 24 24
4th row by 23 and add it to 2nd row); R3 + 115R4 → R3 (multiply 4th row by 115 and add it to
3rd row); R5 + 1 R4 → R5 (multiply 4th row by 1 and add it to 5th row)
1 327
1 0 0 0 −
551 551
1 5 533
0 4.8 − 0 −
23 551 551
24 685
0 0 1 0 −
551 551
115 688
0 0 0 1 −
551 551
2640 3994
(0 0 0 0
551 551 )
2640 2640
R5 / 551 → R5 (divide the 5th row by 551 )
1 327
1 0 0 0 −
551 551
1 5 533
0 4.8 − 0 −
23 551 551
24 685
0 0 1 0 −
551 551
115 688
0 0 0 1 −
551 551
1997
(0 0 0 0 1
1320)
1 1 5
R1 + 551R5 → R1 (multiply 5 row by 551 and add it to 1 row); R2 + 551R5 → R2 (multiply
th st
5 24 24
5th row by 551 and add it to 2nd row); R3 + 551 R5 → R3 (multiply 5th row by 551 and add it
115 115
to 3rd row); R4 + 551 R5 → R4 (multiply 5th row by 551and add it to 4th row)
787
1 0 0 0 0
1320
259
0 1 0 0 0
264
72
0 0 1 0 0
55
413
0 0 0 1 0
264
1997
(0 0 0 0 1 1320)
787
x1 =
1320
259
x2 =
264
72
x3 =
55
413
x4 =
264
1997
x5 =
1320
Make a check:
i. 5x1 - x2 = 2
787 259 787 259
5( )- = - =2
1320 264 264 264
ii. -x1 + 5x2 - x3 = 3
787 289 72 787 1295 72
- + 5( )- =- + - =3
1320 264 55 1320 264 55
iii. -x2 + 5x3 - x4 = 4
259 72 413 259 72 413
- + 5( )- =- + - =4
264 55 264 264 11 264
iv. -x3 + 5x4 - x5 = 5
72 413 1997 72 2065 1997
- + 5( )- =- + - =5
55 264 1320 55 264 1320
v. -x4 + 5x5 = 6
413 1997 413 1997
- + 5( )= - + =6
264 1320 264 264
Check completed successfully.
(b) Using MATLAB the algorithm for Tridiagonal systems of equations.
function x = Tridiag(e,f,g,r)
% input:
% e = subdiagonal vector
% f = diagonal vector
% g = superdiagonal vector
% r = right hand side vector
% output:
% x = solution vector
n=length(f);
% forward elimination
for k = 2:n
factor = e(k)/f(k-1);
f(k) = f(k) - factor*g(k-1);
r(k) = r(k) - factor*r(k-1);
end
% back substitution
x(n) = r(n)/f(n);
for k = n-1:-1:1
x(k) = (r(k)-g(k)*x(k+1))/f(k);
end
for i = 1:length(x) % Disp. xn
fprintf('\nx%d = %f\n', i, x(i));
end
in command box write this:
>> e = [0;-1;-1;-1;-1];
>> f = [5;5;5;5;5];
>> g = [-1;-1;-1;-1];
>> r = [2;3;4;5;6];
>> Tridiag(e,f,g,r);