Gaussian elimination with backward substitution
MATLAB Program:
% Gaussian elimination with
backward substitution
n=input('Enter number of equations, n: ');
A = zeros(n,n+1);
x = zeros(1,n);
A=[4 2 3 8; 3 -5 2 -14; -2 3 8 27];
nn = n-1; m = n+1;
ichg = 0;
i = 1;
while i <= nn
ip = i;
while abs(A(ip,i)) <= 1.0e-20 & ip
<= n
ip = ip+1;
end
if ip == m
fprinff('Method fails');
else
if ip ~= i
for jj = 1:m
c = A(i,jj);
A(i,jj) = A(ip,jj);
A(ip,jj) = c;
end
ichg = ichg+1;
end
jj = i+1;
for j = jj:n
xm = A(j,i)/A(i,i);
for k = jj:m
A(j,k) = A(j,k) - xm *
A(i,k);
end
A(j,i) = 0;
end
end
i = i+1;
end
if abs(A(n,n)) <= 1.0e-20
fprinff('Method fails \n');
else
x(n) = A(n,m) / A(n,n);
for k = 1:nn
i = nn-k+1;
jj = i+1;
sum = 0;
for kk = jj:n
sum = sum - A(i,kk) * x(kk);
end
x(i) = (A(i,m)+sum) / A(i,i);
end
fprintf('\n\nThe reduced system is: \n');
for i = 1:n
for j = 1:m
fprintf(' %11.8f', A(i,j));
end
fprintf('\n');
end
fprintf('\nThe system has the
solution:\n');
for i = 1:n
fprintf(' %11.8f \n', x(i));
end
end
%Gaussian_Elimination_final.m
%Displaying
Gaussian_Elimination_final.m.
Output:
Enter number of equations, n: 3
The reduced system is:
4.00000000 2.00000000 3.00000000 8.00000000
0.00000000 -6.50000000 -0.25000000 -20.00000000
0.00000000 0.00000000 9.34615385 18.69230769
The system has the solution:
-1.00000000
3.00000000
2.00000000
No comments