Function Gauss Jordan

You might also like

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

Gauss Jordan Method

function gauss_jordan_method()
N = 4;
a = input('Enter the elements of the matrix:\n');
identity = eye(N);
aug = [a, identity];

fprintf('\nInitial Augmented Matrix:\n');


disp(aug);

for j = 1:N
for i = 1:N
if (i ~= j)
t = aug(i, j) / aug(j, j);
fprintf('\nRow Operation: R%d = R%d - (%.2f * R%d)\n', i, i, t, j);
for k = 1:(N + 1)
aug(i, k) = aug(i, k) - aug(j, k) * t;
end
fprintf('Updated Augmented Matrix:\n');
disp(aug);
end
end
end

fprintf('\nFinal Diagonal Matrix:\n');


disp(aug);

fprintf('\nThe solution is:\n');


for i = 1:N
fprintf('x[%d] = %f\n', i, aug(i, N + 1) / aug(i, i));
end
Enter the elements of the matrix:
[0.35 0.16 0.21 0.01 14;0.54 0.42 0.54 0.10 28;0.04 0.24 0.10 0.65 12.5;0.07 0.18
0.15 0.24 10.5]

Initial Augmented Matrix:


Columns 1 through 5

0.3500 0.1600 0.2100 0.0100 14.0000


0.5400 0.4200 0.5400 0.1000 28.0000
0.0400 0.2400 0.1000 0.6500 12.5000
0.0700 0.1800 0.1500 0.2400 10.5000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R2 = R2 - (1.54 * R1)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0.0400 0.2400 0.1000 0.6500 12.5000
0.0700 0.1800 0.1500 0.2400 10.5000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R3 = R3 - (0.11 * R1)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0 0.2217 0.0760 0.6489 10.9000
0.0700 0.1800 0.1500 0.2400 10.5000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R4 = R4 - (0.20 * R1)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0 0.2217 0.0760 0.6489 10.9000
0 0.1480 0.1080 0.2380 7.7000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R1 = R1 - (0.92 * R2)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0.0104 -0.0682 8.0858


0 0.1731 0.2160 0.0846 6.4000
0 0.2217 0.0760 0.6489 10.9000
0 0.1480 0.1080 0.2380 7.7000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R3 = R3 - (1.28 * R2)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0.0104 -0.0682 8.0858


0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0.1480 0.1080 0.2380 7.7000

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R4 = R4 - (0.85 * R2)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0.0104 -0.0682 8.0858


0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0 -0.0766 0.1657 2.2294
Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R1 = R1 - (-0.05 * R3)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 -0.0401 8.2260


0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0 -0.0766 0.1657 2.2294

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R2 = R2 - (-1.08 * R3)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 -0.0401 8.2260


0 0.1731 0 0.6666 9.3123
0 0 -0.2006 0.5406 2.7046
0 0 -0.0766 0.1657 2.2294

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R4 = R4 - (0.38 * R3)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 -0.0401 8.2260


0 0.1731 0 0.6666 9.3123
0 0 -0.2006 0.5406 2.7046
0 0 0 -0.0408 1.1961

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000
Row Operation: R1 = R1 - (0.98 * R4)
Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 0 7.0494
0 0.1731 0 0.6666 9.3123
0 0 -0.2006 0.5406 2.7046
0 0 0 -0.0408 1.1961

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R2 = R2 - (-16.34 * R4)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 0 7.0494
0 0.1731 0 0 28.8548
0 0 -0.2006 0.5406 2.7046
0 0 0 -0.0408 1.1961

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Row Operation: R3 = R3 - (-13.25 * R4)


Updated Augmented Matrix:
Columns 1 through 5

0.3500 0 0 0 7.0494
0 0.1731 0 0 28.8548
0 0 -0.2006 0 18.5509
0 0 0 -0.0408 1.1961

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

Final Diagonal Matrix:


Columns 1 through 5

0.3500 0 0 0 7.0494
0 0.1731 0 0 28.8548
0 0 -0.2006 0 18.5509
0 0 0 -0.0408 1.1961

Columns 6 through 9

1.0000 0 0 0
0 1.0000 0 0
0 0 1.0000 0
0 0 0 1.0000

The solution is:


x[1] = 20.141129
x[2] = 166.653226
x[3] = -92.479839
x[4] = -29.314516
Gauss elimination Method
function gauss_elimination_method()
clc
N = 4;
a = input('Enter the augmented matrix (4x5): ');

fprintf('Initial Augmented Matrix:\n');


disp(a);

for j = 1:N-1
fprintf('\nStep %d:\n', j);
for i = j+1:N
t = a(i,j) / a(j,j);
for k = j:N+1
a(i,k) = a(i,k) - a(j,k) * t;
end
disp(a);
end
end

x = zeros(N, 1);
for i = N:-1:1
s = 0;
for j = i+1:N
s = s + a(i,j) * x(j);
end
x(i) = (a(i,N+1) - s) / a(i,i);
end

fprintf('\nSolution:\n');
disp(x);
end
Enter the augmented matrix (4x5):
[0.35 0.16 0.21 0.01 14;0.54 0.42 0.54 0.10 28;0.04 0.24 0.10 0.65 12.5;0.07 0.18
0.15 0.24 10.5]
Initial Augmented Matrix:
0.3500 0.1600 0.2100 0.0100 14.0000
0.5400 0.4200 0.5400 0.1000 28.0000
0.0400 0.2400 0.1000 0.6500 12.5000
0.0700 0.1800 0.1500 0.2400 10.5000

Step 1:
0.3500 0.1600 0.2100 0.0100 14.0000
0 0.1731 0.2160 0.0846 6.4000
0.0400 0.2400 0.1000 0.6500 12.5000
0.0700 0.1800 0.1500 0.2400 10.5000

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0 0.2217 0.0760 0.6489 10.9000
0.0700 0.1800 0.1500 0.2400 10.5000

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0 0.2217 0.0760 0.6489 10.9000
0 0.1480 0.1080 0.2380 7.7000

Step 2:
0.3500 0.1600 0.2100 0.0100 14.0000
0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0.1480 0.1080 0.2380 7.7000

0.3500 0.1600 0.2100 0.0100 14.0000


0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0 -0.0766 0.1657 2.2294

Step 3:
0.3500 0.1600 0.2100 0.0100 14.0000
0 0.1731 0.2160 0.0846 6.4000
0 0 -0.2006 0.5406 2.7046
0 0 0 -0.0408 1.1961

Solution:
20.1411
166.6532
-92.4798
-29.3145

You might also like