Gauss Elimination method NxN
clc;clear;close all;
% Instruction:If you are taking user defined input then enable this 3 lines and suppress Matrix a %which is now enabled.
%p=input('Enter the coefficient matrix:');
%b=input('Enter the constant matrix:');
%a=[p,b];
a= [ 5 -1 1 10
2 4 0 12
1 1 5 -1];
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
x=zeros(1,m);
for s=m:-1:1
c=0;
for k=2:m
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
a
x'
Comments
Post a Comment