-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgausselim1.m
More file actions
69 lines (68 loc) · 1.48 KB
/
gausselim1.m
File metadata and controls
69 lines (68 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
clc;
clear;
fprintf('\n');
n=input('Enter the number of unknowns (equations): ');
a=input('Enter the entries the coefficient matrix rowwise: ');
b=input('Enter the entries of the right hand side centor: ');
fprintf("The augmented matrix corresponding to the system is givne by: \n");
A=[a, b];
disp(A);
x=zeros(n,1);
% fprintf('The system is given as:\n');
m=eye(n);
% Gaussian Elimination
fprintf("Gaussian elimination steps: \n");
% Step-1
for i=1:n-1
% Step-2
fprintf("Step- %d \n\n", i);
%fprintf('\t');
p=0;
for l=i:n
if p==0 && A(l,i)~=0
p=l;
end
end
% Step-3
if p~=0
if p~=i
R=A(i,:);
A(i,:)=A(p,:);
A(p,:)=R;
end
% Step-4
for k=i+1:n
% Step-5
m(k,i)=A(k,i)/A(i,i);
% Step-6
A(k,:)=A(k,:)-m(k,i).*A(i,:);
end
end
disp(A);
end
% Step-7
if p~=0
if A(n,n)==0
if A(n,n+1)==0
fprintf("No unique solution exists.\n");
else
fprintf("No solution exists.\n");
end
else
% Backward Substitution
% Step-8
x(n)=A(n,n+1)/A(n,n);
for i=n-1:-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("Solution of the system is given by: \n");
disp(x);
end
end
if p==0
fprintf("No unique solution exists.\n");
end