In: Computer Science
Write the following MATLAB functions. Run it on an example and test it and confirm that it gives correct results. Show all results. function x=naivege(a,b) that returns the solution to Ax=b obtained by Gauss Elimination without pivoting
Matlab function
============================================================================================
function x=naivege(a,b)
% Gaussian Elimination method without pivoting
%A=[3 4 6;1 -2 7;2 3 -9];
%b=[1;10;15];
% mat is augmented matrix
mat= [a b];
%find size of matrix
[row,col]=size(mat);
for j=1:row-1
for z=2:row
if mat(j,j)==0
t=mat(j,:);mat(j,:)=mat(z,:);
mat(z,:)=t;
end
end
for i=j+1:row
mat(i,:)=mat(i,:)-mat(j,:)*(mat(i,j)/mat(j,j));
end
end
x=zeros(1,row);
for s=row:-1:1
c=0;
for k=2:row
c=c+mat(s,k)*x(k);
end
x(s)=(mat(s,col)-c)/mat(s,s);
end
end
============================================================================================
Output