More Related Content
Similar to Solve the set of linear equations (20)
More from shivam choubey (10)
Solve the set of linear equations
- 2. www.engineeringwithsandeep.com| Student Assignment
Gauss Elimination
% solve Gauss elimination method consider
% 2a+2b-c+d=4
% 4a+3b-c+2d=6
% 8a+5b-3c+4d=12
%3a+3b-2c+2d=6
% checking A^-1*b
A=[2 2 -1 1;4 3 -1 2;8 5 -3 4;3 3 -2 2];
b=[4 6 12 6]';
[n, n] = size(A);
[n, k] = size(b);
x = zeros(n,k); %%initial value
for i = 1:n-1
m = -A(i+1:n,i)/A(i,i);
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;
disp("Matrix before back substitution ")
A
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) -
A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
disp("Value of a ,b ,c & d ")
x
disp("Checking result")
disp("inverse of A")
val=inv(A)
disp("Value of a,b,c &d ")
val*b
Ouput
>> Gauss
Matrix before back substitution
A =
2.0000 2.0000 -1.0000 1.0000
0 -1.0000 1.0000 0
0 0 -2.0000 0
0 0 0 0.5000
Value of a ,b ,c & d
x =
1
1
-1
-1
Checking result
inverse of A
val =
0.5000 1.0000 0.2500 -1.0000
0 -1.0000 -0.5000 0
0 0 -0.5000 0
0 0 0 2.0000
Value of a,b,c &d
ans =
1
1
-1
-1
- 3. www.engineeringwithsandeep.com| Student Assignment
Gauss Jordan Method
function [x,err]=gauss_jordan_elim(A,b)
A=[2 2 -1 1;4 3 -1 2;8 5 -3 4;3 3 -2 2];
b=[4 6 12 6]';
[n,m]=size(A);
Aa=[A,b];
for i=1:n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1));
if err == 0
Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i);
end
end
x=Aa(:,n+1);
A=0;
- 4. www.engineeringwithsandeep.com| Student Assignment
Continue code
function A1=gauss_jordan_step(A,i)
[n,m]=size(A);
A1=A;
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:n]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
function [A1,err]=gauss_pivot(A)
[n,m]=size(A);
A1=A;
err = 0;
if A1(1,1) == 0
check = logical(1);
i = 1;
end
gauss_jordon
ans =
1
1
-1
-1
Checking
inv(A)
ans =
1.0000 -0.5000 0.5000 -1.0000
1.0000 0.5000 -0.5000 0
-1.0000 1.5000 -0.5000 0
-4.0000 1.5000 -0.5000 2.0000
>> inv(A)*b
ans =
1
1
-1
-1