15 views (last 30 days)

Hi, I am receiving the following error for the code trying to use Gauss elimination by pivoting. I am stuck on what I am doing incorrectly.

ERROR:

Gauss([1,2,-1;5,2,2;-3,5,-1],[2;9;1],3,[1;2;3],1000,0.001) %Inputted Values

I just keep receiving

1

2

3 as outputs versus the answer of all ones

CODE:

function Gauss(a,b,n,x,tol,er)

length s(n);

er=0;

for i=1:n

s(i)=abs(a(i,1));

for j=2:n

if abs(a(i,j))>s(i)

s(i)=abs(a(i,j));

end

end

end

Eliminate(a,s,n,b,tol,er)

if er~= -1

Substitute(a,n,b,x)

disp(x)

end

end

function Eliminate(a,s,n,b,tol,er)

for k=1: n-1

Pivot(a,b,s,n,k)

if abs(a(k,k)/s(k))<tol

er=-1;

end

end

for i=k+1:n

factor=a(i,k)/a(k,k);

for j=k+1:n

a(i,j)=a(i,j)-factor*a(k,j);

end

b(i)=b(i)-factor*b(k);

end

if abs(a(n,n)/s(n))<tol

er=-1;

end

end

function Pivot(a,b,s,n,k)

p=k;

big=abs(a(k,k)/s(k));

for ii=k+1:n

dummy=abs(a(ii,k)/s(ii));

if dummy>big

big=dummy;

p=ii;

end

end

if p~=k

for jj=k:n

dummy=a(p,jj);

a(p,jj)=a(k,jj);

a(k,jj)=dummy;

end

dummy=b(p);

b(k)=dummy;

dummy=s(p);

s(p)=s(k);

s(k)=dummy;

end

end

function Substitute(a,n,b,x)

x(n)=b(n)/(a(n,n));

for i=n-1:1:-1

sum=0;

for j=i+1:n

sum=sum+a(i,j)*x(j);

end

x(n)=(b(n)-sum)/a(n,n);

end

end

Steven Lord
on 20 Feb 2020

That's not MATLAB code. I'm not quite sure what language it is, but it's not MATLAB.

If you're new to MATLAB, I recommend going through the free 2-hour MATLAB Onramp course in the Tutorials section of the Support page (https://www.mathworks.com/support.html) to learn the basics of how to work with MATLAB.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506528-implementing-gauss-elimination-by-pivoting-with-matlab#comment_799815

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506528-implementing-gauss-elimination-by-pivoting-with-matlab#comment_799815

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506528-implementing-gauss-elimination-by-pivoting-with-matlab#comment_799996

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/506528-implementing-gauss-elimination-by-pivoting-with-matlab#comment_799996

Sign in to comment.