0% found this document useful (0 votes)
29 views

Assignment 3

This document contains 3 programs to solve systems of linear equations using Gauss elimination. The first program performs Gauss elimination on a 3x3 matrix A and vector B. The second program uses the matrix inverse function inv(A) to solve the system. The third program adds row pivoting to Gauss elimination to solve a different 3x3 system.

Uploaded by

nilay bhalgat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Assignment 3

This document contains 3 programs to solve systems of linear equations using Gauss elimination. The first program performs Gauss elimination on a 3x3 matrix A and vector B. The second program uses the matrix inverse function inv(A) to solve the system. The third program adds row pivoting to Gauss elimination to solve a different 3x3 system.

Uploaded by

nilay bhalgat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

%program for gauss elimination

clc
clear all
A = [10 2 -1; -3 -6 -2; 1 1 5];
B = [27 -61.5 -21.5];
AB=[A,B'];

for i=1:3
TEMP=AB(i,i);
for j=1:4
AB(i,j) = AB(i,j)/TEMP;
end
for k=i+1:3
tempvar=AB(k,i);
for m=1:4
AB(k,m) = AB(k,m)-tempvar* AB(i,m);

end
end
end
AB
X=zeros(3,1);
X(3)=AB(3,4);
X(2)=AB(2,4)-AB(2,3)*X(3);
X(1)=AB(1,4)-AB(1,3)*X(3)-AB(1,2)*X(2);
X
%program by matlab inv function
clc
clear all
A = [10 2 -1; -3 -6 -2; 1 1 5];
B = [27 -61.5 -21.5];
X=inv(A)*B';
X
%program by pivoting
clc
clear all
A = [2 -6 -1; -3 -1 7; -8 1 -2];
B = [-38 -34 -20];
AB=[A,B'];
c=0;
for j=2:3
if (abs(AB(1,j))>abs(AB(1,1)))
for p=1:4
c=AB(1,p);
AB(1,p)=AB(j,p);
AB(j,p)=c;
end
end
end
c=0;
for i=1:3
TEMP=AB(i,i);
if i==2
if (abs(AB(2,2))<abs(AB(3,2)))
for p=1:4
c=AB(2,p);
AB(2,p)=AB(3,p);
AB(3,p)=c;
end

end

end
for j=1:4
AB(i,j) = AB(i,j)/TEMP;
end
for k=i+1:3
tempvar=AB(k,i);
for m=1:4
AB(k,m) = AB(k,m)-tempvar* AB(i,m);

end
end
end
AB
X=zeros(3,1);
X(3)=AB(3,4);
X(2)=AB(2,4)-AB(2,3)*X(3);
X(1)=AB(1,4)-AB(1,3)*X(3)-AB(1,2)*X(2);
X

You might also like